使用 ImageMagick 的 GitHub Action : Create a PNG and commit it to the repo

标签 github yaml imagemagick github-actions git-commit

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

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. 如何/在哪里访问 .pdf从 GitHub Actions Ubuntu 机器内部将文件保存到存储库中?
  3. 我应该在哪里保存 .png文件,有没有~目录?
  4. 我如何提交 .png ImageMagick 生成的文件到存储库?
  5. git commit <file> -m "updated png version of the CV"工作?

最佳答案

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

  • 我忘记 checkout 存储库
  • 需要显式安装 Ghostscript
  • 需要编辑 ImageMagic 的安全策略才能处理 PDF
  • 在 Ubuntu 上,ImageMagick 命令不适用于 1200 dpi(我的情况是 900 dpi),在 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 }}

关于使用 ImageMagick 的 GitHub Action : Create a PNG and commit it to the repo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67060829/

相关文章:

github - 本地主机 IP 地址 :8080 -in webhooks of github +jenkins

GitHub 疯狂地搞乱 Markdown - 将 666 更改为 DCLXVI

python - 即时从 YAML/JSON 创建模型

azure - 无法使用 Azure DevOps 管道中设置的输出变量

类似于 Imagemagick 的 JavaScript 库(即调整图像和图片的大小,同时最大化图片保真度)?

github - 如何查询来自 github 组织的所有拉取请求?

github - 通过基于标签的 github 操作在 package.json 中自动增加 semver

python - python中使用YAML时的未知转义字符问题

node.js - 在node.js中使用imagemagick调整图像大小

linux - 使用终端显示图像而不会失去焦点