签出的版本2解决了分离的HEAD状态问题,并简化了向原点的推送。
name: Push commit
on: push
jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- 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 'your-username@users.noreply.github.com'
git commit -am "Automated report"
git push
如果您需要push事件来触发其他工作流程,请使用repo
范围限定的Personal Access Token。
- uses: actions/checkout@v2
with:
token: ${{ secrets.PAT }}
在@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 'your-username@users.noreply.github.com'
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
范围限定的个人访问令牌是一种公认??的解决方法。有关解决方法的更多详细信息,请参见此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 'your-username@users.noreply.github.com'
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git commit -am "Automated report"
git push
有关在on: pull_request
工作流期间推送到原点的更多示例,请参阅此博客文章GitHub Actions:如何在Pull Requests中自动执行代码格式设置。