首页 > 解决方案 > 从 GitHub 操作推送到源

问题描述

我正在尝试origin从 GitHub 操作推送到远程。我的行动逻辑是:

脚本是:

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 test@test.com
  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 容器运行这个脚本:

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-action Docker 容器未配置为推送到 GitHub。我该如何配置它?是否可以使用 GitHub 提供的一些环境变量或一些挂载的文件(如/github/*路径中)?

标签: gitdockergithubgithub-actionsbuilding-github-actions

解决方案


行动/结帐@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 'your-username@users.noreply.github.com'
          git commit -am "Automated report"
          git push

如果您需要推送事件来触发其他工作流,请使用repo范围内的Personal Access Token

      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.PAT }}

行动/结帐@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 '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 操作:如何在拉取请求中自动化代码格式化


推荐阅读