NWLT
NWLT

Reputation: 11

Github Actions won't commit changes

I'm trying to realize a Github Action, which will run a python script that creates a Markdown file, commit changes to a new branch and finally create a pull request to main from this new branch.

For now it creates the MD file, creates the branch but with no commited changes.

I don't know whats wrong with the workflow, maybe some can help me.

name: get_repo_admins_list

on: [push]
  # schedule:
  #   - cron: '0 08 * * 1' # runs at 08:00 UTC on Mondays

jobs:
  updatelist:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v3

      - name: Install reqs
        run: pip install -r requirements.pip

      - name: Run script
        shell: bash
        run: ./devops/scripts/get_repo_admins.py -at ${{ secrets.GITHUB_TOKEN }}

      - name: Create new Branch
        uses: EndBug/add-and-commit@v9
        with:
          default_author: github_actions
          message: '[Misc] Update repository admin list documentation'
          new_branch: scriptupdate/updated-admin-list
          
      - name: Create Pull Request
        uses: peter-evans/create-pull-request@v4
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          commit-message: "[Misc] Update repository admin list documentation"
          committer: GitHub <[email protected]>
          author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
          signoff: false
          base: main
          branch: scriptupdate/updated-admin-list
          title: '[Misc] Update repository admin list documentation'
          body: |
            Workflow script has made changes to admin list
          reviewers: someuser
          draft: false

The python script is working properly, but if someone needs to check it, I can post it to.

Upvotes: 0

Views: 3920

Answers (2)

NWLT
NWLT

Reputation: 11

Thank you guys for the answers!

Because I could not realize it with the peter-evans/create-pull-request@v4 action, I made it with CURL.

Thank you Ondrej Tuma for your answer. It helped me with the commit and add action a lot!

This is my final Code:

name: get_repo_admins_list

on: [push]
  # schedule:
  #   - cron: '0 08 * * 1' # runs at 08:00 UTC on Mondays

jobs:
  updatelist:
    runs-on: ubuntu-latest

steps:
  - name: Checkout Repository
    uses: actions/checkout@v3
    with:
      repository: ${{ github.event.pull_request.head.repo.full_name }}
      ref: ${{ github.event.pull_request.head.ref }}

  - name: Install reqs
    run: pip install -r requirements.pip         

  - name: Run script
    shell: bash
    run: ./devops/scripts/get_repo_admins.py -at ${{ secrets.GITHUB_TOKEN }}

  - name: Create branch and commit
    uses: EndBug/add-and-commit@v9
    with:
      add: './REPOADMINS.md'
      default_author: github_actions
      message: '[Misc] Update repository admin list documentation'
      new_branch: scriptupdate/updated-admin-list
      push: origin scriptupdate/updated-admin-list --set-upstream --force

  - name: Create Pull Request
    shell: bash
    run: |
      curl \
      -X POST \
      -H 'authorization: Bearer ${{ secrets.GITHUB_TOKEN }}' \
      -H "Accept: application/vnd.github.v3+json" \
      https://api.github.com/repos/(USER)/(REPO)/pulls \
      -d '{"title":"[Misc] Update repository admin list documentation","body":"Adminlist updated!","head":"scriptupdate/updated-admin-list","base":"main"}

Upvotes: 1

Ondřej Tůma
Ondřej Tůma

Reputation: 264

Try adding this to your checkout step:

- name: Checkout Repository
  uses: actions/checkout@v3
  with:
    repository: ${{ github.event.pull_request.head.repo.full_name }}
    ref: ${{ github.event.pull_request.head.ref }}

According to their documentation you need to specify a ref in order to checkout your repo in non-detached state.

Also from reading their docs I recommend adding push input:

push: origin scriptupdate/updated-admin-list --set-upstream --force

If you plan on using this branch multiple times

Upvotes: 1

Related Questions