19

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!

4 Answers4

75

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
            })
silkfire
  • 24,585
  • 15
  • 82
  • 105
Michael Ganß
  • 2,846
  • 1
  • 18
  • 11
  • 1
    `tagname` will contain the value used for naming your tag – Alberto S. Nov 15 '21 at 12:07
  • On a different note is it possible to do a Release from a specific Branch or Tag, i have used the above and it always creates a Release from Master/Main Branch, i even checked out the Relevant Branch/Tag Before this step but no luck! – Mohammed Ali Jan 26 '22 at 18:22
  • @MohammedAli maybe you are running this workflow with a filter for master/main branch only or maybe you are passing the wrong SHA? – Danny Varod Jun 06 '22 at 16:01
  • does it need PAT? `github-token: ${{ secrets.MY_PAT }}` – Tilo Feb 27 '23 at 22:30
  • Note that the job needs the correct permissions (https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions) – Bennik2000 Apr 11 '23 at 21:11
  • In case some one wants to know how to pass tag name variable in, this works for me `ref: 'refs/tags/${{ inputs.tag_name }}',` – Wayne Mao Aug 10 '23 at 14:26
  • Note that the same code works with `actions/github-script@v6`. – d4tm4x Aug 23 '23 at 08:51
11

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/github-tagger@v0.0.1
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.date.outputs.date }}
3

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
      })
Scot
  • 319
  • 3
  • 9
2

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/setup@v0.9.7
        with:
          versionSpec: '5.x'
      - name: Determine Version
        uses: gittools/actions/gitversion/execute@v0.9.7
      - 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
Ben Grandin
  • 129
  • 1
  • 5