Use GitHub Actions to create a tag but not a release

Currently on my GitHub repository, I have the following workflow that releases a nightly snapshot every day, and uses the current date as release name and tag name:

name: Nightly Snapshot

on:
  schedule:
  - cron: "59 23 * * *"

jobs:
  build:
    name: Release
    runs-on: ubuntu-latest
    steps:
      - name: Get current date
        id: date
        run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
      - name: Checkout branch "master"
        uses: actions/checkout@v2
        with:
          ref: 'master'
      - name: Release snapshot
        id: release-snapshot
        uses: actions/create-release@latest
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ steps.date.outputs.date }}
          release_name: ${{ steps.date.outputs.date }}
          draft: false
          prerelease: false

GitHub labels all snapshots created this way as the latest release. However, I want to avoid this, and achieve something akin to what Swift's snapshots are like: the snapshots are only tags; although they appear among the releases, they're treated differently.

How should I modify my workflow file to make this happen? Thanks!

Upvotes: 27

Views: 51921

Answers (7)

Marcos Gomes
Marcos Gomes

Reputation: 41

Hi you don't need actions anymore. Just add the write permissions

your-job:
  permissions:
    id-token: write
    contents: read
    repository-projects: write
  steps:
    - name: Tag branch
      env:
        VERSION: ${{your_version_from_action}}
      run: |
        git tag -f -a ${VERSION} -m "Release ${VERSION}."
        git push -f origin ${VERSION}

It's done!. Enjoy.

Upvotes: 0

Sanjay Bharwani
Sanjay Bharwani

Reputation: 4759

Below is my github action code to create git tag and push

  - name: Create Tag
    uses: rickstaa/[email protected]
    with:
      tag: my_tag_name

Read more about this at https://github.com/marketplace/actions/create-update-tag

Upvotes: 0

kingwingfly
kingwingfly

Reputation: 187

If you follow Scot's method, maybe encounter Resource not accessible by integration error. Here's the solution

jobs:
  publish:
    runs-on: ubuntu-latest
    permissions: write-all // set permission
    - name: Create Tag
      uses: actions/github-script@v6
      with:
        script: |
          github.rest.git.createRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: 'refs/tags/v${{ steps.extract-version.outputs.VERSION }}',
              sha: context.sha
          })

Upvotes: 1

Scot
Scot

Reputation: 379

Building on Michael Ganß's solution, here is an example of how to create a variable dynamically.

- name: Set Dist Version
  run: |
    BUILD_NUMBER="${{ github.run_id }}-${{ github.run_number }}-${{ github.run_attempt }}"
    echo "${BUILD_NUMBER}"
    VERSION="$(mvn -q -U -Dexpression=project.build.finalName help:evaluate -DforceStdout=true -DbuildNumber=${BUILD_NUMBER})"
    echo "DIST_VERSION=${VERSION}" >> $GITHUB_ENV
- name: Create Tag
  uses: actions/github-script@v6
  with:
    script: |
      const {DIST_VERSION} = process.env          
      github.rest.git.createRef({
          owner: context.repo.owner,
          repo: context.repo.repo,
          ref: `refs/tags/${DIST_VERSION}`,
          sha: context.sha
      })

Upvotes: 6

Ben Grandin
Ben Grandin

Reputation: 176

I succeed with only : git tag + git push
I'm using gitVersion to automatically generate the tag

  semver:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Install GitVersion
        uses: gittools/actions/gitversion/[email protected]
        with:
          versionSpec: '5.x'
      - name: Determine Version
        uses: gittools/actions/gitversion/[email protected]
      - name: Display SemVer
        run: |
          echo "SemVer: $GITVERSION_SEMVER" && echo "$version" && echo "$major.$minor.$patch"
      - name: Create git tag
        run: |
          git tag $GITVERSION_SEMVER
      - name: Push git tag
        run: git push origin $GITVERSION_SEMVER

Upvotes: 7

Michael Ganß
Michael Ganß

Reputation: 3386

Another option is to use GitHub Script. This creates a lightweight tag called <tagname> (replace this with the name of your tag):

      - name: Create tag
        uses: actions/github-script@v5
        with:
          script: |
            github.rest.git.createRef({
              owner: context.repo.owner,
              repo: context.repo.repo,
              ref: 'refs/tags/<tagname>',
              sha: context.sha
            })

Upvotes: 95

Edit: Michael Ganß's solution is better.


I found this GitHub action that tags on demand. Using it, my workflow can be revised as such:

name: Nightly Snapshot

on:
  schedule:
  - cron: "59 23 * * *"

jobs:
  tag:
    name: Tag
    runs-on: ubuntu-latest
    steps:
      - name: Get current date
        id: date
        run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
      - name: Checkout branch "master"
        uses: actions/checkout@v2
        with:
          ref: 'master'
      - name: Tag snapshot
        uses: tvdias/[email protected]
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.date.outputs.date }}

Upvotes: 11

Related Questions