从 GitHub 操作推送到源 [英] Push to origin from GitHub action

查看:36
本文介绍了从 GitHub 操作推送到源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to push to origin remote from GitHub action. The logic of my action is:

  • 处理pull_request_review 事件并按评论消息过滤
  • 结帐到 master,合并 PR 分支,运行一些检查并将其推送到 origin
  • handle pull_request_review events and filter by comment message
  • checkout to master, merge PR branch, run some checks and push it to origin

脚本是:

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.10 Docker 容器运行这个脚本:

I'm running this script from alpine:3.10 Docker container:

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:

First steps are working fine (checkout and merge), but action failed to push the merge to origin because of the error:

+ git push origin master
致命:无法读取https://github.com"的用户名:没有这样的设备或地址

+ git push origin master
fatal: could not read Username for 'https://github.com': No such device or address

看起来 GitHub-action Docker 容器没有配置为推送到 GitHub.我该如何配置它?是否可以使用一些环境变量 由 GitHub 提供,或者一些挂载的文件(如 /github/* 路径)?

It looks like GitHub-action Docker container is not configured to push to GitHub. How can I configure it? Is it possible to use some of the env variables provided by GitHub or maybe some mounted files (like in /github/* path)?

推荐答案

actions/checkout@v2

结帐的第 2 版解决了分离的 HEAD 状态问题并简化了向源的推送.

Version 2 of checkout resolves the detached HEAD state issue and simplifies pushing to origin.

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 范围的 个人访问令牌.

If you need the push event to trigger other workflows, use a repo scoped Personal Access Token.

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

actions/checkout@v1(原始答案)

为@rmunn 的出色回答添加更多细节.问题在于 actions/checkout@v1 操作使 git 存储库处于分离的 HEAD 状态.有关更多详细信息,请参阅此问题:https://github.com/actions/checkout/issues/6

To add some further detail to the excellent answer by @rmunn. The problem is that the actions/checkout@v1 action leaves the git repository in a detached HEAD state. See this issue about it for more detailed information: https://github.com/actions/checkout/issues/6

这是一个完整的示例,用于演示如何将签出的存储库置于可用状态并推送到远程.

Here is a complete example to demonstrate how to get the checked out repository to a usable state and push to the remote.

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

要包含未跟踪的(新)文件,请更改工作流程以使用以下内容.

To include untracked (new) files change the workflow to use the following.

          git add -A
          git commit -m "Automated report"

上述工作流程应该适用于大多数事件.对于 on: pull_request 工作流,应检出合并分支 (GITHUB_HEAD_REF) 以替换默认的合并提交.

The above workflow should work for the majority of events. For on: pull_request workflows the merging branch (GITHUB_HEAD_REF) should be checked out to replace the default merge commit.

重要提示:如果您除了以下工作流程之外还有其他拉取请求检查,那么您必须使用 个人访问令牌 而不是默认的 GITHUB_TOKEN.这是由于 GitHub Actions 故意限制工作流(例如 push)引发的事件无法触发进一步的工作流运行.这是为了防止意外的无限循环"情况,并作为一种反滥用措施.使用 repo 范围个人访问令牌 是一种已获批准的解决方法.请参阅此 GitHub 问题,了解有关解决方法的更多详细信息.>

Important: If you have other pull request checks besides the following workflow then you must use a Personal Access Token instead of the default GITHUB_TOKEN. This is due to a deliberate limitation imposed by GitHub Actions that events raised by a workflow (such as push) cannot trigger further workflow runs. This is to prevent accidental "infinite loop" situations, and as an anti-abuse measure. Using a repo scoped Personal Access Token is an approved workaround. See this GitHub issue for further detail on the workaround.

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 操作:如何在拉取请求中自动设置代码格式.

For further examples of push to origin during an on: pull_request workflow see this blog post, GitHub Actions: How to Automate Code Formatting in Pull Requests.

这篇关于从 GitHub 操作推送到源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆