Skip to content

Commit

Permalink
Dispatch Tugboat API calls on main branch (#1483)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunTaoLuo authored Jan 25, 2024
1 parent 33d2e02 commit 642775f
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 121 deletions.
107 changes: 107 additions & 0 deletions .github/workflows/tugboat-dispatch.yml
Original file line number Diff line number Diff line change
@@ -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
41 changes: 15 additions & 26 deletions .github/workflows/tugboat-pr-closed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
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 }},
}
});
37 changes: 10 additions & 27 deletions .github/workflows/tugboat-pr-opened.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
41 changes: 15 additions & 26 deletions .github/workflows/tugboat-pr-updated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 }},
}
});
29 changes: 0 additions & 29 deletions .github/workflows/tugboat-refresh-cache-responder.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
{
Expand All @@ -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

0 comments on commit 642775f

Please sign in to comment.