diff --git a/.github/workflows/base-build.yml b/.github/workflows/base-build.yml index f32921a..63e1ef4 100644 --- a/.github/workflows/base-build.yml +++ b/.github/workflows/base-build.yml @@ -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 @@ -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 }} diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 1b64ac2..e818e7b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -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 diff --git a/.github/workflows/define-shared-variables.yml b/.github/workflows/define-shared-variables.yml new file mode 100644 index 0000000..30a4fe0 --- /dev/null +++ b/.github/workflows/define-shared-variables.yml @@ -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 }}); diff --git a/.github/workflows/deploy-to-firebase.yml b/.github/workflows/deploy-to-firebase.yml index d6fdc5e..af5b950 100644 --- a/.github/workflows/deploy-to-firebase.yml +++ b/.github/workflows/deploy-to-firebase.yml @@ -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 diff --git a/.github/workflows/install-dependencies.yml b/.github/workflows/install-dependencies.yml new file mode 100644 index 0000000..e7d303b --- /dev/null +++ b/.github/workflows/install-dependencies.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/pr.yml b/.github/workflows/pr.yml index a05bc13..268e0f8 100644 --- a/.github/workflows/pr.yml +++ b/.github/workflows/pr.yml @@ -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