13
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

GitHub Actions で TeX をコンパイル・PDF 化してタグ付きリリースする話

Posted at

概要

GitHub Actions を使って「タグを push したら TeX のコンパイルと PDF 化を実行&リリース」し,タグ名を含めることでリリース名をわかりやすくする.拙著の続きのような内容である.

詳細

下記の通りファイルを用意する.同じものがこのリポジトリにある.

ファイルツリーは前回の拙著と変わらない.

.
│  main.tex
│
└─.github
    ├─actions
    │  └─latex
    │          Dockerfile
    │          entrypoint.sh
    │
    └─workflows
            main.yml

Dockerfile も特に変更点はない.

Dockerfile
FROM ubuntu:latest

RUN apt-get update \
  && apt-get install -y --no-install-recommends \
    curl \
    python3 \
    latexmk \
    lmodern \
    texlive \
    texlive-latex-extra \
    texlive-lang-japanese \
  && rm -rf /var/lib/apt/lists/*

RUN mktexlsr && mkdir -p /app
WORKDIR /app

ADD entrypoint.sh /entrypoint.sh
RUN ["chmod", "+x", "/entrypoint.sh"]
ENTRYPOINT ["/entrypoint.sh"]

entrypoint.sh\"tag_name\"\"name\" に渡す値が変わっている.タグを push した場合,GITHUB_REF には refs/tags/v1.0.0 のような値が入るので,そこからタグ名を取り出している.これについては下記を参照した.

entrypoint.sh
#!/bin/bash
set -eux

# build pdf (change if necessary)
ptex2pdf -l -ot -kanji=utf8 main.tex

# create release
res=`curl -H "Authorization: token $GITHUB_TOKEN" -X POST https://api.github.com/repos/$GITHUB_REPOSITORY/releases \
-d "
{
  \"tag_name\": \"$(echo ${GITHUB_REF:10})\",
  \"target_commitish\": \"$GITHUB_SHA\",
  \"name\": \"main.pdf $(echo ${GITHUB_REF:10})\",
  \"draft\": false,
  \"prerelease\": false
}"`

# extract release id
rel_id=`echo ${res} | python3 -c 'import json,sys;print(json.load(sys.stdin)["id"])'`

# upload built pdf
curl -H "Authorization: token $GITHUB_TOKEN" -X POST https://uploads.github.com/repos/$GITHUB_REPOSITORY/releases/${rel_id}/assets?name=main.pdf\
  --header 'Content-Type: application/pdf'\
  --upload-file main.pdf

main.yml では,新たにトリガーの条件として tags: v*.*.* を付け加えている.

main.yml
name: LaTeX to PDF

on:
  push:
    paths: main.tex
    tags: v*.*.*

jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Set up Git repository
        uses: actions/checkout@v1
      - name: Build docker image
        uses: denkiuo604/tex-to-pdf-with-tags/.github/actions/latex@master
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

これで v*.*.* 形式のタグを push すれば PDF が自動生成され,main.pdf v*.*.* という名前でリリースされる.

参考

13
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?