如何设置 Github Action,仅在不存在指定的 Git 标签时运行

3

我正在努力创建一个能够创建发布草稿的Github Action。在这个Action中,我希望只有当应用程序版本没有相应的git标签时才运行发布代码。

当前的Action YAML文件类似于:

# ...

jobs:
  # test, winbuild and linuxbuild jobs


  draftrelease:
    needs: [test, winbuild, linuxbuild]
    runs-on: ubuntu-latest
    # if ${{jobs.test.steps.appversion.outputs.version}} is not a tag
    steps:
      # ...

我知道我可以使用以下代码来打印标签是否存在,但我需要检查标签是否不存在于if语句中:
git show-ref --tags --verify -- "refs/tags/${{jobs.test.steps.appversion.outputs.version}}" 

如果 jobs.test.steps.appversion.outputs.versions 不是 git tag,我该如何设置工作任务才能运行?


这里有一个链接:https://camel.readthedocs.io/en/latest/yamlref.html,但是仍然没有官方的YAML参考指南。YAML网站只提供了密集而棘手的规范。感觉所有与CI相关的脚本都是互相复制粘贴的,很少有人真正理解它的作用,因此无法修改以适应自己的需求。我也需要这个,并且已经设置了赏金。 - Cee McSharpface
1个回答

7
我使用一个模块来读取应用程序版本,检查 git 标签,然后在每个构建步骤中检查一个变量,从而实现了这一点。
jobs:
  build:
    name: Compile Bundles
    strategy:
      matrix:
        os: [windows-latest, ubuntu-latest]

    runs-on: ${{ matrix.os }}
    steps:
      - name: Checkout master branch
        uses: 'actions/checkout@v2'

      # Fetches all tags for the repo
      - name: Fetch tags
        run: git fetch --depth=1 origin +refs/tags/*:refs/tags/*

      # Reads the app's version
      #   In my case, I have a nodejs project, so I read the app's version
      #   from package.json. You may need to find a different github action
      #   module specific to your language to extract the app version
      - name: Read package.json
        id: package
        uses: gregoranders/nodejs-project-info@v0.0.1

      # Check if the app version has a git tag
      # If there is a git tag for the version set the variable 'tagged' to 0
      # if there is NOT a git tag for the version set the variable 'tagged' to 1
      - name: 'Check: package version has corrosponding git tag'
        id: tagged
        shell: bash
        run: git show-ref --tags --verify --quiet -- "refs/tags/v${{ steps.package.outputs.version }}" && echo "::set-output name=tagged::0" || echo "::set-output name=tagged::1"

      - name: Step to only run if there is no git-tag for the version
        if: steps.tagged.outputs.tagged == 1
        # ...

      - name: Another step to only run if there is no git-tag for the version
        if: steps.tagged.outputs.tagged == 1
        # ...

两个需要注意的关键点是 git --show-ref 命令和随后在后续步骤中的 if: 语句。
# Attempt to output the tagged reference for the app's version
# In my case all version tags are prefixed with 'v' so you may need to alter
# this line to better suit your needs
git show-ref --tags --verify --quiet -- "refs/tags/v${{ steps.package.outputs.version }}"

  # If outputting the tag was successful set the variable indicating the tag exists
  && echo "::set-output name=tagged::0"

  # if outputting failed/errored, set the variable indicating the tag does not exist
  || echo "::set-output name=tagged::1


一旦上述步骤已运行,则 Github Actions 变量 steps.tagged.outputs.tagged 将为 0(已标记版本)或 1(未标记版本)。接下来,您只需检查该变量以确定是否仅运行未标记版本的每个步骤即可。
if: steps.tagged.outputs.tagged == 1

Git Show-ref 文档

Github Actions 文档


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