How can I push a github actions artifact to my pull request

125 Views Asked by At

I am trying to set up a workflow with github actions that automatically compiles latex and images into a pdf file when a pull request is opened, than adds the final product (the pdf) to that pull request. Normally the procedure is that I do changes into a branch, then open the PR and merge into the master branch.

The pdf file (doc.pdf) is correctly generated, and after that it is made available using the `upload-artifact' action. However I can't figure out how to add it to the current PR.

The following sets up git,

      - name: git configuration
        run: |
          git config user.name "github-actions[bot]"
          git config user.email "github-actions[bot]@users.noreply.github.com"

      - name: git commit/push
        run: |
          git add doc.pdf
          git commit -m "update pdf"
          git push origin

but it fails returning the error: * (HEAD detached at pull/314/merge). I don't want to push directly to the master branch (for security reasons this requires approval by a maintainer) and I don't want to change every time the yml file to specify the name of the branch I am working on, because the idea of this is that one only modifies the tex files and the pdf is automatically created and made available upon merging to master branch.

If pushing to the open PR is not possible or recommended, what would be an alternative way to achieve the same results?

EDIT: following advice by @SM2A I have added with: ref: ${{ github.head_ref }} at the checkout action, but it causes git to fail.

The initial part of the yml file is this:

name: compile_pdf
on:
  pull_request:
    branches: [ master ]
    paths:
       - 'latex/**/*.tex'
  push:
    branches: [ master ]
    paths:
       - 'latex/**/*.tex'

jobs:
  create_pdf:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - name: Checkout repo
        uses: actions/checkout@v4
        with:
          fetch-depth: 2
          ref: ${{ github.head_ref }}
1

There are 1 best solutions below

11
SM2A On

Specify this on top of your yml file

on:
  pull_request:

The give the runner write permission by adding this to top of your file

permissions:
  contents: write

Now a code like this can do the job

- name: Checkout
  uses: actions/checkout@v2
  with:
    ref: ${{ github.head_ref }}
- name: Commit
  run: |
    git config --global user.name 'Bot'
    git config --global user.email '<>'
    git add .
    git commit -m "update pdf"
    git push