【问题标题】:Push to origin from GitHub action从 GitHub 操作推送到源
【发布时间】:2020-01-15 04:45:53
【问题描述】:

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

  • 处理pull_request_review事件并按评论消息过滤
  • checkout 到 master,合并 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"]

第一步工作正常(结帐和合并),但由于错误,操作未能将合并推送到origin

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

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

【问题讨论】:

    标签: git docker github github-actions building-github-actions


    【解决方案1】:

    您可以在存储库 URL 上使用 secrets.GITHUB_TOKEN 作为密码。所以你可以在git push 行之前添加这个:

    git remote set-url --push origin https://your_username:$GITHUB_TOKEN@github.com/your/repo
    

    这假设您已经将 GITHUB_TOKEN 密钥作为环境变量传递给您的脚本。如果不是,请添加:

    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    

    到您的工作流程步骤。

    【讨论】:

    • 谢谢。但是在使用https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git 作为远程 URL 时出现另一个错误:remote: Invalid username or password.。这个GITHUB_TOKEN 属于github-actions 应用程序,所以我不能将它用作普通用户。你不知道我应该为这个令牌使用什么用户名吗?
    • 您是否按照help.github.com/en/articles/… 的说明将令牌作为环境变量传递给您的脚本?我不知道您所说的“我不能将其用作普通用户”是什么意思;只要您按照这些说明将其作为环境变量传递,它就可用于您的脚本。至于用户名,就是你的repo的用户名:例如,对于我的github.com/rmunn/Testing repo,用户名是rmunn
    • 我发现了问题:我无法将 env 与 Docker 操作一起使用:需要使用 inputs 编写 action.yml 配置以获取令牌并从工作流 @987654335 传递它@ 作为输入,使用 with 关键字。
    【解决方案2】:

    actions/checkout@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 }}
    

    actions/checkout@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) 以替换默认合并提交。

    重要提示:如果除了以下工作流程之外您还有其他拉取请求检查,那么您必须使用 Personal Access Token 而不是默认的 GITHUB_TOKEN。 这是由于 GitHub Actions 故意施加的限制,即工作流引发的事件(例如 push)不能触发进一步的工作流运行。 这是为了防止意外的“无限循环”情况,并作为一种反滥用措施。 使用repo 范围内的Personal Access Token 是一种经过批准的解决方法。有关解决方法的更多详细信息,请参阅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

    【讨论】:

    • 有没有办法在提交之前检查是否有任何文件发生了变化?为避免在测试或使用多个节点版本时出现错误错误,并且已使用工作流的第一次运行推送了提交。
    • 我在博客文章中遵循了您的示例,但是当我在 v2 中对 if 语句使用相同的格式时,它被工作流忽略了。
    • 对于任何好奇的人,checkout@v2 凭据存储在git config http.https://github.com/.extraheader
    • @limitlessloop 使用git status
    • @commonpike 对于分支上的 push 事件,应该没有必要。也许您正在混合pushpull_request 事件?
    【解决方案3】:

    所有提供的答案都是正确的。但我想为这些答案添加更多内容。可能存在文件没有更改的情况。因此,在这种情况下,当尝试执行 git commit 命令时,它会返回一个错误,导致 GitHub 工作流失败(失败的工作流会阻止 PR 合并)。所以我们必须在提交之前检查文件是否有任何变化。

    name: Config Generator
    on:
      pull_request:
        branches: [ main ]
    
    jobs:
      config-generator:
        runs-on: ubuntu-latest
        steps: 
          - uses: actions/checkout@v2
            with:
              ref: ${{ github.head_ref }}
    
          - name: Install jsonnet
            run: sudo apt install -y jsonnet
    
          - name: Generate probe configs
            run: python3 generate-configs.py
    
          - name: Check for modified files
            id: git-check
            run: echo ::set-output name=modified::$(if [ -n "$(git status --porcelain)" ]; then echo "true"; else echo "false"; fi)
    
          - name: Update changes in GitHub repository
            if: steps.git-check.outputs.modified == 'true'
            run:  |
              git config --global user.name 'Your Name' 
              git config --global user.email 'Your Email for GitHub'
              git add -A
              git commit -m '[automated commit] add configs generated using jsonnet & GitHub workflow'
              git push
    

    以上工作流程是使用 jsonnet 和 GitHub 工作流程创建配置文件的示例。它首先安装 jsonnet 并运行一个 python 脚本,该脚本使用 jsonnet 模板创建配置文件。如上所述,可能存在没有文件更改的情况。所以它使用git status 命令来检查是否有任何文件更改。 (也可以使用git diff 代替git status。但它不会显示未跟踪的文件)。其余的 git 命令仅在文件发生更改时运行。

    还要注意,即使我使用了checkout@v2,我也必须使用ref: ${{ github.head_ref }} 来签出到源分支(在我的情况下仍然存在分离头问题)

    【讨论】:

      【解决方案4】:

      所有的回复都很棒,但我应该注意到有一个 GitHub Action Add & Commit 大大简化了流程。

      name: Commit Date
      on: push
      
      jobs:
        run:
          runs-on: ubuntu-latest
          steps:
            - uses: actions/checkout@v2
      
            - name: Create report file
              run: date +%s > report.txt
      
            - name: Commit changes
              uses: EndBug/add-and-commit@v8
              with:
                author_name: Your Name
                author_email: mail@example.com
                message: 'Your commit message'
                add: 'report.txt'
      

      【讨论】:

        【解决方案5】:

        对于不定义用户或任何内容的自动提交,请使用EndBug/add-and-commit@v7。这是我的自动格式化和提交 js 和 python 的工作流文件

        name: auto-format
        on: push
        jobs:
          format-python:
            runs-on: ubuntu-latest
            steps:
              - name: Checkout
                uses: actions/checkout@v2
        
              - name: black
                uses: lgeiger/black-action@v1.0.1
                with:
                  args: .
        
              - uses: EndBug/add-and-commit@v7
                with:
                  default_author: github_actions
        
          format-js:
            runs-on: ubuntu-latest
            steps:
              - name: Checkout
                uses: actions/checkout@v2
        
              - name: prettier
                run: npx prettier --write "./**/*.{js,html}"
        
              - uses: EndBug/add-and-commit@v7
                with:
                  default_author: github_actions
        
        
        

        【讨论】:

          猜你喜欢
          • 2020-11-17
          • 2011-05-15
          • 1970-01-01
          • 2021-11-22
          • 2022-11-25
          • 2012-04-21
          • 1970-01-01
          • 2013-08-04
          • 2020-01-13
          相关资源
          最近更新 更多