diff --git a/.github/workflows/tugboat-dispatch.yml b/.github/workflows/tugboat-dispatch.yml new file mode 100644 index 000000000..bdf56d4bc --- /dev/null +++ b/.github/workflows/tugboat-dispatch.yml @@ -0,0 +1,107 @@ +name: Dispatch Tugboat API Calls +on: + workflow_dispatch: + inputs: + action: + type: choice + description: 'Tugboat API action' + options: + - create + - rebuild + - delete + required: true + pr_number: + type: number + description: 'The pull request number to take action on' + required: true + pr_title: + type: string + description: 'The pull request title for creating previews, requied for creation' + required: false +jobs: + dispatch_tugboat_api: + runs-on: self-hosted + env: + NODE_EXTRA_CA_CERTS: /etc/ssl/certs/ca-certificates.crt + name: Dispatch Tugboat API Calls + steps: + # Setup + - name: Confirm Workflow Inputs + run: | + echo "Action: ${{ inputs.action }} PR Number: ${{ inputs.pr_number }} PR Title: ${{ inputs.pr_title }}" + + # Restore Preview ID for non-creation actions + - name: Restore Preview ID + if: ${{ inputs.action != 'create' }} + uses: actions/cache/restore@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 + with: + path: .tugboat_preview.txt + key: ${{ runner.os }}-tugboat-preview-id-pr-${{ inputs.pr_number }} + - name: Set Preview ID + if: ${{ inputs.action != 'create' }} + run: | + if ! [ -f .tugboat_preview.txt ]; then + echo "Preview ID not found, please contact platform-cms-qa on Github or CMS QA Engineers in #cms-support on Slack for assistance." + exit 1 + fi + PREVIEW_ID=$(cat .tugboat_preview.txt) + echo "Preview ID: ${PREVIEW_ID}" + echo "PREVIEW_ID=$PREVIEW_ID" >> $GITHUB_ENV + + # Make API calls + - name: Create Tugboat Preview + id: tugboat_pr_preview + if: ${{ inputs.action == 'create' }} + run: | + curl --fail \ + -H "Authorization: Bearer ${{ secrets.TUGBOAT_API_TOKEN }}" \ + -H "Content-Type: application/json" \ + -X POST \ + -d '{ "repo": "${{ secrets.TUGBOAT_REPOSITORY }}", "ref": "${{ inputs.pr_number }}", "name": "${{ inputs.pr_title }}", "type": "pullrequest" }' \ + -o .tugboat_response.json \ + https://api.tugboat.vfs.va.gov/v3/previews + - name: Rebuild Tugboat Preview + if: ${{ inputs.action == 'rebuild' }} + run: | + curl --fail \ + -H "Authorization: Bearer ${{ secrets.TUGBOAT_API_TOKEN }}" \ + -H "Content-Type: application/json" \ + -X POST \ + -d '{ "children": "false", "force": "false" }' \ + https://api.tugboat.vfs.va.gov/v3/previews/${{ env.PREVIEW_ID }}/rebuild + - name: Delete Tugboat Preview + if: ${{ inputs.action == 'delete' }} + run: | + curl --fail \ + -H "Authorization: Bearer ${{ secrets.TUGBOAT_API_TOKEN }}" \ + -H "Content-Type: application/json" \ + -X DELETE \ + -d '{ "force": "false" }' \ + https://api.tugboat.vfs.va.gov/v3/previews/${{ env.PREVIEW_ID }} + + # Store Preview ID in cache on creation + - name: Extract Preview ID + if: ${{ inputs.action == 'create' }} + run: jq -r .preview .tugboat_response.json > .tugboat_preview.txt + - name: Confirm Preview ID + if: ${{ inputs.action == 'create' }} + run: cat .tugboat_preview.txt + - name: Delete Previous Preview ID + continue-on-error: true + uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 + with: + script: | + await github.rest.actions.deleteActionsCacheByKey({ + owner: context.repo.owner, + repo: context.repo.repo, + key: `${{ runner.os }}-tugboat-preview-id-pr-${{ inputs.pr_number }}`, + }); + - name: Save Preview ID + uses: actions/cache/save@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 + with: + path: .tugboat_preview.txt + key: ${{ runner.os }}-tugboat-preview-id-pr-${{ inputs.pr_number }} + + # Cleanup + - name: Cleanup temporary file + run: rm .tugboat_preview.txt diff --git a/.github/workflows/tugboat-pr-closed.yml b/.github/workflows/tugboat-pr-closed.yml index 26501bbe2..31330d9af 100644 --- a/.github/workflows/tugboat-pr-closed.yml +++ b/.github/workflows/tugboat-pr-closed.yml @@ -8,32 +8,21 @@ on: jobs: tugboat_delete_preview: - runs-on: self-hosted - env: - NODE_EXTRA_CA_CERTS: /etc/ssl/certs/ca-certificates.crt + runs-on: ubuntu-latest name: Delete Tugboat Preview steps: - - name: Restore Preview ID - uses: actions/cache/restore@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 + - name: Dispatch Tugboat Preview Deletion + continue-on-error: true + uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 with: - path: .tugboat_preview.txt - key: ${{ runner.os }}-tugboat-preview-id-pr-${{ github.event.pull_request.number }} - - name: Set Preview ID - run: | - if ! [ -f .tugboat_preview.txt ]; then - echo "Preview ID not found, please manually delete Tugboat Preview. Contact platform-cms-qa on Github or CMS QA Engineers in #cms-support on Slack for assistance." - exit 1 - fi - PREVIEW_ID=$(cat .tugboat_preview.txt) - echo "Preview ID: ${PREVIEW_ID}" - echo "PREVIEW_ID=$PREVIEW_ID" >> $GITHUB_ENV - - name: Cleanup temporary file - run: rm .tugboat_preview.txt - - name: Delete Tugboat Preview - run: | - curl --fail \ - -H "Authorization: Bearer ${{ secrets.TUGBOAT_API_TOKEN }}" \ - -H "Content-Type: application/json" \ - -X DELETE \ - -d '{ "force": "false" }' \ - https://api.tugboat.vfs.va.gov/v3/previews/${{ env.PREVIEW_ID }} \ No newline at end of file + script: | + await github.rest.actions.createWorkflowDispatch({ + owner: context.repo.owner, + repo: context.repo.repo, + workflow_id: 'tugboat-dispatch.yml', + ref: 'main', + inputs: { + action: 'delete', + pr_number: ${{ github.event.pull_request.number }}, + } + }); diff --git a/.github/workflows/tugboat-pr-opened.yml b/.github/workflows/tugboat-pr-opened.yml index 1bc92b762..1872640e0 100644 --- a/.github/workflows/tugboat-pr-opened.yml +++ b/.github/workflows/tugboat-pr-opened.yml @@ -9,39 +9,22 @@ on: jobs: tugboat_create_preview: - runs-on: self-hosted - env: - NODE_EXTRA_CA_CERTS: /etc/ssl/certs/ca-certificates.crt + runs-on: ubuntu-latest name: Create Tugboat Preview steps: - - name: Create Tugboat Preview - id: tugboat_pr_preview - run: | - curl --fail \ - -H "Authorization: Bearer ${{ secrets.TUGBOAT_API_TOKEN }}" \ - -H "Content-Type: application/json" \ - -X POST \ - -d '{ "repo": "${{ secrets.TUGBOAT_REPOSITORY }}", "ref": "${{ github.event.pull_request.number }}", "name": "${{ github.event.pull_request.title }}", "type": "pullrequest" }' \ - -o .tugboat_response.json \ - https://api.tugboat.vfs.va.gov/v3/previews - - name: Diagnostics - run: cat .tugboat_response.json - - name: Extract Preview ID - run: jq -r .preview .tugboat_response.json > .tugboat_preview.txt - - name: Delete Previous Preview ID + - name: Dispatch Tugboat Preview Creation continue-on-error: true uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 with: script: | - await github.rest.actions.deleteActionsCacheByKey({ + await github.rest.actions.createWorkflowDispatch({ owner: context.repo.owner, repo: context.repo.repo, - key: `${{ runner.os }}-tugboat-preview-id-pr-${{ github.event.pull_request.number }}`, + workflow_id: 'tugboat-dispatch.yml', + ref: 'main', + inputs: { + action: 'create', + pr_number: ${{ github.event.pull_request.number }}, + pr_title: '${{ github.event.pull_request.title }}', + } }); - - name: Save Preview ID - uses: actions/cache/save@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 - with: - path: .tugboat_preview.txt - key: ${{ runner.os }}-tugboat-preview-id-pr-${{ github.event.pull_request.number }} - - name: Cleanup temporary file - run: rm .tugboat_preview.txt \ No newline at end of file diff --git a/.github/workflows/tugboat-pr-updated.yml b/.github/workflows/tugboat-pr-updated.yml index 1e0adb446..eaea0e881 100644 --- a/.github/workflows/tugboat-pr-updated.yml +++ b/.github/workflows/tugboat-pr-updated.yml @@ -8,32 +8,21 @@ on: jobs: tugboat_rebuild_preview: - runs-on: self-hosted - env: - NODE_EXTRA_CA_CERTS: /etc/ssl/certs/ca-certificates.crt + runs-on: ubuntu-latest name: Rebuild Tugboat Preview steps: - - name: Restore Preview ID - uses: actions/cache/restore@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 + - name: Dispatch Tugboat Preview Rebuild + continue-on-error: true + uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 with: - path: .tugboat_preview.txt - key: ${{ runner.os }}-tugboat-preview-id-pr-${{ github.event.pull_request.number }} - - name: Set Preview ID - run: | - if ! [ -f .tugboat_preview.txt ]; then - echo "Preview ID not found, please manually rebuild Tugboat Preview. Contact platform-cms-qa on Github or CMS QA Engineers in #cms-support on Slack for assistance." - exit 1 - fi - PREVIEW_ID=$(cat .tugboat_preview.txt) - echo "Preview ID: ${PREVIEW_ID}" - echo "PREVIEW_ID=$PREVIEW_ID" >> $GITHUB_ENV - - name: Cleanup temporary file - run: rm .tugboat_preview.txt - - name: Rebuild Tugboat Preview - run: | - curl --fail \ - -H "Authorization: Bearer ${{ secrets.TUGBOAT_API_TOKEN }}" \ - -H "Content-Type: application/json" \ - -X POST \ - -d '{ "children": "false", "force": "false" }' \ - https://api.tugboat.vfs.va.gov/v3/previews/${{ env.PREVIEW_ID }}/rebuild \ No newline at end of file + script: | + await github.rest.actions.createWorkflowDispatch({ + owner: context.repo.owner, + repo: context.repo.repo, + workflow_id: 'tugboat-dispatch.yml', + ref: 'main', + inputs: { + action: 'rebuild', + pr_number: ${{ github.event.pull_request.number }}, + } + }); diff --git a/.github/workflows/tugboat-refresh-cache-responder.yml b/.github/workflows/tugboat-refresh-cache-responder.yml deleted file mode 100644 index 9186df928..000000000 --- a/.github/workflows/tugboat-refresh-cache-responder.yml +++ /dev/null @@ -1,29 +0,0 @@ - - -name: Refresh Tugboat Preview ID Cache -on: - pull_request: - types: [ labeled ] -jobs: - refresh_cache: - name: Refresh Tugboat Preview ID Cache - runs-on: ubuntu-latest - if: ${{ github.event.label.name == 'refresh-tugboat-cache' }} - steps: - - name: Refresh Preview ID - uses: actions/cache/restore@704facf57e6136b1bc63b828d79edcd491f0ee84 # v3.3.2 - with: - path: .tugboat_preview.txt - key: ${{ runner.os }}-tugboat-preview-id-pr-${{ github.event.pull_request.number }} - - name: Cleanup temporary file - run: rm .tugboat_preview.txt - - name: Remove refresh label - uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 - with: - script: | - await github.rest.issues.removeLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: ${{ github.event.pull_request.number }}, - name: 'refresh-tugboat-cache', - }); diff --git a/.github/workflows/tugboat-refresh-cache-dispatch.yml b/.github/workflows/tugboat-refresh-cache.yml similarity index 58% rename from .github/workflows/tugboat-refresh-cache-dispatch.yml rename to .github/workflows/tugboat-refresh-cache.yml index efba64d67..5c790cb23 100644 --- a/.github/workflows/tugboat-refresh-cache-dispatch.yml +++ b/.github/workflows/tugboat-refresh-cache.yml @@ -5,14 +5,16 @@ on: - cron: '0 */6 * * *' jobs: # Collects the cache keys that need to be refreshed - dispatch_cache_keys: - name: Dispatch Tugboat Preview ID cache keys that need to be refreshed + collect_cache_keys: + name: Collect Tugboat Preview ID cache keys that need to be refreshed + outputs: + matrix: ${{ steps.cache-keys.outputs.result }} runs-on: ubuntu-latest steps: - name: Cross reference open PRs against cache keys in repo + id: cache-keys uses: actions/github-script@d7906e4ad0b1822421a7e6a35d5ca353c962f410 # v6.4.1 with: - github-token: ${{ secrets.LABEL_API_TOKEN }} script: | const prs = await github.paginate( github.rest.pulls.list, @@ -23,11 +25,9 @@ jobs: }, (response) => response.data.map((pr) => pr.number) ) - for (const pr of prs) { console.log(`PR: ${pr}`) } - const cacheKeys = await github.paginate( github.rest.actions.getActionsCacheList, { @@ -36,19 +36,34 @@ jobs: }, (response) => response.data.map((cache) => cache.key) ) - for (const key of cacheKeys) { console.log(`Key: ${key}`) } - + const toRefresh = [] for (const pr of prs) { if (cacheKeys.includes(`${{ runner.os }}-tugboat-preview-id-pr-${pr}`)) { console.log(`Need to refresh: ${pr}`) - await github.rest.issues.addLabels({ - owner: context.repo.owner, - repo: context.repo.repo, - issue_number: `${pr}`, - labels: ['refresh-tugboat-cache'], - }); + toRefresh.push(pr) } } + const result = JSON.stringify(toRefresh) + console.log(`Refresh Keys: ${result}`) + return result + result-encoding: string + + # Refresh cache for given keys + refresh_cache: + name: Refresh cache for given keys + needs: [ collect_cache_keys ] + runs-on: ubuntu-latest + strategy: + matrix: + value: ${{fromJSON(needs.collect_cache_keys.outputs.matrix)}} + steps: + - name: Refresh Preview ID + uses: actions/cache/restore@88522ab9f39a2ea568f7027eddc7d8d8bc9d59c8 # v3.3.1 + with: + path: .tugboat_preview.txt + key: ${{ runner.os }}-tugboat-preview-id-pr-${{ matrix.value }} + - name: Cleanup temporary file + run: rm .tugboat_preview.txt