Created
August 19, 2020 13:35
-
-
Save edzis/17602cb75eb6346b9096b7b53498a424 to your computer and use it in GitHub Desktop.
Customizer monorepo GH Actions
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
/* | |
WHEN YOU EDIT THIS FILE REMEMBER TO RUN: | |
yarn run prepare | |
TO BUILD THE DIST AND COMMIT IT. | |
*/ | |
const core = require('@actions/core') | |
const github = require('@actions/github') | |
function getBranch(ref) { | |
const parts = ref.split('/') | |
// Drop first 2. | |
parts.splice(0, 2) | |
return parts.join('/') | |
} | |
async function run() { | |
try { | |
const branch = getBranch(github.context.ref) | |
core.info(`Detected branch: ${branch}`) | |
const customizers = [ | |
'AAAAA-11111111111', | |
'AAAAA-222222', | |
'BBBB-111111', | |
'BBBB-2222222222', | |
'CCCC-11111111', | |
'DDDD-11111111', | |
'DDDD-2222222222', | |
] | |
const envs = [ | |
{ re: /^master$/, deployTo: 'dev' }, | |
{ re: /^staging\/(.*)$/, deployTo: 'staging' }, | |
{ re: /^production\/(.*)$/, deployTo: 'production' }, | |
{ re: /^((?:.*)-(?:.*))\/(?:.*)$/, deployTo: 'dev' }, | |
] | |
let matrix = null | |
let deploy = false | |
for (const env of envs) { | |
const res = env.re.exec(branch) | |
if (res) { | |
core.info('Match on: ' + env.re.source) | |
switch (res.length) { | |
case 1: | |
matrix = { | |
customizer: customizers, | |
deployTo: [env.deployTo], | |
} | |
break | |
case 2: | |
matrix = { | |
customizer: [res[1]], | |
deployTo: [env.deployTo], | |
} | |
break | |
default: | |
core.setFailed('Unknown branch naming schema.') | |
break | |
} | |
deploy = true | |
break | |
} | |
} | |
core.setOutput('matrix', matrix) | |
core.setOutput('deploy', deploy.toString()) | |
core.info(`Matrix:\n${JSON.stringify(matrix, null, ' ')}`) | |
core.info(`Deploy: ${deploy}`) | |
} catch (error) { | |
core.setFailed(error.message) | |
} | |
} | |
run() |
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: Customizer | |
on: | |
- push | |
jobs: | |
plan: | |
name: 'Plan CI/CD jobs' | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.matrix.outputs.matrix }} | |
deploy: ${{ steps.matrix.outputs.deploy }} | |
steps: | |
- name: Check out source code | |
uses: actions/checkout@v2 | |
- name: Calculate deploy matrix | |
id: matrix | |
uses: ./.github/actions/deploy-matrix | |
onlyTest: | |
if: needs.plan.outputs.deploy == 'false' | |
name: 'Only Test' | |
runs-on: ubuntu-latest | |
needs: [plan] | |
steps: | |
- name: Check out source code | |
uses: actions/checkout@v2 | |
- name: Create LFS file list | |
run: git lfs ls-files -l | cut -d' ' -f1 | sort > .lfs-assets-id | |
- name: Restore LFS cache | |
uses: actions/cache@v2 | |
id: lfs-cache | |
with: | |
path: .git/lfs | |
key: ${{ runner.os }}-lfs-${{ hashFiles('.lfs-assets-id') }}-v1 | |
- name: Git LFS Pull | |
run: git lfs pull | |
- name: Get yarn cache directory path | |
id: yarn-cache-dir-path | |
run: echo "::set-output name=dir::$(yarn config get cacheFolder)" | |
- uses: actions/cache@v2 | |
id: yarn-cache | |
with: | |
path: ${{ steps.yarn-cache-dir-path.outputs.dir }} | |
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-yarn- | |
- uses: actions/setup-node@v2-beta | |
with: | |
# Last version which works with librsvg2 package. | |
node-version: '11' | |
- name: Install system deps | |
run: sudo apt-get install librsvg2-bin | |
- name: Install dependencies | |
run: yarn --pure-lockfile | |
- name: Run tests | |
run: scripts/test | |
deploy: | |
if: needs.plan.outputs.deploy == 'true' | |
name: 'Deploy' | |
runs-on: ubuntu-latest | |
needs: [plan] | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJson(needs.plan.outputs.matrix) }} | |
steps: | |
- name: Check out source code | |
uses: actions/checkout@v2 | |
- name: Create LFS file list | |
run: git lfs ls-files -l | cut -d' ' -f1 | sort > .lfs-assets-id | |
- name: Restore LFS cache | |
uses: actions/cache@v2 | |
id: lfs-cache | |
with: | |
path: .git/lfs | |
key: ${{ runner.os }}-lfs-${{ hashFiles('.lfs-assets-id') }}-v1 | |
- name: Git LFS Pull | |
run: git lfs pull | |
- name: Get yarn cache directory path | |
id: yarn-cache-dir-path | |
run: echo "::set-output name=dir::$(yarn config get cacheFolder)" | |
- uses: actions/cache@v2 | |
id: yarn-cache | |
with: | |
path: ${{ steps.yarn-cache-dir-path.outputs.dir }} | |
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }} | |
restore-keys: | | |
${{ runner.os }}-yarn- | |
- uses: actions/setup-node@v2-beta | |
with: | |
# Last version which works with librsvg2 package. | |
node-version: '11' | |
- name: Install system deps | |
run: sudo apt-get install librsvg2-bin | |
- name: Install dependencies | |
run: yarn --pure-lockfile | |
- name: Run tests | |
run: scripts/test | |
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master | |
with: | |
service_account_key: ${{ secrets.GCP_SA_KEY }} | |
- name: Deploy | |
env: | |
CUSTOMIZER: ${{ matrix.customizer }} | |
DEPLOY_ENV: ${{ matrix.deployTo }} | |
COMMIT: ${{ github.sha }} | |
run: | | |
echo "Build and deploy ${CUSTOMIZER}" | |
echo "::group name::Setup gcloud" | |
gcloud config set project XXXXXXX | |
gcloud auth configure-docker | |
gcloud container clusters get-credentials --region=XXXXXXX XXXXXXXX | |
echo "::endgroup::" | |
echo "::group name::Build" | |
scripts/build "${CUSTOMIZER}" "${COMMIT}" | |
echo "::endgroup::" | |
echo "::group name::Prepare storage buckets" | |
scripts/prepare-storage-buckets "${CUSTOMIZER}" | |
echo "::endgroup::" | |
echo "::group name::Deploy" | |
scripts/deploy "${CUSTOMIZER}" "${COMMIT}" "${DEPLOY_ENV}" | |
echo "::endgroup::" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment