Skip to content

Commit

Permalink
Add sharing env variables across callable workflows (#82)
Browse files Browse the repository at this point in the history
* 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
skynetigor authored Jul 8, 2023
1 parent 1ad144f commit 2c64bb5
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 62 deletions.
91 changes: 41 additions & 50 deletions .github/workflows/base-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,58 +13,41 @@ on:
type: string
default: ''

jobs:
install-deps:
jobs:
validate:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
name: Checkout Base

- name: Cache or restore node_modules
id: cache-nodemodules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
- name: Restore variables
uses: actions/cache/restore@v3
with:
# caching node_modules
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
path: 'temp/variables.json'
key: shared-variables-${{ github.run_id }}-${{ github.run_attempt }}

- name: Install dependencies
if: steps.cache-nodemodules.outputs.cache-hit != 'true'
run: npm ci --legacy-peer-deps

validate:
needs: [install-deps]
runs-on: ubuntu-latest
- 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);
});
steps:
- uses: actions/checkout@v2
name: Checkout Base

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 14.18.0
node-version: ${{ env.NODE_VERSION }}

- name: Cache or restore node_modules
id: cache-nodemodules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
- name: Restore node_modules
uses: actions/cache/restore@v3
with:
# caching node_modules
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
key: ${{ env.DEPENDENCIES_CACHE_KEY }}
restore-keys: ${{ env.DEPENDENCIES_RESTORE_KEY }}

- name: Lint
run: npx nx lint ${{ inputs.appName }} --quiet
Expand All @@ -73,31 +56,39 @@ jobs:
run: npx nx test ${{ inputs.appName }} --configuration=single-run

build:
needs: [install-deps]
runs-on: ubuntu-latest

steps:
- 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);
});
- uses: actions/checkout@v2
name: Checkout Base

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 14.18.0
node-version: ${{ env.NODE_VERSION }}

- name: Cache or restore node_modules
id: cache-nodemodules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
- name: Restore node_modules
uses: actions/cache/restore@v3
with:
# caching node_modules
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
key: ${{ env.DEPENDENCIES_CACHE_KEY }}
restore-keys: ${{ env.DEPENDENCIES_RESTORE_KEY }}

- name: Build
run: npx nx build ${{ inputs.appName }}
Expand Down
9 changes: 9 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ concurrency:
cancel-in-progress: true

jobs:
define_shared_variables:
uses: ./.github/workflows/define-shared-variables.yml

install_dependencies:
uses: ./.github/workflows/install-dependencies.yml
needs: define_shared_variables

build_lib:
uses: ./.github/workflows/base-build.yml
needs: install_dependencies
with:
appName: dynamic-form
branch: temp

build_showcase:
uses: ./.github/workflows/base-build.yml
needs: install_dependencies
with:
appName: showcase
branch: temp
Expand Down
87 changes: 87 additions & 0 deletions .github/workflows/define-shared-variables.yml
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 }});
33 changes: 21 additions & 12 deletions .github/workflows/deploy-to-firebase.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,36 @@ jobs:
runs-on: ubuntu-latest

steps:
- 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);
});
- uses: actions/checkout@v2
name: Checkout Base

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: 14.18.0
node-version: ${{ env.NODE_VERSION }}

- name: Cache or restore node_modules
id: cache-nodemodules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
- name: Restore node_modules
uses: actions/cache/restore@v3
with:
# caching node_modules
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
key: ${{ env.DEPENDENCIES_CACHE_KEY }}
restore-keys: ${{ env.DEPENDENCIES_RESTORE_KEY }}

- name: Download app artifacts
uses: actions/download-artifact@master
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/install-dependencies.yml
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
9 changes: 9 additions & 0 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ concurrency:
cancel-in-progress: true

jobs:
define_shared_variables:
uses: ./.github/workflows/define-shared-variables.yml

install_dependencies:
uses: ./.github/workflows/install-dependencies.yml
needs: define_shared_variables

build_lib:
uses: ./.github/workflows/base-build.yml
needs: install_dependencies
with:
appName: dynamic-form
branch: temp

build_showcase:
uses: ./.github/workflows/base-build.yml
needs: install_dependencies
with:
appName: showcase
branch: temp
Expand Down

0 comments on commit 2c64bb5

Please sign in to comment.