如何设置 Github 操作仅在指定的 git 标签不存在时运行

SRe*_*ect 3 github-actions

我正在努力创建一个 github 操作来创建发布草案。在操作中,如果应用程序版本没有相应的 git 标签,我只想运行发布代码

当前的操作 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:
      # ...
Run Code Online (Sandbox Code Playgroud)

我知道我可以使用以下命令来打印标签是否存在,但我需要检查 if 中标签是否不存在:

git show-ref --tags --verify -- "refs/tags/${{jobs.test.steps.appversion.outputs.version}}" 
Run Code Online (Sandbox Code Playgroud)

我该如何将作业设置为仅在jobs.test.steps.appversion.outputs.versions不是 git 标签时运行?

SRe*_*ect 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
        # ...
Run Code Online (Sandbox Code Playgroud)

需要注意的两件事是该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

Run Code Online (Sandbox Code Playgroud)

运行上述代码后,如果steps.tagged.outputs.tagged版本已标记,则 github actions 变量将为 0,如果版本尚未标记,则 github actions 变量将为 1。

从那里,您只需检查该变量以及您只想在版本尚未标记的情况下运行的每个步骤:

# 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

Run Code Online (Sandbox Code Playgroud)

Git Show-ref 文档

Github 操作文档