GitHub Action using ImageMagick: Create a PNG and commit it to the repo

I asked this question earlier today on Stack Overflow, however, I think it fits better here…

Background
I’m maintaining a forked repository that contains a LaTeX project. The README.md contains a .png preview of the .pdf, compiled from the example .tex file included in the repo. I often forget to update the .png version using the following ImageMagick command:

    magick convert -density 1200 -background white -alpha off Twenty-Seconds-Icons_cv.pdf -quality 90 Twenty-Seconds-Icons_cv.png

Thus I would like to automate this process using GitHub Actions.

Workflow
I think the main.yml file should look something like this, however, I don’t fully understand what I am doing.

name: PDF to PNG
on:
  push:
    branches:
      - kaspar
  pull_request:
    branches:
      - kaspar

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Install ImageMagick
        run: sudo apt install imagemagick # Seems to work and already be included with ubuntu-latest...
      - name: Convert PDF to PNG
        run: # How?
      - name: Commit the updated PNG 
        run: # How?

Output:

Run sudo apt install imagemagick
  sudo apt install imagemagick
  shell: /usr/bin/bash -e {0}

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Reading package lists...
Building dependency tree...
Reading state information...
imagemagick is already the newest version (8:6.9.10.23+dfsg-2.1ubuntu11.2).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Questions
It seems like this should be a pretty basic task, therefore, I would prefer to do everything from the main.yml file without creating a separate repository for this action, if possible.

  1. It seems like I can just run bash shell commands with run: ; is this true?
  2. How/where can I access the .pdf file in the repo from inside the GitHub Actions Ubuntu machine?
  3. Where should I save the .png file, is there a ~ directory?
  4. How can I commit the .png file generated by ImageMagick to the repo?
  5. Would git commit <file> -m "updated png version of the CV" work?

It seems like I figured out the/a solution myself.
I would be very thankful for feedback (code review) since my experience with GitHub Actions is very limited…

name: PDF to PNG
on:
  push:
    branches:
      - kaspar
  pull_request:
    branches:
      - kaspar

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Install ghostscript
        run: sudo apt install ghostscript
      - name: Change ImageMagick security policy
        run: |
          DQT='"' 
          SRC="rights=${DQT}none${DQT} pattern=${DQT}PDF${DQT}"
          RPL="rights=${DQT}read\|write${DQT} pattern=${DQT}PDF${DQT}"
          sudo sed -i "s/$SRC/$RPL/" /etc/ImageMagick-6/policy.xml
      - name: Convert PDF to PNG
        run: convert -density 900 -background white -alpha off Twenty-Seconds-Icons_cv.pdf -quality 90 Twenty-Seconds-Icons_cv.png
      - name: Commit PNG
        id: commit
        run: |
          git config --local user.email "action[bot]@github.com"
          git config --local user.name "github-actions[bot]"
          git add Twenty-Seconds-Icons_cv.png
          if [-z "$(git status --porcelain)"]; then
             echo "::set-output name=push::false"
          else
             git commit -m "[bot] updated Twenty-Seconds-Icons_cv.png"
             echo "::set-output name=push::true"
          fi
        shell: bash
      - name: Push Commit
        if: steps.commit.outputs.push == 'true'
        uses: ad-m/github-push-action@master
        with:
           github_token: ${{ secrets.SECRET_TOKEN }}

What I was doing wrong:

  • I did not checkout the repository
  • ghostscript was missing :ghost:
  • The security policy for ImageMagic needs to be edited for it to process PDFs :angry:
  • On Ubuntu the ImageMagick command doesn’t work for 1200 dpi (900 is ok in my case), on Windows, this works just fine :man_shrugging:t2:
  • I found out how to commit and push