GitHub动作*** NO_CI ***替代

汤玛斯·赫伯鲍尔

我正在尝试将代码从GitHub Actions Beta(YAML版本)中运行的工作流推送到GitHub存储库。只需git pushorigin远程URL设置为包含凭据远程URL之后运行,即可轻松做到这一点,以便Git知道如何通过GitHub进行身份验证。

GitHub Actions甚至提供了一个开箱即用的PAT:,GITHUB_TOKEN可以使用它,并且整个系统足够聪明,以至于当使用已知与先前工作流运行相关联的PAT将提交推送到存储库时,就不会调度其他操作。

太好了,但是,有一些使用自定义PAT的用例(在我的情况下触发GitHub Pages的构建,现成的PAT不会这样做),并且使用自定义PAT失去了让GitHub知道的优势在推送当前工作流中的提交后不运行其他工作流。

结果,当使用自定义PAT时,人们会陷入工作流程的无限循环中。许多CI解决方案都以***NO_CI***提交消息为荣,并且如果此字符串出现在提交消息中,则不会运行任何检查/构建/任何操作。GitHub Actions Beta并不关心它。

我唯一能想到的就是使GitHub Action工作流的工作/步骤成为条件,并构造如下条件:

- if: !contains(github.commit_message, "***NO_CI***")

但是,github上下文对象没有包含提交消息的字段,并且上下文似乎没有足够的表现力,无法让我运行命令来使用Git从SHA获取提交消息并在其contains运行

我有什么选择可以实现这一目标吗?

汤玛斯·赫伯鲍尔

GitHub Actions***NO_CI***本身不支持,而是依靠您使用预定义的secrets.GITHUB_TOKEN上下文值来推送到存储库。如果使用此令牌,GitHub Actions将对此令牌进行特殊处理,并且不会发出任何新的工作流运行。但是,当使用此令牌进行推送时,他们也不会部署GitHub Pages。

我发现,为了实现这两种行为(将工作流程推送到存储库不会导致另一个工作流程运行+从推送到存储库的工作流程运行工件构建GitHub Pages),有必要结合以下两点:

  • secrets.GITHUB_TOKEN做推到仓库
  • 使用自定义PAT,并将其repo_publicrepo放在私有范围内)作用域放置在存储库设置的“秘密”选项卡中,并使用secrets.GITHUB_PAGES_PAT
  • 使用带有自定义PAT的API手动运行GitHub Pages部署(默认令牌标记为集成令牌,不允许调用此API方法)

一个例子.github/workflows/main.yml

name: github-pages
on:
  push:
    branches:
    # Limit to the `master` branch
    - master
  schedule:
    # Run hourly
    - cron:  '0 * * * *'
jobs:
  github-pages:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v1
    - name: Generate GitHub Pages
      run: |
        set -x
        # Configure Git for the push from the workflow to the repository
        git config --global user.email "[email protected]"
        git config --global user.name "Tomas Hubelbauer"
        # Check out the `master` branch because by default GitHub Actions checks out detached HEAD
        git checkout master
        # Run script which generates your artifacts to push to the repo
        ./script.sh
        # Add generated artifacts to Git stage
        git add out
        # Reset unstaged changes to prevent `git commit` from yelling if there's changes outside of `out` (cache, …)
        git checkout -- .
        # Commit the changes to the Git repository to deploy GitHub Pages (if any)
        if git diff-index --quiet HEAD --; then
          exit
        fi
        git commit -m "Generate GitHub Pages"
        # Authenticate with GitHub using the default integration PAT (this one won't deploy GitHub Pages)
        git remote set-url origin https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
        # Pull before pushing to integrate fast forward changes if any
        git pull --rebase
        # Push the changes to GitHub without causing another workflow run thanks to the default integration PAT
        git push
        # Enqueue a GitHub Pages deployment using the API with the custom PAT with repo_public or repo (private) scope
        curl -f -X POST -H "Authorization: token ${{ secrets.GITHUB_PAGES_PAT }}" -H "Accept: application/vnd.github.mister-fantastic-preview+json" "https://api.github.com/repos/${{ github.repository }}/pages/builds"

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章