-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
968988c
commit 72c841e
Showing
2 changed files
with
97 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
async function getDeployData({ event, eventName }) { | ||
let ref = '' | ||
|
||
switch (eventName) { | ||
case 'pull_request': { | ||
ref = event.pull_request.head.ref | ||
break | ||
} | ||
|
||
default: { | ||
ref = event.ref.replace('refs/heads/', '') | ||
break | ||
} | ||
} | ||
|
||
ref = ref.replaceAll('/', '-').slice(0, 25) | ||
|
||
const isPr = eventName === 'pull_request' | ||
const isPreview = | ||
event.labels?.some(label => label.name === 'preview') ?? false | ||
|
||
return JSON.stringify({ | ||
ref, | ||
url: `https://${ref}.cryoet.dev.si.czi.technology`, | ||
isPush: eventName === 'push', | ||
isDelete: eventName === 'delete', | ||
isLabeled: isPr && event.action === 'labeled', | ||
isUnlabeled: isPr && event.action === 'unlabeled', | ||
isSync: isPr && event.action === 'synchronize' && isPreview, | ||
isClosed: isPr && event.action === 'closed' && isPreview, | ||
}) | ||
} | ||
|
||
module.exports = getDeployData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ on: | |
delete: | ||
branches: | ||
- 'dev-*' | ||
pull_request: | ||
types: [labeled, unlabeled, synchronize, closed] | ||
|
||
# https://docs.github.com/en/actions/deployment/security-hardening-your-deployments/configuring-openid-connect-in-amazon-web-services | ||
permissions: | ||
|
@@ -18,25 +20,53 @@ jobs: | |
name: deploy dev branch | ||
runs-on: ubuntu-latest | ||
environment: dev | ||
if: github.repository == 'chanzuckerberg/cryoet-data-portal' | ||
if: | | ||
github.repository == 'chanzuckerberg/cryoet-data-portal' && ( | ||
github.event_name != 'pull_request' || | ||
( | ||
startsWith(github.head_ref, 'dev-') != true && | ||
( | ||
contains(github.event.pull_request.labels.*.name, 'preview') || | ||
( | ||
github.event.action == 'unlabeled' && | ||
github.event.label.name == 'preview' | ||
) | ||
) | ||
) | ||
) | ||
steps: | ||
- id: deploy-data | ||
name: get stack name | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const getDeployData = require('./.github/get-deploy-data') | ||
return getDeployData({ | ||
event: ${{ toJson(github.event) }}, | ||
eventName: '${{ github.event_name }}', | ||
}) | ||
- name: check deploy data | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
const data = ${{ fromJson(steps.deploy-data.outputs.result) }} | ||
console.log('breh', JSON.stringify(data, null, 2)) | ||
- name: Configure AWS Credentials | ||
uses: aws-actions/configure-aws-credentials@v4 | ||
with: | ||
mask-aws-account-id: true | ||
aws-region: ${{ secrets.AWS_REGION }} | ||
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }} | ||
role-duration-seconds: 1200 | ||
- name: Setting up stack name | ||
run: | | ||
if [[ ${#GITHUB_REF_NAME} -gt 25 ]]; then | ||
echo "branch name: ${GITHUB_REF_NAME}, length ${#GITHUB_REF_NAME}, is $((${#GITHUB_REF_NAME} - 25)) characters too long, please use a branch name that's 25 characters or shorter" | ||
exit 1 | ||
else | ||
echo $GITHUB_REF_NAME | tr '[:upper:]' '[:lower:]' | xargs -I {} -n 1 echo STACK_NAME={} >> $GITHUB_ENV | ||
fi | ||
|
||
- name: Create or update dev stack | ||
if: ${{ github.event_name == 'push' }} | ||
if: | | ||
fromJson(steps.deploy-data.outputs.result).isPush || | ||
fromJson(steps.deploy-data.outputs.result).isLabeled || | ||
fromJson(steps.deploy-data.outputs.result).isSync | ||
uses: chanzuckerberg/github-actions/.github/actions/[email protected] | ||
env: | ||
# Force using BuildKit instead of normal Docker, required so that metadata | ||
|
@@ -46,31 +76,45 @@ jobs: | |
DOCKER_REPO: ${{ secrets.ECR_REPO }}/ | ||
ENV: dev | ||
with: | ||
stack-name: ${{ env.STACK_NAME }} | ||
stack-name: ${{ fromJson(steps.deploy-data.outputs.result).ref }} | ||
create-tag: true | ||
tfe-token: ${{ secrets.TFE_TOKEN }} | ||
working-directory: ./frontend | ||
env: dev | ||
operation: create-or-update | ||
|
||
- name: Annotate workflow run with frontend URL | ||
if: ${{ github.event_name == 'push' }} && success() | ||
if: | | ||
fromJson(steps.deploy-data.outputs.result).isPush || | ||
fromJson(steps.deploy-data.outputs.result).isLabeled || | ||
fromJson(steps.deploy-data.outputs.result).isSync | ||
run: | | ||
echo "# Frontend URL:" >> $GITHUB_STEP_SUMMARY | ||
echo >> $GITHUB_STEP_SUMMARY | ||
echo "https://${{ env.STACK_NAME }}.cryoet.dev.si.czi.technology" >> $GITHUB_STEP_SUMMARY | ||
echo "https://${{ steps.deploy-data.outputs.result }}.cryoet.dev.si.czi.technology" >> $GITHUB_STEP_SUMMARY | ||
- name: Post comment on PR | ||
if: fromJson(steps.deploy-data.outputs.result).isLabeled | ||
uses: actions/github-script@v7 | ||
with: | ||
script: | | ||
github.rest.issues.createComment({ | ||
issue_number: context.issue.number, | ||
owner: context.repo.owner, | ||
repo: context.repo.repo, | ||
body: '# Frontend Preview URL\n\n${{ fromJson(steps.deploy-data.outputs.result).url }}' | ||
}) | ||
- name: Delete dev set up stack name | ||
if: ${{ github.event_name == 'delete' && startsWith(github.event.ref, 'dev-') }} | ||
run: | | ||
echo ${{ github.event.ref }} | tr '[:upper:]' '[:lower:]' | xargs -I {} -n 1 echo DELETE_STACK_NAME={} >> $GITHUB_ENV | ||
- name: Delete dev | ||
if: ${{ github.event_name == 'delete' && startsWith(github.event.ref, 'dev-') }} | ||
if: | | ||
fromJson(steps.deploy-data.outputs.result).isDelete || | ||
fromJson(steps.deploy-data.outputs.result).isUnlabeled || | ||
fromJson(steps.deploy-data.outputs.result).isClosed | ||
uses: chanzuckerberg/github-actions/.github/actions/[email protected] | ||
env: | ||
ENV: dev | ||
with: | ||
stack-name: ${{ env.DELETE_STACK_NAME }} | ||
stack-name: ${{ fromJson(steps.deploy-data.outputs.result).ref }} | ||
operation: delete | ||
tfe-token: ${{ secrets.TFE_TOKEN }} | ||
env: dev | ||
|