Publish Gatsby #4859
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: Publish Gatsby | |
on: | |
repository_dispatch: | |
types: [publish-gatsby] | |
workflow_dispatch: | |
inputs: | |
branchName: | |
description: "Publish gatsby for which branch?" | |
default: 'main' | |
type: choice | |
required: true | |
options: | |
- alpha | |
- main | |
buildEnv: | |
description: "Publish gatsby for which environment?" | |
default: 'prod' | |
type: choice | |
required: true | |
options: | |
- prod | |
- test | |
- dev | |
concurrency: | |
group: publish-gatsby-${{ github.event.client_payload.branch }}${{ github.event.inputs.branchName }}-${{ github.event.client_payload.env }}${{ github.event.inputs.buildEnv }} | |
cancel-in-progress: true | |
env: | |
GATSBY_ENABLE_SNOWPLOW: true | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Validate inputs | |
run: | | |
if [[ "${{ github.event.inputs.branchName }}" == "alpha" && "${{ github.event.inputs.buildEnv }}" == "prod" ]]; then | |
echo "alpha/prod is not a valid environment combo" | |
exit 1 | |
fi | |
- name: Set env based on branch | |
run: | | |
if [[ "${{ github.event.client_payload.branch }}" == "main" || "${{ github.event.inputs.branchName }}" == "main" ]]; then | |
echo "BRANCH=main" >> $GITHUB_ENV | |
echo "BUILDER_IMAGE_NAME=public-builder-main" >> $GITHUB_ENV | |
echo "IMAGE_NAME=public-main" >> $GITHUB_ENV | |
echo "DEPLOYMENT_NAME=main-public" >> $GITHUB_ENV | |
else | |
echo "BRANCH=alpha" >> $GITHUB_ENV | |
echo "BUILDER_IMAGE_NAME=public-builder-alpha" >> $GITHUB_ENV | |
echo "IMAGE_NAME=public-alpha" >> $GITHUB_ENV | |
echo "DEPLOYMENT_NAME=alpha-public" >> $GITHUB_ENV | |
fi | |
- name: Set vars | |
id: vars | |
run: | | |
echo "registry_image=${{ secrets.OPENSHIFT_GOLD_EXTERNAL_REPOSITORY }}/${{ secrets.OPENSHIFT_GOLD_LICENSE_PLATE }}-tools/${{ env.IMAGE_NAME }}" >> $GITHUB_OUTPUT | |
echo "registry_builder_image=${{ secrets.OPENSHIFT_GOLD_EXTERNAL_REPOSITORY }}/${{ secrets.OPENSHIFT_GOLD_LICENSE_PLATE }}-tools/${{ env.BUILDER_IMAGE_NAME }}" >> $GITHUB_OUTPUT | |
- name: Set env based on client_payload | |
run: | | |
if [[ "${{ github.event.client_payload.env }}" == "prod" || "${{ github.event.inputs.buildEnv }}" == "prod" ]]; then | |
echo "ENV_SUFFIX=prod" >> $GITHUB_ENV | |
echo "IMAGE_TAG=prod" >> $GITHUB_ENV | |
echo "STRAPI_SOURCE_URL=https://cms.bcparks.ca" >> $GITHUB_ENV | |
echo "REACT_APP_CMS_BASE_URL=https://cms.bcparks.ca" >> $GITHUB_ENV | |
echo "REACT_APP_SITE_URL=https://bcparks.ca" >> $GITHUB_ENV | |
echo "STRAPI_TOKEN=${{ secrets.GATSBY_STRAPI_API_TOKEN__MAIN_PROD__GOLD }}" >> $GITHUB_ENV | |
elif [[ "${{ github.event.client_payload.env }}" == "test" || "${{ github.event.inputs.buildEnv }}" == "test" ]]; then | |
echo "ENV_SUFFIX=test" >> $GITHUB_ENV | |
echo "IMAGE_TAG=test" >> $GITHUB_ENV | |
if [[ "${{ github.event.client_payload.branch }}" == "main" || "${{ github.event.inputs.branchName }}" == "main" ]]; then | |
echo "STRAPI_SOURCE_URL=https://test-cms.bcparks.ca" >> $GITHUB_ENV | |
echo "REACT_APP_CMS_BASE_URL=https://bcparks.test.api.gov.bc.ca" >> $GITHUB_ENV | |
echo "REACT_APP_SITE_URL=https://test.bcparks.ca" >> $GITHUB_ENV | |
echo "STRAPI_TOKEN=${{ secrets.GATSBY_STRAPI_API_TOKEN__MAIN_TEST__GOLD }}" >> $GITHUB_ENV | |
else | |
echo "REACT_APP_CMS_BASE_URL=https://alpha-test-cms.bcparks.ca" >> $GITHUB_ENV | |
echo "REACT_APP_SITE_URL=https://alpha-test.bcparks.ca" >> $GITHUB_ENV | |
echo "STRAPI_TOKEN=${{ secrets.GATSBY_STRAPI_API_TOKEN__ALPHA_TEST }}" >> $GITHUB_ENV | |
fi | |
else | |
echo "ENV_SUFFIX=dev" >> $GITHUB_ENV | |
echo "IMAGE_TAG=latest" >> $GITHUB_ENV | |
if [[ "${{ github.event.client_payload.branch }}" == "main" || "${{ github.event.inputs.branchName }}" == "main" ]]; then | |
echo "REACT_APP_CMS_BASE_URL=https://dev-cms.bcparks.ca" >> $GITHUB_ENV | |
echo "REACT_APP_SITE_URL=https://dev.bcparks.ca" >> $GITHUB_ENV | |
echo "STRAPI_TOKEN=${{ secrets.GATSBY_STRAPI_API_TOKEN__MAIN_DEV__GOLD }}" >> $GITHUB_ENV | |
else | |
echo "REACT_APP_CMS_BASE_URL=https://alpha-dev-cms.bcparks.ca" >> $GITHUB_ENV | |
echo "REACT_APP_SITE_URL=https://alpha-dev.bcparks.ca" >> $GITHUB_ENV | |
echo "STRAPI_TOKEN=${{ secrets.GATSBY_STRAPI_API_TOKEN__ALPHA_DEV }}" >> $GITHUB_ENV | |
fi | |
fi | |
- name: Login to OpenShift Container Repository | |
uses: docker/login-action@v2 | |
with: | |
registry: ${{secrets.OPENSHIFT_GOLD_EXTERNAL_REPOSITORY}} | |
username: ${{secrets.OPENSHIFT_GOLD_SA_USERNAME}} | |
password: ${{secrets.OPENSHIFT_GOLD_SA_PASSWORD}} | |
- name: Setup Node | |
uses: actions/setup-node@v3 | |
with: | |
node-version: "18" | |
- run: mkdir -p gatsby/public && mkdir gatsby/.cache | |
- name: Cache Gatsby Cache Folder | |
uses: actions/cache@v3 | |
id: gatsby-cache-folder | |
with: | |
path: gatsby/.cache | |
key: ${{ runner.os }}-cache-gatsby-${{ env.BRANCH }}-${{ env.ENV_SUFFIX }} | |
restore-keys: | | |
${{ runner.os }}-cache-gatsby-${{ env.BRANCH }}-${{ env.ENV_SUFFIX }} | |
- name: Cache Gatsby Public Folder | |
uses: actions/cache@v3 | |
id: gatsby-public-folder | |
with: | |
path: gatsby/public | |
key: ${{ runner.os }}-public-gatsby-${{ env.BRANCH }}-${{ env.ENV_SUFFIX }} | |
restore-keys: | | |
${{ runner.os }}-public-gatsby-${{ env.BRANCH }}-${{ env.ENV_SUFFIX }} | |
- name: Build static | |
run: | | |
# Run the builder image as container since you can't directly copy file from an image | |
docker run -d -t --name builder ${{ steps.vars.outputs.registry_builder_image }}:${{ env.IMAGE_TAG }} | |
# Copy Gatsby source and packages to local. Tried to just do the build in the container, but ran into some weird issues with the build | |
docker cp builder:/gatsby/. gatsby | |
cd gatsby && npm run build | |
- name: Build and push public image | |
run: | | |
docker build -t ${{ steps.vars.outputs.registry_image }}:${{ env.IMAGE_TAG }} . | |
docker push ${{ steps.vars.outputs.registry_image }}:${{ env.IMAGE_TAG }} | |
working-directory: gatsby | |
- name: Install OpenShift CLI tools | |
uses: redhat-actions/openshift-tools-installer@v1 | |
with: | |
oc: "4.14" | |
- name: Login OpenShift | |
uses: redhat-actions/oc-login@v1 | |
with: | |
openshift_server_url: ${{ secrets.OPENSHIFT_GOLD_SERVER_URL }} | |
openshift_token: ${{ secrets.OPENSHIFT_GOLD_SERVICE_TOKEN }} | |
- name: Trigger rollout | |
run: | | |
oc -n ${{ secrets.OPENSHIFT_GOLD_LICENSE_PLATE }}-${{ env.ENV_SUFFIX }} rollout restart deployment ${{ env.DEPLOYMENT_NAME }} | |
# alert-if-failure: | |
# if: ${{ always() && (needs.build.result=='failure')}} | |
# runs-on: ubuntu-latest | |
# needs: [build] | |
# steps: | |
# - name: Send alert if build fails | |
# id: failure_alert | |
# run: | | |
# curl -X POST -H 'Content-Type: application/json' --data '{"workflow":"${{github.workflow}}","repo":"${{github.repository}}"}' https://chat.developer.gov.bc.ca/hooks/${{ secrets.ROCKETCHAT_TOKEN }} |