GitHub 使用 ImageMagick 的操作:创建 PNG 并将其提交到存储库
GitHub Action using ImageMagick: Create a PNG and commit it to the repo
背景
我正在维护一个包含 LaTeX 项目的分叉 repository。 README.md
包含 .pdf
的 .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 操作来自动执行此过程。
工作流程
我认为 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.
问题
- 看来我可以用
run:
运行 bash shell 命令;这是真的吗?
- How/where 我可以从 GitHub Actions Ubuntu 机器中访问存储库中的
.pdf
文件吗?
.png
文件应该保存在哪里,有~
目录吗?
- 如何将 ImageMagick 生成的
.png
文件提交到存储库?
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 }}
背景
我正在维护一个包含 LaTeX 项目的分叉 repository。 README.md
包含 .pdf
的 .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 操作来自动执行此过程。
工作流程
我认为 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.
问题
- 看来我可以用
run:
运行 bash shell 命令;这是真的吗? - How/where 我可以从 GitHub Actions Ubuntu 机器中访问存储库中的
.pdf
文件吗? .png
文件应该保存在哪里,有~
目录吗?- 如何将 ImageMagick 生成的
.png
文件提交到存储库? 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 }}