|
| 1 | +name: "03 Maintain: Apply Package Cache" |
| 2 | +description: "Generate the package cache for the lesson after a pull request has been merged or via manual trigger, and cache in S3 or GitHub" |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + name: |
| 7 | + description: 'Who triggered this build?' |
| 8 | + required: true |
| 9 | + default: 'Maintainer (via GitHub)' |
| 10 | + pull_request: |
| 11 | + types: |
| 12 | + - closed |
| 13 | + branches: |
| 14 | + - main |
| 15 | + |
| 16 | +# queue cache runs |
| 17 | +concurrency: |
| 18 | + group: docker-apply-cache |
| 19 | + cancel-in-progress: false |
| 20 | + |
| 21 | +jobs: |
| 22 | + preflight: |
| 23 | + name: "Preflight: PR or Manual Trigger?" |
| 24 | + runs-on: ubuntu-latest |
| 25 | + outputs: |
| 26 | + do-apply: ${{ steps.check.outputs.merged_or_manual }} |
| 27 | + steps: |
| 28 | + - name: "Should we run cache application?" |
| 29 | + id: check |
| 30 | + run: | |
| 31 | + if [[ "${{ github.event_name }}" == "workflow_dispatch" || |
| 32 | + ("${{ github.ref }}" == "refs/heads/main" && "${{ github.event.action }}" == "closed" && "${{ github.event.pull_request.merged }}" == "true") ]]; then |
| 33 | + echo "merged_or_manual=true" >> $GITHUB_OUTPUT |
| 34 | + else |
| 35 | + echo "This was not a manual trigger and no PR was merged. No action taken." |
| 36 | + echo "merged_or_manual=false" >> $GITHUB_OUTPUT |
| 37 | + fi |
| 38 | + shell: bash |
| 39 | + |
| 40 | + check-renv: |
| 41 | + name: "Check If We Need {renv}" |
| 42 | + runs-on: ubuntu-latest |
| 43 | + needs: preflight |
| 44 | + if: needs.preflight.outputs.do-apply == 'true' |
| 45 | + permissions: |
| 46 | + id-token: write |
| 47 | + outputs: |
| 48 | + renv-needed: ${{ steps.check-for-renv.outputs.renv-needed }} |
| 49 | + renv-cache-hashsum: ${{ steps.check-for-renv.outputs.renv-cache-hashsum }} |
| 50 | + renv-cache-available: ${{ steps.check-for-renv.outputs.renv-cache-available }} |
| 51 | + steps: |
| 52 | + - name: "Check for renv" |
| 53 | + id: check-for-renv |
| 54 | + uses: carpentries/actions/renv-checks@main |
| 55 | + with: |
| 56 | + role-to-assume: ${{ secrets.AWS_GH_OIDC_ARN }} |
| 57 | + aws-region: ${{ secrets.AWS_GH_OIDC_REGION }} |
| 58 | + WORKBENCH_TAG: ${{ vars.WORKBENCH_TAG || 'latest' }} |
| 59 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 60 | + |
| 61 | + no-renv-cache-used: |
| 62 | + name: "No renv cache used" |
| 63 | + runs-on: ubuntu-latest |
| 64 | + needs: check-renv |
| 65 | + if: needs.check-renv.outputs.renv-needed != 'true' |
| 66 | + steps: |
| 67 | + - name: "No renv cache needed" |
| 68 | + run: echo "No renv cache needed for this lesson" |
| 69 | + |
| 70 | + renv-cache-available: |
| 71 | + name: "renv cache available" |
| 72 | + runs-on: ubuntu-latest |
| 73 | + needs: check-renv |
| 74 | + if: needs.check-renv.outputs.renv-cache-available == 'true' |
| 75 | + steps: |
| 76 | + - name: "renv cache available" |
| 77 | + run: echo "renv cache available for this lesson" |
| 78 | + |
| 79 | + update-renv-cache: |
| 80 | + name: "Update renv Cache" |
| 81 | + runs-on: ubuntu-latest |
| 82 | + needs: check-renv |
| 83 | + if: | |
| 84 | + needs.check-renv.outputs.renv-needed == 'true' && |
| 85 | + needs.check-renv.outputs.renv-cache-available != 'true' && |
| 86 | + ( |
| 87 | + github.event_name == 'workflow_dispatch' || |
| 88 | + ( |
| 89 | + github.event.pull_request.merged == true && |
| 90 | + ( |
| 91 | + ( |
| 92 | + contains( |
| 93 | + join(github.event.pull_request.labels.*.name, ','), |
| 94 | + 'type: package cache' |
| 95 | + ) && |
| 96 | + github.event.pull_request.head.ref == 'update/packages' |
| 97 | + ) |
| 98 | + || |
| 99 | + ( |
| 100 | + contains( |
| 101 | + join(github.event.pull_request.labels.*.name, ','), |
| 102 | + 'type: workflows' |
| 103 | + ) && |
| 104 | + github.event.pull_request.head.ref == 'update/workflows' |
| 105 | + ) |
| 106 | + || |
| 107 | + ( |
| 108 | + contains( |
| 109 | + join(github.event.pull_request.labels.*.name, ','), |
| 110 | + 'type: docker version' |
| 111 | + ) && |
| 112 | + github.event.pull_request.head.ref == 'update/workbench-docker-version' |
| 113 | + ) |
| 114 | + ) |
| 115 | + ) |
| 116 | + ) |
| 117 | + permissions: |
| 118 | + checks: write |
| 119 | + contents: write |
| 120 | + pages: write |
| 121 | + id-token: write |
| 122 | + container: |
| 123 | + image: ghcr.io/carpentries/workbench-docker:${{ vars.WORKBENCH_TAG || 'latest' }} |
| 124 | + env: |
| 125 | + WORKBENCH_PROFILE: "ci" |
| 126 | + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} |
| 127 | + RENV_PATHS_ROOT: /home/rstudio/lesson/renv |
| 128 | + RENV_PROFILE: "lesson-requirements" |
| 129 | + RENV_VERSION: ${{ needs.check-renv.outputs.renv-cache-hashsum }} |
| 130 | + RENV_CONFIG_EXTERNAL_LIBRARIES: "/usr/local/lib/R/site-library" |
| 131 | + volumes: |
| 132 | + - ${{ github.workspace }}:/home/rstudio/lesson |
| 133 | + options: --cpus 2 |
| 134 | + steps: |
| 135 | + - uses: actions/checkout@v4 |
| 136 | + |
| 137 | + - name: "Debugging Info" |
| 138 | + run: | |
| 139 | + echo "Current Directory: $(pwd)" |
| 140 | + ls -lah /home/rstudio/.workbench |
| 141 | + ls -lah $(pwd) |
| 142 | + Rscript -e 'sessionInfo()' |
| 143 | + shell: bash |
| 144 | + |
| 145 | + - name: "Mark Repository as Safe" |
| 146 | + run: | |
| 147 | + git config --global --add safe.directory $(pwd) |
| 148 | + shell: bash |
| 149 | + |
| 150 | + - name: "Ensure sandpaper is loadable" |
| 151 | + run: | |
| 152 | + .libPaths() |
| 153 | + library(sandpaper) |
| 154 | + shell: Rscript {0} |
| 155 | + |
| 156 | + - name: "Setup Lesson Dependencies" |
| 157 | + run: | |
| 158 | + Rscript /home/rstudio/.workbench/setup_lesson_deps.R |
| 159 | + shell: bash |
| 160 | + |
| 161 | + - name: "Fortify renv Cache" |
| 162 | + run: | |
| 163 | + Rscript /home/rstudio/.workbench/fortify_renv_cache.R |
| 164 | + shell: bash |
| 165 | + |
| 166 | + - name: "Get Container Version Used" |
| 167 | + id: wb-vers |
| 168 | + uses: carpentries/actions/container-version@main |
| 169 | + with: |
| 170 | + WORKBENCH_TAG: ${{ vars.WORKBENCH_TAG }} |
| 171 | + renv-needed: ${{ needs.check-renv.outputs.renv-needed }} |
| 172 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 173 | + |
| 174 | + - name: "Validate Current Org and Workflow" |
| 175 | + id: validate-org-workflow |
| 176 | + uses: carpentries/actions/validate-org-workflow@main |
| 177 | + with: |
| 178 | + repo: ${{ github.repository }} |
| 179 | + workflow: ${{ github.workflow }} |
| 180 | + |
| 181 | + - name: "Configure AWS credentials via OIDC" |
| 182 | + id: aws-creds |
| 183 | + env: |
| 184 | + role-to-assume: ${{ secrets.AWS_GH_OIDC_ARN }} |
| 185 | + aws-region: ${{ secrets.AWS_GH_OIDC_REGION }} |
| 186 | + if: | |
| 187 | + steps.validate-org-workflow.outputs.is_valid == 'true' && |
| 188 | + env.role-to-assume != '' && |
| 189 | + env.aws-region != '' |
| 190 | + uses: aws-actions/configure-aws-credentials@v5.0.0 |
| 191 | + with: |
| 192 | + role-to-assume: ${{ env.role-to-assume }} |
| 193 | + aws-region: ${{ env.aws-region }} |
| 194 | + output-credentials: true |
| 195 | + |
| 196 | + - name: "Upload cache object to S3" |
| 197 | + id: upload-cache |
| 198 | + uses: carpentries/actions-cache@frog-matchedkey-1 |
| 199 | + with: |
| 200 | + accessKey: ${{ steps.aws-creds.outputs.aws-access-key-id }} |
| 201 | + secretKey: ${{ steps.aws-creds.outputs.aws-secret-access-key }} |
| 202 | + sessionToken: ${{ steps.aws-creds.outputs.aws-session-token }} |
| 203 | + bucket: workbench-docker-caches |
| 204 | + path: | |
| 205 | + /home/rstudio/lesson/renv |
| 206 | + /usr/local/lib/R/site-library |
| 207 | + key: ${{ github.repository }}/${{ steps.wb-vers.outputs.container-version }}_renv-${{ needs.check-renv.outputs.renv-cache-hashsum }} |
| 208 | + restore-keys: |
| 209 | + ${{ github.repository }}/${{ steps.wb-vers.outputs.container-version }}_renv- |
| 210 | + |
| 211 | + trigger-build-deploy: |
| 212 | + name: "Trigger Build and Deploy Workflow" |
| 213 | + runs-on: ubuntu-latest |
| 214 | + needs: update-renv-cache |
| 215 | + if: | |
| 216 | + needs.update-renv-cache.result == 'success' || |
| 217 | + needs.check-renv.outputs.renv-cache-available == 'true' |
| 218 | + steps: |
| 219 | + - uses: actions/checkout@v4 |
| 220 | + |
| 221 | + - name: "Trigger Build and Deploy Workflow" |
| 222 | + env: |
| 223 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 224 | + run: | |
| 225 | + gh workflow run docker_build_deploy.yaml --ref main |
| 226 | + shell: bash |
| 227 | + continue-on-error: true |
0 commit comments