使用 ImageMagick 的 GitHub Action:创建一个 PNG 并将其提交到存储库

问题描述

背景
我正在维护一个包含 LaTeX 项目的分叉 repositoryREADME.md 包含从存储库中包含的示例 .png 文件编译的 .pdf.tex 预览。我经常忘记使用以下 ImageMagick 命令更新 .png 版本:

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

因此,我想使用 GitHub Actions 自动执行此过程。

工作流程
我认为 main.yml 文件应该看起来像这样,但是,我不完全明白我在做什么。

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?

输出

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.

问题

  1. 似乎我可以使用 run: 运行 bash shell 命令;这是真的吗?
  2. 如何/在哪里可以从 GitHub Actions Ubuntu 机器内部访问存储库中的 .pdf 文件
  3. 我应该在哪里保存 .png 文件,是否有 ~ 目录?
  4. 如何将 ImageMagick 生成.png 文件提交到存储库?
  5. git commit <file> -m "updated png version of the CV" 可以吗?

解决方法

我找到了解决问题的方法:

  • 我忘记签出存储库
  • 需要明确安装ghostscript
  • 需要编辑 ImageMagic 的安全策略才能处理 PDF
  • 在 Ubuntu 上,ImageMagick 命令不适用于 1200 dpi(在我的情况下为 900),在 Windows 上,这工作得很好
  • 我发现了如何提交和推送文件
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 }}