forked from cloud-carbon-footprint/cloud-carbon-footprint
-
Notifications
You must be signed in to change notification settings - Fork 0
375 lines (357 loc) · 15.8 KB
/
ci.yml
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
name: CI
on:
workflow_dispatch:
push:
branches: [trunk]
paths-ignore:
- 'microsite/**'
- '.github/workflows/deploy-microsite.yml'
pull_request:
branches: [trunk]
types: [closed]
paths-ignore:
- 'microsite/**'
- '.github/workflows/deploy-microsite.yml'
jobs:
lint-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: setup node 14
uses: actions/setup-node@v2
with:
node-version: '14'
# Cache every node_modules folder inside the monorepo
- name: cache all node_modules
id: cache-modules
uses: actions/cache@v2
with:
path: '**/node_modules'
# We use both yarn.lock and package.json as cache keys to ensure that changes to local monorepo packages bust the cache.
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }}
- name: find location of global yarn cache
id: yarn-cache
if: steps.cache-modules.outputs.cache-hit != 'true'
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: cache global yarn cache
uses: actions/cache@v2
if: steps.cache-modules.outputs.cache-hit != 'true'
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: yarn install
if: steps.cache-modules.outputs.cache-hit != 'true'
run: yarn
- name: lint
run: yarn lint
- name: test and upload coverage
run: |
yarn test
bash <(curl -s https://codecov.io/bash)
bash <(curl -s https://codecov.io/bash) -f packages/client/coverage/* -F client
bash <(curl -s https://codecov.io/bash) -f packages/core/coverage/* -F core
bash <(curl -s https://codecov.io/bash) -f packages/app/coverage/* -F app
bash <(curl -s https://codecov.io/bash) -f packages/api/coverage/* -F api
bash <(curl -s https://codecov.io/bash) -f packages/cli/coverage/* -F cli
bash <(curl -s https://codecov.io/bash) -f packages/aws/coverage/* -F aws
bash <(curl -s https://codecov.io/bash) -f packages/gcp/coverage/* -F gcp
bash <(curl -s https://codecov.io/bash) -f packages/azure/coverage/* -F azure
# - name: integration tests
# run: yarn test:integration
build:
needs: lint-test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0
- uses: actions/setup-node@v2
with:
node-version: '14'
# Cache every node_modules folder inside the monorepo
- name: cache all node_modules
id: cache-modules
uses: actions/cache@v2
with:
path: '**/node_modules'
# We use both yarn.lock and package.json as cache keys to ensure that changes to local monorepo packages bust the cache.
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }}
- name: find location of global yarn cache
id: yarn-cache
if: steps.cache-modules.outputs.cache-hit != 'true'
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: cache global yarn cache
uses: actions/cache@v2
if: steps.cache-modules.outputs.cache-hit != 'true'
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: yarn install
if: steps.cache-modules.outputs.cache-hit != 'true'
run: yarn install
- name: build-packages
run: |
yarn lerna run build --ignore @cloud-carbon-footprint/client --ignore @cloud-carbon-footprint/cli --ignore @cloud-carbon-footprint/create-app --ignore @cloud-carbon-footprint/integration-tests
- name: build-client
env:
REACT_APP_DATE_RANGE_VALUE: ${{ secrets.REACT_APP_DATE_RANGE_VALUE }}
REACT_APP_DATE_RANGE_TYPE: ${{ secrets.REACT_APP_DATE_RANGE_TYPE }}
REACT_APP_PREVIOUS_YEAR_OF_USAGE: ${{ secrets.REACT_APP_PREVIOUS_YEAR_OF_USAGE }}
REACT_APP_GROUP_BY: ${{ secrets.REACT_APP_GROUP_BY }}
REACT_APP_MINIMAL_DATE_AGE: ${{ secrets.REACT_APP_MINIMAL_DATE_AGE }}
run: |
cd packages/client
./create_client_env_file.sh
yarn build:app
cd ../..
- name: build-workspace
run: |
yarn lerna run build:workspace
# replacing local @cloud-carbon-footprint dependencies with a local reference
yarn lerna run build:update
- name: zip artifact
run: zip dist-workspace.zip dist-workspace/* -r
- uses: actions/upload-artifact@v2
with:
name: dist-workspace
path: dist-workspace.zip
release:
needs: build
runs-on: ubuntu-latest
env:
CI: 'true'
NODE_OPTIONS: --max-old-space-size=4096
steps:
- uses: actions/checkout@v2
# Beginning of yarn setup, keep in sync between all workflows, see ci.yml
- name: use node.js 14
uses: actions/setup-node@v1
with:
node-version: '14'
registry-url: https://registry.npmjs.org/ # Needed for auth
- name: cache all node_modules
id: cache-modules
uses: actions/cache@v2
with:
path: '**/node_modules'
key: ${{ runner.os }}-node_modules-${{ hashFiles('yarn.lock', '**/package.json') }}
- name: find location of global yarn cache
id: yarn-cache
if: steps.cache-modules.outputs.cache-hit != 'true'
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: cache global yarn cache
uses: actions/cache@v2
if: steps.cache-modules.outputs.cache-hit != 'true'
with:
path: ${{ steps.yarn-cache.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
- name: yarn install
if: steps.cache-modules.outputs.cache-hit != 'true'
run: yarn install --immutable
# End of yarn setup
# - name: build type declarations
# run: yarn tsc:full
- name: build packages
# uses the react-scripts build and tsc build for client
run: |
yarn lerna run build
cd packages/client
./create_client_env_file.sh
yarn build:app
cd ../..
# Publishes current version of packages that are not already present in the registry
- name: publish
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
run: yarn lerna publish from-package --yes
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# Creates the next available tag with format "release-<year>-<month>-<day>[.<n>]"
- name: Create a release tag
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
id: create_tag
run: node scripts/create-release-tag.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Creates and updates the next available tag with format "latest"
- name: Create latest release tag
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
id: create_latest_tag
uses: EndBug/latest-tag@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# Convert the newly created tag into a release with changelog information
- name: Create release on GitHub
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
run: node scripts/create-github-release.js ${{ steps.create_tag.outputs.tag_name }} 1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Login to DockerHub
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push client to Docker Hub
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
uses: docker/build-push-action@v2
with:
context: packages/client
push: true
tags: |
cloudcarbonfootprint/client:${{ steps.create_tag.outputs.tag_name }}
cloudcarbonfootprint/client:latest
- name: Build and push api to Docker Hub
if: github.event.pull_request.head.ref == 'changeset-release/trunk'
uses: docker/build-push-action@v2
with:
context: packages/api
push: true
tags: |
cloudcarbonfootprint/api:${{ steps.create_tag.outputs.tag_name }}
cloudcarbonfootprint/api:latest
deploy-staging:
if: github.ref == 'refs/heads/trunk'
needs: build
runs-on: ubuntu-latest
container:
image: node:14.18-alpine3.13
environment:
name: staging
env:
AWS_PROXY_ACCOUNT_ID: ${{ secrets.AWS_PROXY_ACCOUNT_ID }}
AWS_PROXY_ROLE_NAME: ${{ secrets.AWS_PROXY_ROLE_NAME }}
AWS_ACCOUNTS: ${{ secrets.AWS_ACCOUNTS }}
AWS_AUTH_MODE: ${{ secrets.AWS_AUTH_MODE }}
AWS_ATHENA_REGION: ${{ secrets.AWS_ATHENA_REGION }}
AWS_TARGET_ACCOUNT_ROLE_NAME: ${{ secrets.AWS_TARGET_ACCOUNT_ROLE_NAME }}
AWS_ATHENA_DB_NAME: ${{ secrets.AWS_ATHENA_DB_NAME }}
AWS_ATHENA_DB_TABLE: ${{ secrets.AWS_ATHENA_DB_TABLE }}
AWS_ATHENA_QUERY_RESULT_LOCATION: ${{ secrets.AWS_ATHENA_QUERY_RESULT_LOCATION }}
AWS_BILLING_ACCOUNT_ID: ${{ secrets.AWS_BILLING_ACCOUNT_ID }}
AWS_BILLING_ACCOUNT_NAME: ${{ secrets.AWS_BILLING_ACCOUNT_NAME }}
GCP_PROJECTS: ${{ secrets.GCP_PROJECTS }}
GCP_BIG_QUERY_TABLE: ${{ secrets.GCP_BIG_QUERY_TABLE }}
GCP_BILLING_PROJECT_ID: ${{ secrets.GCP_BILLING_PROJECT_ID }}
GCP_BILLING_PROJECT_NAME: ${{ secrets.GCP_BILLING_PROJECT_NAME }}
GCP_USE_CARBON_FREE_ENERGY_PERCENTAGE: ${{ secrets.GCP_USE_CARBON_FREE_ENERGY_PERCENTAGE }}
GCP_VCPUS_PER_GKE_CLUSTER: ${{ secrets.GCP_VCPUS_PER_GKE_CLUSTER }}
GCP_VCPUS_PER_CLOUD_COMPOSER_ENVIRONMENT: ${{ secrets.GCP_VCPUS_PER_CLOUD_COMPOSER_ENVIRONMENT }}
AZURE_AUTH_MODE: ${{ secrets.AZURE_AUTH_MODE }}
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
AZURE_USE_BILLING_DATA: ${{ secrets.AZURE_USE_BILLING_DATA }}
GROUP_QUERY_RESULTS_BY: ${{ secrets.GROUP_QUERY_RESULTS_BY }}
GCS_CACHE_BUCKET_NAME: ${{ secrets.GCS_CACHE_BUCKET_NAME }}
CACHE_MODE: ${{ secrets.CACHE_MODE }}
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: dist-workspace
- name: unzip artifact for deployment
run: unzip dist-workspace.zip
- name: deploy-staging
env:
GCLOUD_SERVICE_KEY: ${{ secrets.GCLOUD_SERVICE_KEY }}
GOOGLE_PROJECT_ID: ${{ secrets.GOOGLE_PROJECT_ID }}
run: |
apk update && apk add --no-cache python3 py-crcmod bash libc6-compat curl git
export PATH=$PATH:/root/google-cloud-sdk/bin
curl -sSL https://sdk.cloud.google.com > /tmp/gcloud-install && bash /tmp/gcloud-install --disable-prompts --install-dir=/root
cp packages/api/create_server_env_file.sh dist-workspace/packages/api
cp packages/api/app-staging.yaml dist-workspace/packages/api
cp packages/client/app-staging.yaml dist-workspace/packages/client
# We need to set yarn to v1 because App Engine will only install with a yarn.lock file generated from this version.
yarn set version 1.22.4
rm yarn.lock
rm package.json
cd dist-workspace/packages/api
# Generate yarn.lock file
yarn install
./create_server_env_file.sh
cd ../../..
# Store service account
echo $GCLOUD_SERVICE_KEY > ${HOME}/gcloud-service-key.json
# Initialize gcloud CLI
gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json
gcloud --quiet config set project $GOOGLE_PROJECT_ID
cd appengine
./deploy-staging.sh
cd ..
deploy-production:
if: github.ref == 'refs/heads/trunk'
needs: deploy-staging
runs-on: ubuntu-latest
container:
image: node:14.18-alpine3.13
environment:
name: production
env:
AWS_PROXY_ACCOUNT_ID: ${{ secrets.AWS_PROXY_ACCOUNT_ID }}
AWS_PROXY_ROLE_NAME: ${{ secrets.AWS_PROXY_ROLE_NAME }}
AWS_ACCOUNTS: ${{ secrets.AWS_ACCOUNTS }}
AWS_AUTH_MODE: ${{ secrets.AWS_AUTH_MODE }}
AWS_ATHENA_REGION: ${{ secrets.AWS_ATHENA_REGION }}
AWS_TARGET_ACCOUNT_ROLE_NAME: ${{ secrets.AWS_TARGET_ACCOUNT_ROLE_NAME }}
AWS_ATHENA_DB_NAME: ${{ secrets.AWS_ATHENA_DB_NAME }}
AWS_ATHENA_DB_TABLE: ${{ secrets.AWS_ATHENA_DB_TABLE }}
AWS_ATHENA_QUERY_RESULT_LOCATION: ${{ secrets.AWS_ATHENA_QUERY_RESULT_LOCATION }}
AWS_BILLING_ACCOUNT_ID: ${{ secrets.AWS_BILLING_ACCOUNT_ID }}
AWS_BILLING_ACCOUNT_NAME: ${{ secrets.AWS_BILLING_ACCOUNT_NAME }}
GCP_PROJECTS: ${{ secrets.GCP_PROJECTS }}
GCP_BIG_QUERY_TABLE: ${{ secrets.GCP_BIG_QUERY_TABLE }}
GCP_BILLING_PROJECT_ID: ${{ secrets.GCP_BILLING_PROJECT_ID }}
GCP_BILLING_PROJECT_NAME: ${{ secrets.GCP_BILLING_PROJECT_NAME }}
GCP_USE_CARBON_FREE_ENERGY_PERCENTAGE: ${{ secrets.GCP_USE_CARBON_FREE_ENERGY_PERCENTAGE }}
GCP_VCPUS_PER_GKE_CLUSTER: ${{ secrets.GCP_VCPUS_PER_GKE_CLUSTER }}
GCP_VCPUS_PER_CLOUD_COMPOSER_ENVIRONMENT: ${{ secrets.GCP_VCPUS_PER_CLOUD_COMPOSER_ENVIRONMENT }}
AZURE_AUTH_MODE: ${{ secrets.AZURE_AUTH_MODE }}
AZURE_CLIENT_ID: ${{ secrets.AZURE_CLIENT_ID }}
AZURE_CLIENT_SECRET: ${{ secrets.AZURE_CLIENT_SECRET }}
AZURE_TENANT_ID: ${{ secrets.AZURE_TENANT_ID }}
GROUP_QUERY_RESULTS_BY: ${{ secrets.GROUP_QUERY_RESULTS_BY }}
GCS_CACHE_BUCKET_NAME: ${{ secrets.GCS_CACHE_BUCKET_NAME }}
CACHE_MODE: ${{ secrets.CACHE_MODE }}
steps:
- uses: actions/checkout@v2
- uses: actions/download-artifact@v2
with:
name: dist-workspace
- name: unzip artifact for deployment
run: unzip dist-workspace.zip
- name: deploy-production
env:
GCLOUD_SERVICE_KEY: ${{ secrets.GCLOUD_SERVICE_KEY }}
GOOGLE_PROJECT_ID: ${{ secrets.GOOGLE_PROJECT_ID }}
run: |
apk update && apk add --no-cache python3 py-crcmod bash libc6-compat curl git
export PATH=$PATH:/root/google-cloud-sdk/bin
curl -sSL https://sdk.cloud.google.com > /tmp/gcloud-install && bash /tmp/gcloud-install --disable-prompts --install-dir=/root
cp packages/api/create_server_env_file.sh dist-workspace/packages/api
cp packages/api/app-production.yaml dist-workspace/packages/api
cp packages/client/app-production.yaml dist-workspace/packages/client
# We need to set yarn to v1 because App Engine will only install with a yarn.lock file generated from this version.
yarn set version 1.22.4
rm yarn.lock
rm package.json
cd dist-workspace/packages/api
# Generate yarn.lock file
yarn install
./create_server_env_file.sh
cd ../../..
# Store service account
echo $GCLOUD_SERVICE_KEY > ${HOME}/gcloud-service-key.json
# Initialize gcloud CLI
gcloud auth activate-service-account --key-file=${HOME}/gcloud-service-key.json
gcloud --quiet config set project $GOOGLE_PROJECT_ID
cd appengine
./deploy-production.sh
cd ..