Skip to content

Commit 8ec2bab

Browse files
authored
Merge pull request #129 from Public-Health-Scotland/build-readme
Add GitHub Actions workflow to build README
2 parents 952cc6c + 298ab81 commit 8ec2bab

2 files changed

Lines changed: 135 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
2+
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
3+
on:
4+
workflow_call:
5+
6+
name: phs_build_readme.yaml
7+
8+
permissions:
9+
contents: read
10+
pull-requests: read
11+
12+
jobs:
13+
phs_build_readme:
14+
name: "Build README"
15+
runs-on: ubuntu-latest
16+
container:
17+
image: rocker/r-ver
18+
19+
permissions:
20+
contents: write
21+
pull-requests: write
22+
23+
env:
24+
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
25+
26+
steps:
27+
- name: Install git
28+
run: |
29+
apt-get update
30+
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends git
31+
32+
- name: Mark workspace as safe for Git
33+
run: git config --global --add safe.directory "$GITHUB_WORKSPACE"
34+
35+
- name: Checkout repo
36+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
37+
with:
38+
fetch-depth: 0
39+
ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref_name }}
40+
41+
- name: Determine whether README needs rebuilding
42+
id: readme-check
43+
shell: bash
44+
run: |
45+
set -euo pipefail
46+
47+
if [ -f "README.Rmd" ]; then
48+
readme_source="README.Rmd"
49+
elif [ -f "README.rmd" ]; then
50+
readme_source="README.rmd"
51+
else
52+
echo "build=false" >> "$GITHUB_OUTPUT"
53+
exit 0
54+
fi
55+
56+
# Always rebuild on push / workflow_dispatch / anything that is not a PR.
57+
if [ "${{ github.event_name }}" != "pull_request" ]; then
58+
echo "build=true" >> "$GITHUB_OUTPUT"
59+
exit 0
60+
fi
61+
62+
# On PRs, rebuild if README.md is missing.
63+
if [ ! -f "README.md" ]; then
64+
echo "build=true" >> "$GITHUB_OUTPUT"
65+
exit 0
66+
fi
67+
68+
# On PRs, rebuild only if the README source changed.
69+
base_ref="${{ github.event.pull_request.base.ref }}"
70+
git fetch origin "$base_ref" --depth=1
71+
72+
changed_files="$(git diff --name-only "origin/$base_ref"...HEAD)"
73+
74+
if echo "$changed_files" | grep -Fxq "$readme_source"; then
75+
echo "build=true" >> "$GITHUB_OUTPUT"
76+
else
77+
echo "build=false" >> "$GITHUB_OUTPUT"
78+
fi
79+
80+
81+
- name: Setup Pandoc
82+
if: ${{ steps.readme-check.outputs.build == 'true' }}
83+
uses: r-lib/actions/setup-pandoc@a51a8012b0aab7c32ef9d19bf54da93f3254335e # v2.12.0
84+
85+
- name: Install dependencies
86+
if: ${{ steps.readme-check.outputs.build == 'true' }}
87+
uses: r-lib/actions/setup-r-dependencies@a51a8012b0aab7c32ef9d19bf54da93f3254335e # v2.12.0
88+
with:
89+
extra-packages: |
90+
any::devtools
91+
needs: website
92+
93+
- name: Build README
94+
if: ${{ steps.readme-check.outputs.build == 'true' }}
95+
run: devtools::build_readme()
96+
shell: Rscript {0}
97+
98+
- name: Commit and push changes
99+
if: ${{ steps.readme-check.outputs.build == 'true' }}
100+
id: commit_step_readme
101+
uses: stefanzweifel/git-auto-commit-action@04702edda442b2e678b25b537cec683a1493fcb9 # v7.1.0
102+
with:
103+
commit_message: "Build README (GHA)"
104+
commit_user_name: ${{ github.actor }}
105+
commit_user_email: ${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com
106+
continue-on-error: true
107+
108+
- name: Create Pull Request
109+
if: ${{ steps.readme-check.outputs.build == 'true' && steps.commit_step_readme.outputs.changes_detected == 'true' && steps.commit_step_readme.outcome == 'failure' }}
110+
uses: peter-evans/create-pull-request@5f6978faf089d4d20b00c7766989d076bb2fc7f1 # v8.1.1
111+
with:
112+
commit-message: "Build README (GHA)"
113+
branch: auto-build-readme
114+
delete-branch: true
115+
title: "Build README (GHA)"
116+
labels: maintenance
117+
base: ${{ github.event.pull_request.base.ref }}
118+
body: |
119+
This Pull Request was automatically created to rebuild README.md from README.Rmd.
120+
Please review and merge this PR.
121+
assignees: ${{ github.actor }}
122+
reviewers: ${{ github.actor }}

.github/workflows/phs_package_checks.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ jobs:
4444
concurrency:
4545
group: ${{ github.workflow }}-${{ github.ref }}
4646
cancel-in-progress: true
47+
Build_README:
48+
needs: [Style_and_document]
49+
uses: ./.github/workflows/phs_build_readme.yaml
50+
permissions:
51+
contents: write
52+
pull-requests: write
53+
secrets: inherit
54+
concurrency:
55+
group: ${{ github.workflow }}-build-readme-${{ github.ref }}
56+
cancel-in-progress: true
4757
R-CMD-check-core:
4858
needs: [Style_and_document]
4959
uses: ./.github/workflows/phs_R-CMD-check-core.yaml
@@ -75,6 +85,7 @@ jobs:
7585
pkgdown:
7686
needs:
7787
- Style_and_document
88+
- Build_README
7889
- R-CMD-check-core
7990
- R-CMD-check-extended
8091
- test-coverage
@@ -87,6 +98,7 @@ jobs:
8798
finalize:
8899
needs:
89100
- Style_and_document
101+
- Build_README
90102
- R-CMD-check-core
91103
- R-CMD-check-extended
92104
- test-coverage
@@ -101,6 +113,7 @@ jobs:
101113
with:
102114
script: |
103115
const allSuccess = ${{ needs.Style_and_document.result == 'success' &&
116+
needs.Build_README.result == 'success' &&
104117
needs.R-CMD-check-core.result == 'success' &&
105118
(needs.R-CMD-check-extended.result == 'success' || needs.R-CMD-check-extended.result == 'skipped') &&
106119
needs.test-coverage.result == 'success' &&

0 commit comments

Comments
 (0)