GitHub 操作发布标签

NS3*_*S31 15 yaml release github devops github-actions

在 GitHub 中创建发布后,我会触发 GitHub 操作。在此操作中,我想从发布中获取一些数据,这可能吗?例如,我想获取标签,然后使用这个标签作为NuGet包版本。有没有办法从工作中获取这些数据?

Pre*_*hts 8

您可以使用${{ github.ref }}${{ github.event.release.tag_name }}

例子:

name: Release

on:
  push:
    # Sequence of patterns matched against refs/tags
    tags:
      - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10

jobs:
  deploy:    
    runs-on: ubuntu-latest
    steps:
      - name: Download artifact
        uses: actions/download-artifact@v2
        with:
          name: NameOfYourArtifact
      - name: Create release
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
        with:
          tag_name: ${{ github.ref }}
          release_name: Release ${{ github.ref }}
          body: TODO
          draft: true
          prerelease: false

      - name: Upload Release Asset
        id: upload-release-asset 
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps 
          asset_path: NameOfYourArtifact.exe
          asset_name: NameOfYourArtifact.exe
          asset_content_type: application/octet-stream
Run Code Online (Sandbox Code Playgroud)

当使用 v* 等名称创建新标签时,将执行此操作。

要触发该操作:

git push origin v1.0.0
Run Code Online (Sandbox Code Playgroud)

  • 很好的例子,谢谢。我看到 github.ref 在我的情况下返回“refs/tags/1.9.9”,然后我是否应用正则表达式来删除“ref/tags”部分? (2认同)
  • 啊,它在 ${{ github.event.release.tag_name }} 中 (2认同)

Jir*_*aya 6

这是 2022 年关于如何从 GitHub 获取标签名称的答案。

on:
  push:
    tags:
      - '*'
jobs:
  github-example-tags:
    steps:
     - name: GitHub Tag Name example
       run: |
         echo "Tag name from GITHUB_REF_NAME: $GITHUB_REF_NAME"
         echo "Tag name from github.ref_name: ${{  github.ref_name }}"
Run Code Online (Sandbox Code Playgroud)

https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables