Skip to content

Build and Deploy EMIT #447

Build and Deploy EMIT

Build and Deploy EMIT #447

Workflow file for this run

name: Build and Deploy EMIT
on:
schedule:
- cron: '15 17 * * *' # Every day at 12:15 CT (5:15 PM UTC)
workflow_dispatch:
push:
branches: [ main, production ]
paths:
- emit-ch4plume-v1/**
jobs:
define-environment:
name: Set environment
runs-on: ubuntu-latest
outputs:
env_name: ${{ steps.define_environment.outputs.env_name }}
target_branch: ${{ steps.define_environment.outputs.target_branch }}
steps:
- name: Set the environment based on the branch
id: define_environment
run: |
if [ "${{ github.ref }}" = "refs/heads/production" ] || [ "${{ github.event_name }}" = "schedule" ]; then
echo "target_branch=production" >> $GITHUB_OUTPUT
echo "env_name=production" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "target_branch=main" >> $GITHUB_OUTPUT
echo "env_name=staging" >> $GITHUB_OUTPUT
fi
- name: Print the environment
run: echo "The environment is ${{ steps.define_environment.outputs.env_name }}"
build:
runs-on: ubuntu-latest
needs: define-environment
environment: ${{ needs.define-environment.outputs.env_name }}
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
with:
sparse-checkout: 'emit-ch4plume-v1/'
sparse-checkout-cone-mode: false
ref: ${{ needs.define-environment.outputs.target_branch }}
- uses: actions/setup-node@v3
with:
node-version: 'v20.9.0'
- name: Update Data 📊
working-directory: ./emit-ch4plume-v1
run: |
GEOAPIFY_APIKEY="${{ secrets.GEOAPIFY_APIKEY }}" node update_data.js
- name: Validate required variables and secrets
run: |
missing_vars=()
# Check for required variables
[ -z "${{ vars.PUBLIC_URL_EMIT }}" ] && missing_vars+=("vars.PUBLIC_URL_EMIT")
[ -z "${{ vars.MAP_STYLE }}" ] && missing_vars+=("vars.MAP_STYLE")
[ -z "${{ secrets.MAP_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.MAP_ACCESS_TOKEN")
[ -z "${{ secrets.DEPLOYMENT_ROLE_ARN }}" ] && missing_vars+=("secrets.DEPLOYMENT_ROLE_ARN")
[ -z "${{ secrets.CF_DISTRIBUTION_ID }}" ] && missing_vars+=("secrets.CF_DISTRIBUTION_ID")
# If any variables are missing, print them and exit with an error
if [ ${#missing_vars[@]} -ne 0 ]; then
echo "Error: The following required variables are missing:"
printf '%s\n' "${missing_vars[@]}"
exit 1
fi
shell: bash
- name: Build for production 🔧
working-directory: ./emit-ch4plume-v1
run: |
echo PUBLIC_URL="${{ vars.PUBLIC_URL_EMIT }}" >> .env
echo MAP_STYLE="${{ vars.MAP_STYLE }}" >> .env
echo MAP_ACCESS_TOKEN="${{ secrets.MAP_ACCESS_TOKEN }}" >> .env
echo PUBLIC_URL="${{ vars.PUBLIC_URL_EMIT }}" >> .env
echo STAGE="production" >> .env
npm install
npm run deploy
- name: Upload dist folder
uses: actions/upload-artifact@v4
with:
name: dist_folder
path: ./emit-ch4plume-v1/dist
deploy-staging:
needs:
- build
- define-environment
if: needs.define-environment.outputs.target_branch == 'main'
environment: ${{ needs.define-environment.outputs.env_name }}
permissions:
id-token: write
contents: read
runs-on: ubuntu-latest
steps:
- name: Download dist folder
uses: actions/download-artifact@v4
with:
name: dist_folder
path: ./emit-ch4plume-v1/dist
- name: ConfigureAWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.DEPLOYMENT_ROLE_ARN }}
role-session-name: ${{ github.repository_owner}}
aws-region: us-west-2
- name: Upload to S3
run: |
export PUBLIC_URL=$(echo ${{ vars.PUBLIC_URL_EMIT }} | sed -E 's|^https?://[^/]+/||')
echo "PUBLIC_URL=${PUBLIC_URL}" >> $GITHUB_ENV
aws s3 sync "./emit-ch4plume-v1/dist" s3://ghgc-custom-interfaces-staging/${PUBLIC_URL}/ --cache-control max-age=30,must-revalidate,s-maxage=604800 --delete
- name: Request Invalidation to AWS Cloudfront
uses: oneyedev/aws-cloudfront-invalidation@v1
with:
distribution-id: ${{ secrets.CF_DISTRIBUTION_ID }}
paths: |
/ghgcenter/custom-interfaces/*
deploy-production:
needs:
- build
- define-environment
if: needs.define-environment.outputs.target_branch == 'production'
environment: ${{ needs.define-environment.outputs.env_name }}
permissions:
id-token: write
contents: read
runs-on: ubuntu-latest
steps:
- name: Download dist folder
uses: actions/download-artifact@v4
with:
name: dist_folder
path: ./emit-ch4plume-v1/dist
- name: ConfigureAWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.DEPLOYMENT_ROLE_ARN }}
role-session-name: ${{ github.repository_owner}}
aws-region: us-west-2
- name: Upload to S3
run: |
export PUBLIC_URL=$(echo ${{ vars.PUBLIC_URL_EMIT }} | sed -E 's|^https?://[^/]+/||')
echo "PUBLIC_URL=${PUBLIC_URL}" >> $GITHUB_ENV
aws s3 sync "./emit-ch4plume-v1/dist" s3://ghgc-custom-interfaces-production/${PUBLIC_URL}/ --cache-control max-age=30,must-revalidate,s-maxage=604800 --delete
- name: Request Invalidation to AWS Cloudfront
uses: oneyedev/aws-cloudfront-invalidation@v1
with:
distribution-id: ${{ secrets.CF_DISTRIBUTION_ID }}
paths: |
/ghgcenter/custom-interfaces/*