|
| 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 }} |
0 commit comments