Skip to content

Commit dca7653

Browse files
committed
chore(workflows/preview_build): PR の更新毎に gh-pages /gen/pull/<番号>/ にプレビューをアップロード
cpprefjp#1273 (comment) PR 内容の変換結果を https://cpprefjp.github.io/site/gen/pull/<番号> か ら閲覧することができるようにする。
1 parent 5b1731b commit dca7653

File tree

3 files changed

+115
-28
lines changed

3 files changed

+115
-28
lines changed

.github/workflows/build.yml

Lines changed: 66 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@ name: build
22

33
on:
44
workflow_dispatch:
5+
workflow_call:
6+
inputs:
7+
arguments:
8+
description: "build.sh に渡すコマンドライン引数"
9+
default: ''
10+
required: false
11+
type: string
512
push:
613
branches:
714
- master
@@ -14,13 +21,11 @@ jobs:
1421
build:
1522
runs-on: ubuntu-latest
1623
steps:
17-
- name: Register SSH key
18-
env:
19-
CPPREFJP_GITHUB_IO_SECRETS: ${{ secrets.CPPREFJP_GITHUB_IO_SECRETS }}
24+
- id: vars
2025
run: |
21-
mkdir -p $HOME/.ssh
22-
echo "$CPPREFJP_GITHUB_IO_SECRETS" > $HOME/.ssh/id_ed25519
23-
chmod 600 $HOME/.ssh/id_ed25519
26+
echo "base_repo=${{ startsWith(inputs.arguments, '--pull ') && github.event.pull_request.base.repo.full_name || github.event.repository.full_name }}" >> "$GITHUB_OUTPUT"
27+
echo "head_repo=${{ startsWith(inputs.arguments, '--pull ') && github.event.pull_request.head.repo.full_name || github.event.repository.full_name }}" >> "$GITHUB_OUTPUT"
28+
echo "head_ref=${{ startsWith(inputs.arguments, '--pull ') && github.event.pull_request.head.ref || github.ref }}" >> "$GITHUB_OUTPUT"
2429
2530
# site_generator
2631
- uses: actions/checkout@v4
@@ -38,19 +43,14 @@ jobs:
3843
- run: git submodule update -i
3944
working-directory: site_generator/kunai
4045

41-
# cpprefjp.github.io
42-
- uses: actions/checkout@v4
43-
with:
44-
repository: cpprefjp/cpprefjp.github.io
45-
path: site_generator/cpprefjp/cpprefjp.github.io
46-
4746
# site
4847
- uses: actions/checkout@v4
4948
with:
50-
repository: cpprefjp/site
51-
path: site_generator/cpprefjp/site
49+
repository: ${{ steps.vars.outputs.head_repo }}
5250
# atom 生成のために全履歴が必要
5351
fetch-depth: 0
52+
ref: ${{ steps.vars.outputs.head_ref }}
53+
path: site_generator/cpprefjp/site
5454
- run: git submodule update -i
5555
working-directory: site_generator/cpprefjp/site
5656

@@ -66,10 +66,61 @@ jobs:
6666
python-version: 3.11
6767
# 3.12でUndefined symbolエラーがでた
6868

69+
# build.sh - base の build.sh を使う必要がある。もし PR head の
70+
# build.sh を使うと、pull_request_target で呼び出された時に PR
71+
# head の build.sh に悪意のあるコードが埋め込まれていると秘密鍵な
72+
# ど盗まれてしまう。
73+
- name: Check out build.sh
74+
uses: actions/checkout@v4
75+
with:
76+
repository: ${{ steps.vars.outputs.base_repo }}
77+
ref: master
78+
sparse-checkout: .github
79+
path: .trusted
80+
81+
# Deploy 用
82+
- name: "(Deploy) Register SSH key"
83+
if: inputs.arguments == ''
84+
env:
85+
CPPREFJP_GITHUB_IO_SECRETS: ${{ secrets.CPPREFJP_GITHUB_IO_SECRETS }}
86+
run: |
87+
mkdir -p $HOME/.ssh
88+
echo "$CPPREFJP_GITHUB_IO_SECRETS" > $HOME/.ssh/id_ed25519
89+
chmod 600 $HOME/.ssh/id_ed25519
90+
91+
# Deploy 用
92+
- name: "(Deploy) Check out cpprefjp.github.io"
93+
if: inputs.arguments == ''
94+
uses: actions/checkout@v4
95+
with:
96+
repository: cpprefjp/cpprefjp.github.io
97+
path: site_generator/cpprefjp/cpprefjp.github.io
98+
99+
# Preview 用
100+
- name: "(Preview build) Check out gh-pages"
101+
if: startsWith(inputs.arguments, '--pull ')
102+
continue-on-error: true
103+
uses: actions/checkout@v4
104+
with:
105+
repository: ${{ github.event.pull_request.base.repo.full_name }}
106+
ref: gh-pages
107+
path: site_generator/cpprefjp/gh-pages
108+
69109
# あとはスクリプトで頑張る
70-
- run: ./cpprefjp/site/.github/workflows/script/build.sh
110+
- name: Run script build.sh
111+
run: ../.trusted/.github/workflows/script/build.sh ${{ inputs.arguments }}
71112
working-directory: site_generator
72113

114+
# Preview 用
115+
- name: "(Preview build) Publish result in gh-pages"
116+
if: startsWith(inputs.arguments, '--pull ')
117+
uses: peaceiris/actions-gh-pages@v4
118+
with:
119+
github_token: ${{ secrets.GITHUB_TOKEN }}
120+
publish_dir: ./site_generator/cpprefjp/gh-pages
121+
commit_message: |
122+
Preview PR ${{ github.event.number }}: ${{ github.event.pull_request.head.sha }} <=
123+
73124
concurrency:
74125
group: ${{ github.workflow }}-${{ github.ref }}
75126
cancel-in-progress: true

.github/workflows/check.yml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: check
22

3-
on: [push, pull_request, workflow_dispatch]
3+
on: [push, pull_request_target, workflow_dispatch]
44

55
jobs:
66
check:
@@ -25,8 +25,17 @@ jobs:
2525
python -m pip install --upgrade pip
2626
pip install requests
2727
- uses: actions/checkout@v4
28+
with:
29+
repository: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.repo.full_name || github.event.repository.full_name }}
30+
ref: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.head.ref || github.ref }}
31+
- uses: actions/checkout@v4
32+
with:
33+
repository: ${{ github.event_name == 'pull_request_target' && github.event.pull_request.base.repo.full_name || github.event.repository.full_name }}
34+
ref: ${{ github.event_name == 'pull_request_target' && 'master' || github.ref }}
35+
sparse-checkout: .github
36+
path: .trusted
2837
- name: check
29-
run: python3 .github/workflows/script/${{ matrix.item.script || format('{0}_check.py', matrix.item.name) }}
38+
run: python3 .trusted/.github/workflows/script/${{ matrix.item.script || format('{0}_check.py', matrix.item.name) }}
3039

3140
detect_forbidden_characters:
3241
# 本リポジトリでは、以下に挙げる文字の使用を禁止している:
@@ -60,3 +69,12 @@ jobs:
6069
path: ${{ env.REPO_DIR }}
6170
- name: check
6271
run: "! $BIN_DIR/rg -t md --vimgrep '[\u00ad\u200b]' $REPO_DIR"
72+
73+
preview_build:
74+
if: github.event_name == 'pull_request_target'
75+
needs: [check, detect_forbidden_characters]
76+
uses: ./.github/workflows/build.yml
77+
with:
78+
arguments: --pull ${{ github.event.number }}
79+
concurrency:
80+
group: cpprefjp.gh-pages.lock

.github/workflows/script/build.sh

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,32 @@ popd
3333
pip3 install -r docker/requirements.txt
3434
python3 run.py settings.cpprefjp --concurrency=`nproc`
3535

36-
# 生成されたサイトの中身を push
37-
pushd cpprefjp/cpprefjp.github.io
38-
# push するために ssh のリモートを追加する
39-
git remote add origin2 [email protected]:cpprefjp/cpprefjp.github.io.git
40-
41-
git add ./ --all
42-
git config --global user.email "[email protected]"
43-
git config --global user.name "cpprefjp-autoupdate"
44-
git commit -a -m "update automatically"
45-
git push origin2 master
46-
popd
36+
if (($# == 0)); then
37+
# 生成されたサイトの中身を push
38+
pushd cpprefjp/cpprefjp.github.io
39+
# push するために ssh のリモートを追加する
40+
git remote add origin2 [email protected]:cpprefjp/cpprefjp.github.io.git
41+
42+
git add ./ --all
43+
git config --global user.email "[email protected]"
44+
git config --global user.name "cpprefjp-autoupdate"
45+
git commit -a -m "update automatically"
46+
git push origin2 master
47+
popd
48+
49+
elif [[ $1 == --pull ]]; then
50+
if [[ ! $2 ]]; then
51+
printf '%s\n' "build.sh: プルリクエスト番号が指定されていません" >&2
52+
exit 2
53+
fi
54+
55+
target_directory=cpprefjp/gh-pages/gen/pull/$2
56+
rm -rf "$target_directory"
57+
mkdir -p "$target_directory"
58+
cp -r cpprefjp/cpprefjp.github.io/* "$target_directory"/
59+
60+
else
61+
printf '%s\n' "build.sh: コマンドライン引数が認識できません: $1" >&2
62+
exit 2
63+
64+
fi

0 commit comments

Comments
 (0)