Staging to Production #241
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
name: PR preview | |
on: | |
pull_request: | |
types: [opened, synchronize, closed] | |
paths: | |
- 'emit-ch4plume-v1/**' | |
- 'noaa-cpfp-point/**' | |
- 'nist-interface/**' | |
- 'goes-plume-viewer/**' | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.ref }} | |
cancel-in-progress: true | |
env: | |
S3_BUCKET: ghgc-custom-interfaces-develop | |
PR_STRING: pr-preview-${{ github.event.number }} | |
jobs: | |
build_and_deploy: | |
if: ${{ github.event.action != 'closed' }} | |
environment: development | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write | |
contents: read | |
pull-requests: write | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: 'v20.9.0' | |
- name: Install Yarn | |
run: npm install -g yarn | |
- name: Determine changed files | |
id: changes | |
run: | | |
git fetch origin ${{ github.event.pull_request.base.ref }} --depth=1 | |
git diff --name-only origin/${{ github.event.pull_request.base.ref }} > changed_files.txt | |
cat changed_files.txt | |
# Initialize CHANGED_WEBSITES as an empty array | |
CHANGED_WEBSITES=() | |
# Check for changed folders and add them to the array | |
if grep -q 'emit-ch4plume-v1/' changed_files.txt; then | |
CHANGED_WEBSITES+=('emit-ch4plume-v1') | |
fi | |
if grep -q 'noaa-cpfp-point/' changed_files.txt; then | |
CHANGED_WEBSITES+=('noaa-cpfp-point') | |
fi | |
if grep -q 'nist-interface/' changed_files.txt; then | |
CHANGED_WEBSITES+=('nist-interface') | |
fi | |
if grep -q 'goes-plume-viewer/' changed_files.txt; then | |
CHANGED_WEBSITES+=('goes-plume-viewer') | |
fi | |
# Convert array to comma-separated string and set it as an environment variable | |
CHANGED_WEBSITES_STRING=$(IFS=, ; echo "${CHANGED_WEBSITES[*]}") | |
echo "CHANGED_WEBSITES=$CHANGED_WEBSITES_STRING" >> $GITHUB_ENV | |
- name: Configure AWS 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: Validate required variables and secrets | |
run: | | |
IFS=',' read -r -a WEBSITES <<< "${{ env.CHANGED_WEBSITES }}" | |
for WEBSITE in "${WEBSITES[@]}"; do | |
if [ "$WEBSITE" == "emit-ch4plume-v1" ]; then | |
missing_vars=() | |
# Check for required variables | |
[ -z "${{ vars.MAP_STYLE }}" ] && missing_vars+=("vars.MAP_STYLE") | |
[ -z "${{ secrets.MAP_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.MAP_ACCESS_TOKEN") | |
[ -z "${{ vars.PUBLIC_URL_EMIT }}" ] && missing_vars+=("vars.PUBLIC_URL_EMIT") | |
[ -z "${{ secrets.GEOAPIFY_APIKEY }}" ] && missing_vars+=("secrets.GEOAPIFY_APIKEY") | |
# 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 | |
elif [ "$WEBSITE" == "noaa-cpfp-point" ]; then | |
missing_vars=() | |
# Check for required variables | |
[ -z "${{ vars.PUBLIC_URL_NOAA }}" ] && missing_vars+=("vars.PUBLIC_URL_NOAA") | |
[ -z "${{ secrets.MAP_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.MAP_ACCESS_TOKEN") | |
# 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 | |
elif [ "$WEBSITE" == "nist-interface" ]; then | |
missing_vars=() | |
# Check for required variables | |
[ -z "${{ secrets.REACT_APP_MAPBOX_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.REACT_APP_MAPBOX_ACCESS_TOKEN") | |
[ -z "${{ secrets.REACT_APP_MAPBOX_STYLE_URL }}" ] && missing_vars+=("secrets.REACT_APP_MAPBOX_STYLE_URL") | |
[ -z "${{ vars.FEATURES_API_URL }}" ] && missing_vars+=("vars.FEATURES_API_URL") | |
# 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 | |
elif [ "$WEBSITE" == "goes-plume-viewer" ]; then | |
missing_vars=() | |
# Check for required variables | |
[ -z "${{ secrets.REACT_APP_MAPBOX_ACCESS_TOKEN }}" ] && missing_vars+=("secrets.REACT_APP_MAPBOX_TOKEN") | |
[ -z "${{ vars.REACT_APP_MAPBOX_STYLE_URL }}" ] && missing_vars+=("vars.REACT_APP_MAPBOX_STYLE_URL") | |
[ -z "${{ vars.REACT_APP_BASE_PATH_GOES }}" ] && missing_vars+=("vars.REACT_APP_BASE_PATH_GOES") | |
# 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 | |
fi | |
done | |
shell: bash | |
- name: Build and deploy | |
run: | | |
IFS=',' read -r -a WEBSITES <<< "${{ env.CHANGED_WEBSITES }}" | |
COMMENT="### ✅ Deploy Preview for *custom-interfaces* is ready! \n \n" | |
for WEBSITE in "${WEBSITES[@]}"; do | |
if [ "$WEBSITE" == "emit-ch4plume-v1" ]; then | |
echo MAP_STYLE="${{ vars.MAP_STYLE }}" >> ./emit-ch4plume-v1/.env | |
echo MAP_ACCESS_TOKEN="${{ secrets.MAP_ACCESS_TOKEN }}" >> ./emit-ch4plume-v1/.env | |
echo PUBLIC_URL="${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}" >> ./emit-ch4plume-v1/.env | |
echo STAGE="production" >> ./emit-ch4plume-v1/.env | |
cd ./emit-ch4plume-v1 | |
GEOAPIFY_APIKEY="${{ secrets.GEOAPIFY_APIKEY }}" node update_data.js | |
npm install | |
npm run deploy | |
cd .. | |
aws s3 sync emit-ch4plume-v1/dist s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}/ | |
COMMENT+="- 🛰️ emit-ch4plume-v1: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}/index.html \n \n" | |
elif [ "$WEBSITE" == "noaa-cpfp-point" ]; then | |
echo PUBLIC_URL="${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}" >> ./noaa-cpfp-point/.env | |
echo MAPBOX_ACCESS_TOKEN="${{ secrets.MAP_ACCESS_TOKEN }}" >> ./noaa-cpfp-point/.env | |
cd ./noaa-cpfp-point | |
npm install | |
PUBLIC_URL="${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}" npm run production | |
cd .. | |
aws s3 sync noaa-cpfp-point/dist s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/ | |
aws s3 sync noaa-cpfp-point/data s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/data/ | |
COMMENT+="- 🗼 noaa-cpfp-point: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/index.html" | |
elif [ "$WEBSITE" == "nist-interface" ]; then | |
NIST_PATH=$(echo "${{ vars.PUBLIC_URL_NIST }}/${{ env.PR_STRING }}" | sed -E 's|^https?://[^/]+||; s|/*$|/|') | |
echo REACT_APP_MAPBOX_TOKEN="${{ secrets.REACT_APP_MAPBOX_ACCESS_TOKEN }}" >> ./nist-interface/.env | |
echo REACT_APP_MAPBOX_STYLE_URL="${{ secrets.REACT_APP_MAPBOX_STYLE_URL }}" >> ./nist-interface/.env | |
echo REACT_APP_FEATURES_API_URL="${{ vars.FEATURES_API_URL }}" >> ./nist-interface/.env | |
echo REACT_APP_PUBLIC_URL="${NIST_PATH}" >> ./nist-interface/.env | |
URL_VALUE=$(grep "REACT_APP_PUBLIC_URL" ./nist-interface/.env) | |
echo "The value of REACT_APP_PUBLIC_URL is: ${URL_VALUE}" | |
cd ./nist-interface | |
yarn | |
yarn build | |
cd .. | |
aws s3 sync nist-interface/build s3://${{ env.S3_BUCKET }}${NIST_PATH} | |
COMMENT+="- 🗼 nist-interface: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${NIST_PATH}index.html" | |
elif [ "$WEBSITE" == "goes-plume-viewer" ]; then | |
GOES_PATH=$(echo "${{ vars.PUBLIC_URL_GOES }}/${{ env.PR_STRING }}" | sed -E 's|^https?://[^/]+||; s|/*$|/|') | |
echo >> ./goes-plume-viewer/.env # add a new line to append the below variables | |
echo REACT_APP_MAPBOX_TOKEN="${{secrets.REACT_APP_MAPBOX_ACCESS_TOKEN}}" >> ./goes-plume-viewer/.env | |
echo REACT_APP_MAPBOX_STYLE_URL="${{vars.REACT_APP_MAPBOX_STYLE_URL}}" >> ./goes-plume-viewer/.env | |
echo REACT_APP_BASE_PATH="${{vars.REACT_APP_BASE_PATH_GOES}}" >> ./goes-plume-viewer/.env | |
echo REACT_APP_PUBLIC_URL="${GOES_PATH}" >> ./goes-plume-viewer/.env | |
URL_VALUE=$(grep "REACT_APP_PUBLIC_URL" ./goes-plume-viewer/.env) | |
echo "The value of REACT_APP_PUBLIC_URL is: ${URL_VALUE}" | |
cd ./goes-plume-viewer | |
yarn | |
CI=false yarn build | |
cd .. | |
aws s3 sync goes-plume-viewer/build s3://${{ env.S3_BUCKET }}${GOES_PATH} | |
COMMENT+="- 🛰️ goes-plume-viewer: https://${{ env.S3_BUCKET }}.s3.us-west-2.amazonaws.com${GOES_PATH}index.html" | |
fi | |
echo "COMMENT=${COMMENT}" >> $GITHUB_ENV | |
done | |
- name: Add/update PR comment | |
id: find-comment | |
run: | | |
COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | |
COMMENTS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" $COMMENTS_URL) | |
COMMENT_ID=$(echo $COMMENTS | jq -r '.[] | select(.user.login == "github-actions[bot]") | .id') | |
if [ -z "$COMMENT_ID" ]; then | |
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -X POST -d "{\"body\":\"${{ env.COMMENT }}\"}" "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | |
else | |
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -X PATCH -d "{\"body\":\"${{ env.COMMENT }}\"}" "https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
clean_up: | |
if: ${{ github.event.action == 'closed' }} | |
environment: development | |
runs-on: ubuntu-latest | |
permissions: | |
id-token: write | |
contents: read | |
pull-requests: write | |
steps: | |
- name: Configure AWS 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: Delete built websites from S3 | |
run: | | |
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_EMIT }}/${{ env.PR_STRING }}/ --recursive | |
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NOAA }}/${{ env.PR_STRING }}/ --recursive | |
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_NIST }}/${{ env.PR_STRING }}/ --recursive | |
aws s3 rm s3://${{ env.S3_BUCKET }}${{ vars.PUBLIC_URL_GOES }}/${{ env.PR_STRING }}/ --recursive | |
- name: Remove PR comment | |
run: | | |
COMMENTS_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | |
COMMENTS=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" $COMMENTS_URL) | |
COMMENT_ID=$(echo $COMMENTS | jq -r '.[] | select(.user.login == "github-actions[bot]") | .id') | |
if [ -n "$COMMENT_ID" ]; then | |
curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -X DELETE -d "{\"body\":\"${{ env.COMMENT }}\"}" "https://api.github.com/repos/${{ github.repository }}/issues/comments/$COMMENT_ID" | |
fi | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |