Use GitHub Actions to create a tag but not a release
Asked Answered
S

6

24

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!

Snowflake answered 1/4, 2020 at 4:32 Comment(0)
F
88

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
            })
Favourite answered 22/10, 2020 at 9:31 Comment(12)
tagname will contain the value used for naming your tagCrackle
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!Partook
@MohammedAli maybe you are running this workflow with a filter for master/main branch only or maybe you are passing the wrong SHA?Scoliosis
does it need PAT? github-token: ${{ secrets.MY_PAT }}Trilateration
Note that the job needs the correct permissions (docs.github.com/en/actions/using-workflows/…)Babar
In case some one wants to know how to pass tag name variable in, this works for me ref: 'refs/tags/${{ inputs.tag_name }}',Skillet
Note that the same code works with actions/github-script@v6.England
Note that if you use this mention with the GITHUB_TOKEN instead of a PAT or Deploy Key, tag generation will work but it will not trigger any other workflows that use on.push.tags triggers. See github.com/orgs/community/discussions/27028 for why.Lymphatic
What would be the equivalent of an annotated type tag? I guess it differs from the lightweight no?Sextans
In case you have a permission error, adding ``` permissions: contents: write ```Bisectrix
Note that this can only tag the head commit. Tagging any other commit requires permission workflows:write which is not available via GITHUB_TOKEN and must use a PAT. github.com/orgs/community/discussions/121022Shoring
Hi can this work also to update a tag? what about annotated tags? thanks. I also found github.com/marketplace/actions/create-update-tagMarketing
S
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/[email protected]
        with:
          repo-token: ${{ secrets.GITHUB_TOKEN }}
          tag: ${{ steps.date.outputs.date }}
Snowflake answered 3/4, 2020 at 19:15 Comment(2)
Possible alternative: github.com/simpleactions/create-tagAuricular
Michael Ganß's solution uses GitHub's own API directly, instead of through a 3rd party action.Snowflake
W
7

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
Wyeth answered 19/9, 2022 at 13:39 Comment(0)
H
5

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
      })
Hallo answered 9/12, 2022 at 5:7 Comment(0)
M
1

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
          })
Mendenhall answered 18/12, 2023 at 16:48 Comment(1)
If you use this and it still gives you Resource not accessible by integration, it's probably because you're trying to tag a commit that isn't the head commit: github.com/orgs/community/discussions/121022Shoring
I
0

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

Imminence answered 17/3 at 10:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.