actions/checkout@v2 及以上
从 checkout 版本 2 开始,分离的 HEAD 状态问题已得到解决,并简化了推送到原点的过程。
name: Push commit
on: push
permissions:
contents: write
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Create report file
run: date +%s > report.txt
- name: Commit report
run: |
git config --global user.name 'Your Name'
git config --global user.email '[email protected]'
git commit -am "Automated report"
git push
如果您需要推送事件来触发其他工作流程,请使用repo
scoped 个人访问令牌.
- uses: actions/checkout@v3
with:
token: ${{ secrets.PAT }}
actions/checkout@v1(原始答案)
为@rmunn 的优秀答案添加一些进一步的细节。问题是actions/checkout@v1
操作使 git 存储库处于分离的 HEAD 状态。有关更多详细信息,请参阅此问题:https://github.com/actions/checkout/issues/6
这是一个完整的示例,演示如何使签出的存储库达到可用状态并推送到远程。
name: Push commit
on: push
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Create report file
run: date +%s > report.txt
- name: Commit report
run: |
git config --global user.name 'Your Name'
git config --global user.email '[email protected]'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git checkout "${GITHUB_REF:11}"
git commit -am "Automated report"
git push
要包含未跟踪的(新)文件,请更改工作流程以使用以下内容。
git add -A
git commit -m "Automated report"
上述工作流程应该适用于大多数事件。为了on: pull_request
合并分支的工作流程(GITHUB_HEAD_REF
)应该被签出以替换默认的合并提交。
重要的:如果除了以下工作流程之外您还有其他拉取请求检查,那么您必须使用个人访问令牌而不是默认的GITHUB_TOKEN
。
这是由于 GitHub Actions 对工作流引发的事件(例如push
) 无法触发进一步的工作流程运行。
这是为了防止意外的“无限循环”情况,并作为一种反滥用措施。
用一个repo
scoped 个人访问令牌是一个经过批准的解决方法。看这个 GitHub 问题有关解决方法的更多详细信息。
name: Push commit on pull request
on: pull_request
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
with:
ref: ${{ github.head_ref }}
- name: Create report file
run: date +%s > report.txt
- name: Commit report
run: |
git config --global user.name 'Your Name'
git config --global user.email '[email protected]'
git remote set-url origin https://x-access-token:${{ secrets.PAT }}@github.com/${{ github.repository }}
git commit -am "Automated report"
git push
有关在执行期间推送到原点的更多示例on: pull_request
工作流程请参阅这篇博文,GitHub Actions:如何在拉取请求中自动设置代码格式.