-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sharing env variables across callable workflows (#82)
* add define_shared_variables * Update define-shared-variables.yml * Update define-shared-variables.yml * Update define-shared-variables.yml * Update define-shared-variables.yml * Create install-dependencies.yml * use env variables to restore cache in base build * Update base-build.yml * Use shared variables in deploy-to-firebase.yml * Use centralized NODE_VERSION * Update define-shared-variables.yml * Update build.yml * Update define-shared-variables.yml
- Loading branch information
1 parent
1ad144f
commit 2c64bb5
Showing
6 changed files
with
207 additions
and
62 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
name: Define shared variables to be reused by callable workflow | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
shared-variables: | ||
required: false | ||
type: string | ||
|
||
jobs: | ||
run: | ||
runs-on: ubuntu-20.04 | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
name: Checkout Base | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: Export variables to JSON | ||
id: varsStep | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const sharedVariablesObject = '${{ inputs.shared-variables }}' || {}; | ||
function exportVariable(name, value) { | ||
sharedVariablesObject[name] = value; | ||
} | ||
switch('${{ github.event_name }}') { | ||
case 'pull_request': { | ||
exportVariable('HEAD_SHA', '${{ github.event.pull_request.head.sha }}'); | ||
exportVariable('CURRENT_BRANCH', '${{ github.head_ref }}'); | ||
exportVariable('BASE_BRANCH', '${{ github.base_ref }}'); | ||
exportVariable('NX_BASE', 'origin/${{ github.base_ref }}'); | ||
exportVariable('NX_HEAD', 'origin/${{ github.head_ref }}'); | ||
exportVariable('NX_BRANCH', '${{ github.head_ref }}'); | ||
break; | ||
} | ||
case 'workflow_dispatch': | ||
case 'push': { | ||
exportVariable('HEAD_SHA', '${{ github.sha }}'); | ||
exportVariable('CURRENT_BRANCH', '${{ github.ref_name }}'); | ||
exportVariable('BASE_BRANCH', '${{ github.ref_name }}'); | ||
exportVariable('NX_HEAD', 'origin/${{ github.ref_name }}'); | ||
exportVariable('NX_BRANCH', '${{ github.ref_name }}'); | ||
break; | ||
} | ||
} | ||
core.setOutput('value', JSON.stringify(sharedVariablesObject)); | ||
Object.entries(sharedVariablesObject).forEach(([name, value]) => { | ||
core.exportVariable(name, value); | ||
}); | ||
- name: Export composed variables to JSON | ||
id: sharedVarsStep | ||
uses: actions/github-script@v6 | ||
env: | ||
NODE_VERSION: 16.15.1 | ||
DEPENDENCIES_CACHE_KEY: ${{ runner.os }}-build-cache-node-modules-${{ hashFiles('**/package-lock.json') }} | ||
DEPENDENCIES_RESTORE_KEY: '' | ||
|
||
with: | ||
script: | | ||
const fileName = 'temp/variables.json'; | ||
const fs = require('fs'); | ||
const path = require('path') | ||
const sharedVariablesObject = ${{ steps.varsStep.outputs.value }}; | ||
const stepEnvVariables = ${{ toJson(env) }}; | ||
const mergedVariables = { ...sharedVariablesObject, ...stepEnvVariables }; | ||
fs.mkdirSync(path.dirname(fileName), { recursive: true }) | ||
fs.writeFileSync(fileName, JSON.stringify(mergedVariables)); | ||
core.setOutput('value', JSON.stringify(mergedVariables)); | ||
- name: Save success targets | ||
uses: actions/cache/save@v3 | ||
with: | ||
path: 'temp/variables.json' | ||
key: shared-variables-${{ github.run_id }}-${{ github.run_attempt }} | ||
|
||
- name: Display variables | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
console.log(${{ steps.sharedVarsStep.outputs.value }}); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
name: Define shared variables to be reused by callable workflow | ||
|
||
on: | ||
workflow_call: | ||
|
||
jobs: | ||
install-deps: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v2 | ||
name: Checkout Base | ||
|
||
- name: Restore variables | ||
uses: actions/cache/restore@v3 | ||
with: | ||
path: 'temp/variables.json' | ||
key: shared-variables-${{ github.run_id }}-${{ github.run_attempt }} | ||
|
||
- name: Set env variables | ||
uses: actions/github-script@v6 | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const sharedVariables = JSON.parse(fs.readFileSync('temp/variables.json', { encoding: 'utf8' })) | ||
Object.entries(sharedVariables).forEach(([name, value]) => { | ||
core.exportVariable(name, value); | ||
}); | ||
- name: Cache or restore node_modules | ||
id: cache-nodemodules | ||
uses: actions/cache@v3 | ||
with: | ||
path: node_modules | ||
key: ${{ env.DEPENDENCIES_CACHE_KEY }} | ||
restore-keys: ${{ env.DEPENDENCIES_RESTORE_KEY }} | ||
|
||
- name: Install dependencies | ||
if: steps.cache-nodemodules.outputs.cache-hit != 'true' | ||
run: npm ci --legacy-peer-deps |
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