-
Notifications
You must be signed in to change notification settings - Fork 18
162 lines (151 loc) · 7.5 KB
/
publish-gatsby.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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 }}