【问题标题】:How can I cancel a GitHub Actions workflow if the commit has no tag如果提交没有标签,我如何取消 GitHub Actions 工作流程
【发布时间】:2021-11-01 16:43:49
【问题描述】:

我有 npm publish github 动作,如果我的提交有标签,我想运行这个动作,否则我不想运行我的动作,因为如果我不添加任何标签我的提交然后动作运行并失败因为它尝试发布已经发布的具有相同标签的 npm 包。例如,在我的最后一次提交中,我有标签 1.2.3 并且我的 npm 包是使用 1.2.3 版本发布的。当我在没有任何标签操作的情况下向我的分支添加新提交时,尝试使用 1.2.3 版本标签发布我的包,所以它失败了。下面是我的操作代码,有什么解决办法吗。

感谢您的建议。

name: NPM Publish

on:
  push:
    branches:
    - master
    tags: 
    - v*

jobs:
  build:
    name: Build ???? & Publish ????
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v2.4.0
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/      
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

我在 yml 文件中需要类似的东西

if(git_commit has tag) continue job else stop job;

编辑版本

我根据@Enrico Campidoglio 的建议编辑了我的 yml 文件,但仍然无法正常工作。我做了两个提交,第一个没有标签,它取消了动作,但第二个有标签它仍然取消了动作。有什么新的建议或解决方案吗?

name: NPM Publish

on:
  push:
    branches:
    - master

jobs:
  build:
    name: Build ???? & Publish ????
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v1
      - uses: actions/setup-node@v2.4.0
        with:
          node-version: 12
          registry-url: https://registry.npmjs.org/
      - run: echo "GIT_COMMIT=`echo $(git rev-parse --short HEAD)`" >> $GITHUB_ENV
      - run: echo "GIT_TAG=`echo $(git describe --tags --exact-match ${{ env.GIT_COMMIT }} || :)`" >> $GITHUB_ENV
      - run: echo ${{ env.GIT_TAG }} != v*
      - run: |
          if [[ ${{ env.GIT_TAG }} == v* ]] ; then
          echo "Tag found..."
          else
          echo "No git tag found, action cancelled..."
          exit 1
          fi


      - uses: andymckay/cancel-action@0.2
        if: ${{ env.GIT_TAG }} != v*
          
      - run: npm install
      - run: npm publish --access public
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}

有动作结果我不知道是什么问题,

这里是最近失败的操作:https://github.com/sametcelikbicak/enum2array/runs/3513521031?check_suite_focus=true

【问题讨论】:

    标签: github github-actions


    【解决方案1】:

    暂时没有取消当前工作流程的官方行动。但是,有 an official GitHub APIa third-party action 调用它。您可以将其与 if conditionalgithub context 结合使用以实现您想要的:

    steps:
      - uses: andymckay/cancel-action@0.2
        if: startsWith(github.ref, 'refs/tags')
    

    请注意,通过 API 取消工作流是一个异步操作,这意味着在工作流运行程序处理请求之前,后续步骤可能仍会执行。

    更可靠的方法是在发布步骤中设置一个条件,使其仅在工作流由新标签触发时运行:

    steps:
      - run: npm publish --access public
        if: startsWith(github.ref, 'refs/tags')
        env:
          NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
    

    【讨论】:

    • 感谢您的回答,我做了一些更改,并用新添加的更改更新了我的问题,但仍然无法正常工作:(
    • 您更新的工作流程看起来不正确。第一步调用cancel-action 没有任何if 条件,这意味着它总是会取消构建。
    • 哪个地方或行不正确,你能在我最新的 yml 文件中通知那个地方吗?
    • - uses: andymckay/cancel-action@0.2 文件中的yml 行需要if 条件。您可以在我回答的第一个示例中看到它。但是,我建议您将其完全删除,并在工作流由标签触发时发布,就像您在 - run: npm publish --access public 行下方所做的那样。
    • 我猜- uses: andymckay/cancel-action@0.2 if: ${{ env.GIT_TAG }} != v* 行有问题,我该如何处理这个控件?
    【解决方案2】:

    经过多次尝试,我终于找到了解决方案。我改变主意并尝试运行shell script,它可以工作:)

    只需在我的 yml 文件中添加该行

          - name: Check Git Tag to continue publish
            run: ./scripts/publish.sh
    

    我创建了一个sh 文件来控制提交标签。您可以在下面找到最新的脚本和 yml 文件定义

    这是我最新的yml 文件,npm-publish.yml

    name: NPM Publish
    
    on:
      push:
        branches:
        - master
    
    jobs:
      build:
        name: Build ? & Publish ?
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v1
          - uses: actions/setup-node@v2.4.0
            with:
              node-version: 12
              registry-url: https://registry.npmjs.org/
    
          - name: Check Git Tag to continue publish
            run: ./scripts/publish.sh
              
          - run: npm install
          - run: npm publish --access public
            env:
              NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
    

    这是我的script 文件,publish.sh

    #!/usr/bin/env bash
    
    GIT_COMMIT=$(git rev-parse --short HEAD)
    GIT_TAG=$(git describe --tags --exact-match $COMMIT || :)
    
    if [[ ${GIT_TAG} == v* ]] ; then
        echo "$GIT_TAG Tag found..."
    else
        echo "No git tag found, action cancelled..."
        exit 1
    fi
    

    【讨论】:

      猜你喜欢
      • 2022-01-10
      • 1970-01-01
      • 2020-04-02
      • 2022-08-13
      • 1970-01-01
      • 2020-03-23
      • 1970-01-01
      • 2020-01-18
      • 2015-01-31
      相关资源
      最近更新 更多