使用GitHub Actions创建标签但不发布版本

20

目前在我的GitHub存储库上,我有以下工作流程,每天发布一个夜间快照,并使用当前日期作为发布名称和标签名称:

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将以这种方式创建的所有快照标记为最新的发布版。然而,我想避免这种情况,并实现类似于Swift快照的东西:这些快照只是标签;尽管它们出现在发布版本中,但处理方式不同。

我应该如何修改我的工作流程文件以实现这一点?谢谢!

4个回答

78

另一个选择是使用GitHub Script。 这会创建一个名为<tagname>的轻量级标签(请将其替换为您的标签名称):

      - 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
            })

1
tagname将包含用于命名您的标记的值。 - Alberto S.
另外一个问题是,是否可以从特定的分支或标签中进行发布?我已经尝试了上述方法,但它总是从主分支创建发布。我甚至在此步骤之前检出了相关的分支/标签,但没有成功! - Mohammed Ali
需要PAT吗? github-token: ${{ secrets.MY_PAT }} - Tilo
请注意,此任务需要正确的权限(https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#permissions)。 - Bennik2000
如果有人想知道如何传递标签名变量,请使用以下方式 ref: 'refs/tags/${{ inputs.tag_name }}', - Wayne Mao
显示剩余3条评论

11

编辑:Michael Ganß的解决方案更好。


我发现了这个GitHub action,它可以按需进行标记。使用它,我的工作流可以进行如下修改:

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 }}

2
可能的替代方案:https://github.com/simpleactions/create-tag - AKd
4
Michael Ganß的解决方案直接使用GitHub自己的API,而非通过第三方操作。 - Wowbagger and his liquid lunch

3

在Michael Ganß的解决方案基础上,这里提供一个动态创建变量的示例。

- 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
      })

3

我只使用 git tag + git push 就成功了
我使用 gitVersion 自动生成标签

  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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接