gpt4 book ai didi

git - 从 GitHub 操作推送到源

转载 作者:太空狗 更新时间:2023-10-29 14:28:46 25 4
gpt4 key购买 nike

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

  • 处理 pull_request_review 事件并按评论消息过滤
  • checkout 到 master, merge PR 分支,运行一些检查并将其推送到 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 容器运行这个脚本:

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"]

第一步工作正常( checkout 和 merge ),但由于错误,操作未能将 merge 推送到 origin:

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

看起来 GitHub-action Docker 容器未配置为推送到 GitHub。我该如何配置它?是否可以使用一些 env variables由 GitHub 提供或可能是一些挂载文件(如 /github/* 路径)?

最佳答案

actions/checkout@v2

checkout 的第 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

如果您需要推送事件来触发其他工作流,请使用范围为 Personal Access Tokenrepo .

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

actions/checkout@v1(原始答案)

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

这里有一个完整的例子来演示如何将 checkout 的仓库变为可用状态并推送到远程。

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 工作流,应该 check out merge 分支 (GITHUB_HEAD_REF) 以替换默认的 merge 提交。

重要:如果除了以下工作流程之外还有其他 pull 请求检查,则必须使用 Personal Access Token而不是默认的 GITHUB_TOKEN。这是由于 GitHub Actions 故意施加的限制,即工作流引发的事件(例如 push)无法触发进一步的工作流运行。这是为了防止意外的“无限循环”情况,并作为一种反滥用措施。使用范围为 Personal Access Tokenrepo是批准的解决方法。参见 this GitHub issue有关解决方法的更多详细信息。

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: How to Automate Code Formatting in Pull Requests .

关于git - 从 GitHub 操作推送到源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57921401/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com