我正在尝试origin从GitHub动作推送到远程。我的操作逻辑是:
origin
pull_request_review
脚本是:
if [[ "${GITHUB_EVENT_NAME}" != "pull_request_review" ]]; then echo "unsupported event: ${GITHUB_EVENT_NAME}" exit 1 fi user=$(jq -r .review.user.login ${GITHUB_EVENT_PATH}) cmd=$(jq -r .review.body ${GITHUB_EVENT_PATH}) echo "reviewer is ${user}, command is ${cmd}" if [[ "${cmd}" == "merge" ]]; then head=$(jq -r .pull_request.head.ref ${GITHUB_EVENT_PATH}) git config user.email [email protected] git config user.name test git checkout -B _tmp origin/${head} git checkout -B master origin/master git merge --no-ff _tmp git push origin master fi
我正在从alpine:3.10Docker容器运行此脚本:
alpine:3.10
FROM alpine:3.10 LABEL "com.github.actions.name"="Hello world action" LABEL "com.github.actions.icon"="shield" LABEL "com.github.actions.color"="green" WORKDIR /app COPY action.sh action.sh RUN apk --update add bash git jq CMD ["bash", "/app/action.sh"]
第一步工作正常(签出和合并),但是origin由于错误,操作无法将合并推送到:
+ git push origin master 致命:无法读取“ https://github.com ”的用户名:无此设备或地址
看起来GitHub动作Docker容器未配置为推送到GitHub。我该如何配置?是否可以使用GitHub提供的某些env变量或某些挂载的文件(如/github/*path中的文件)?
/github/*
动作/结帐@ v2
签出的版本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 '[email protected]' git commit -am "Automated report" git push
如果您需要push事件来触发其他工作流程,请使用repo范围限定的Personal Access Token。
repo
- uses: actions/checkout@v2 with: token: ${{ secrets.PAT }}
动作/结帐@ v1(原始答案)
在@rmunn的出色答案中添加更多详细信息。问题是该actions/checkout@v1操作使git存储库处于分离的HEAD状态。请参阅此问题以获取更多详细信息:https : //github.com/actions/checkout/issues/6
actions/checkout@v1
这是一个完整的示例,以演示如何使已签出的存储库进入可用状态并推送到远程服务器。
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应检出合并分支()以替换默认的合并提交。
on: pull_request
GITHUB_HEAD_REF
重要提示: 如果除了以下工作流程之外还进行其他拉取请求检查,则必须使用个人访问令牌代替默认访问令牌GITHUB_TOKEN。这是由于GitHub Actions故意施加的限制,由工作流程引发的事件(例如push)无法触发进一步的工作流程运行。这是为了防止意外的“无限循环”情况,并且是一种防止滥用的措施。使用repo范围限定的个人访问令牌是一种公认的解决方法。有关解决方法的更多详细信息,请参见此GitHub问题。
GITHUB_TOKEN
push
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.GITHUB_TOKEN }}@github.com/${{ github.repository }} git commit -am "Automated report" git push
有关在on: pull_request工作流期间推送到原点的更多示例,请参阅此博客文章GitHub Actions:如何在Pull Requests中自动执行代码格式设置。