Skip to content

Commit

Permalink
chore: update workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
Bielik20 committed Oct 29, 2022
1 parent 936f2c6 commit dea6c8c
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 128 deletions.
89 changes: 89 additions & 0 deletions .github/actions/run-commands-in-parallel/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: 'Run Commands in Parallel'
description: "Based on https://github.com/nrwl/ci"

inputs:
parallel-commands:
required: false
type: string
parallel-commands-on-agents:
required: false
type: string

runs:
using: 'composite'
steps:
# An unfortunate side-effect of the way reusable workflows work is that by the time they are pulled into the "caller"
# repo, they are effectively completely embedded in that context. This means that we cannot reference any files which
# are local to this repo which defines the workflow, and we therefore need to work around this by embedding the contents
# of the shell utilities for executing commands into the workflow directly.
- name: Create command utils
uses: actions/github-script@v6
with:
script: |
const { writeFileSync } = require('fs');
const runCommandsInParallelScript = `
# Extract the provided commands from the stringified JSON array.
IFS=$'\n' read -d '' -a userCommands < <((jq -c -r '.[]') <<<"$1")
# Invoke the provided commands in parallel and collect their exit codes.
pids=()
for userCommand in "\${userCommands[@]}"; do
eval "$userCommand" & pids+=($!)
done
# If any one of the invoked commands exited with a non-zero exit code, exit the whole thing with code 1.
for pid in \${pids[*]}; do
if ! wait $pid; then
exit 1
fi
done
# All the invoked commands must have exited with code zero.
exit 0
`;
writeFileSync('./.github/workflows/run-commands-in-parallel.sh', runCommandsInParallelScript);
- name: Prepare command utils
shell: bash
# We need to escape the workspace path to be consistent cross-platform: https://github.com/actions/runner/issues/1066
run: chmod +x ${GITHUB_WORKSPACE//\\//}/.github/workflows/run-commands-in-parallel.sh

- name: Process parallel commands configuration
uses: actions/github-script@v6
id: parallel_commands_config
env:
PARALLEL_COMMANDS: ${{ inputs.parallel-commands }}
PARALLEL_COMMANDS_ON_AGENTS: ${{ inputs.parallel-commands-on-agents }}
with:
# For the ones configured for main, explicitly set NX_CLOUD_DISTRIBUTED_EXECUTION to false, taking into account commands chained with &&
# within the strings. In order to properly escape single quotes we need to do some manual replacing and escaping so that the commands
# are forwarded onto the run-commands-in-parallel.sh script appropriately.
script: |
const parallelCommandsOnMainStr = process.env.PARALLEL_COMMANDS || '';
const parallelCommandsOnAgentsStr = process.env.PARALLEL_COMMANDS_ON_AGENTS || '';
const parallelCommandsOnMain = parallelCommandsOnMainStr
.split('\n')
.map(command => command.trim())
.filter(command => command.length > 0)
.map(s => s.replace(/'/g, '%27'));
const parallelCommandsOnAgents = parallelCommandsOnAgentsStr
.split('\n')
.map(command => command.trim())
.filter(command => command.length > 0)
.map(s => s.replace(/'/g, '%27'));
const formattedArrayOfCommands = [
...parallelCommandsOnMain.map(s => s
.split(' && ')
.map(s => `NX_CLOUD_DISTRIBUTED_EXECUTION=false ${s}`)
.join(' && ')
),
...parallelCommandsOnAgents,
];
const stringifiedEncodedArrayOfCommands = JSON.stringify(formattedArrayOfCommands)
.replace(/%27/g, "'\\''");
return stringifiedEncodedArrayOfCommands
result-encoding: string

- name: Run any configured parallel commands on main and agent jobs
shell: bash
# We need to escape the workspace path to be consistent cross-platform: https://github.com/actions/runner/issues/1066
run: ${GITHUB_WORKSPACE//\\//}/.github/workflows/run-commands-in-parallel.sh '${{ steps.parallel_commands_config.outputs.result }}'
env:
NX_CLOUD_DISTRIBUTED_EXECUTION: true
33 changes: 33 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: 'Setup'
description: 'Run basic setup'

inputs:
affected:
required: false
default: false
type: boolean

runs:
using: 'composite'
steps:
- name: Cache Dependencies
id: cache
uses: actions/cache@v3
with:
path: |
node_modules
tools/src/**/*.js
key: dependencies-${{ runner.os }}-${{ hashFiles('yarn.lock', 'tools/src/**/*.ts') }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Install
if: steps.cache.outputs.cache-hit != 'true'
shell: bash
run: yarn
- name: Derive appropriate SHAs for base and head for `nx affected` commands
if: ${{ inputs.affected == 'true' }}
uses: nrwl/nx-set-shas@v3
with:
main-branch-name: 'master'
132 changes: 27 additions & 105 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,123 +1,45 @@
name: Check
on:
pull_request:
env:
NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT: 4
NX_CLOUD_DISTRIBUTED_EXECUTION: false

jobs:
install:
name: Install Dependencies
controller:
name: Controller
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Cache Dependencies
id: cache
uses: actions/cache@v3
with:
path: |
node_modules
tools/src/**/*.js
key: dependencies-${{ runner.os }}-${{ hashFiles('yarn.lock', 'tools/src/**/*.ts') }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Execute
if: steps.cache.outputs.cache-hit != 'true'
run: yarn
- name: Set Variables
id: variables
run: yarn script set-pr-variables
- name: Set e2e
id: set-e2e
run: yarn script set-affected --target e2e --base ${{ steps.variables.outputs.base }}
- name: Set lint
id: set-lint
run: yarn script set-affected --target lint --base ${{ steps.variables.outputs.base }}
- name: Set test
id: set-test
run: yarn script set-affected --target test --base ${{ steps.variables.outputs.base }}
outputs:
e2e-array: ${{ steps.set-e2e.outputs.array }}
lint-comma: ${{ steps.set-lint.outputs.comma-separated }}
test-comma: ${{ steps.set-test.outputs.comma-separated }}
- name: Setup
uses: ./.github/actions/setup
with:
affected: true
- run: npx nx-cloud start-ci-run
- name: Run Commands
uses: ./.github/actions/run-commands-in-parallel
with:
parallel-commands-on-agents: |
npx nx affected --target=e2e --parallel=1
npx nx affected --target=lint --parallel=3 --silent
npx nx affected --target=test --parallel=3 --quiet
- run: npx nx-cloud stop-all-agents
if: ${{ always() }}

lint:
name: Lint
needs: install
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Load Dependencies
uses: actions/cache@v3
with:
path: |
node_modules
tools/src/**/*.js
key: dependencies-${{ runner.os }}-${{ hashFiles('yarn.lock', 'tools/src/**/*.ts') }}
- name: Execute
if: ${{ needs.install.outputs.lint-comma }}
run: npm run nx -- run-many --projects ${{ needs.install.outputs.lint-comma }} --target lint --parallel -- --quiet

test:
name: Test
needs: install
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Load Dependencies
uses: actions/cache@v3
with:
path: |
node_modules
tools/src/**/*.js
key: dependencies-${{ runner.os }}-${{ hashFiles('yarn.lock', 'tools/src/**/*.ts') }}
- name: Execute
if: ${{ needs.install.outputs.test-comma }}
run: yarn nx run-many --projects ${{ needs.install.outputs.test-comma }} --target test --parallel

e2e:
name: E2E
needs: [install]
agents:
name: Agent
runs-on: ubuntu-latest
timeout-minutes: 60
strategy:
fail-fast: false
matrix:
project: ${{ fromJson(needs.install.outputs.e2e-array) }}
agent: [1, 2, 3, 4]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Load Dependencies
uses: actions/cache@v3
with:
path: |
node_modules
tools/src/**/*.js
key: dependencies-${{ runner.os }}-${{ hashFiles('yarn.lock', 'tools/src/**/*.ts') }}
- name: Execute
run: yarn nx run ${{ matrix.project }}:e2e

e2e-done:
name: E2E
needs: [install, e2e]
if: ${{ always() }}
runs-on: ubuntu-latest
steps:
- name: Check status
if: ${{ needs.e2e.result != 'success' && !contains(needs.install.outputs.e2e-array, '[]') }}
run: exit 1
- name: Setup
uses: ./.github/actions/setup
- name: Start Nx Agent ${{ matrix.agent }}
run: npx nx-cloud start-agent
21 changes: 5 additions & 16 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,14 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Cache Dependencies
id: cache
uses: actions/cache@v3
with:
path: |
node_modules
tools/src/**/*.js
key: dependencies-${{ runner.os }}-${{ hashFiles('yarn.lock', 'tools/src/**/*.ts') }}
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- name: Install Dependencies
if: steps.cache.outputs.cache-hit != 'true'
run: yarn
- name: Setup
uses: ./.github/actions/setup
- name: Start Release
id: semantic
run: npx semantic-release --dry-run
- name: Build
run: yarn nx affected --target build --parallel 3 --base ${{ steps.semantic.outputs.last-head || 'HEAD' }}
- name: Publish
run: yarn nx affected --target publish --parallel --base ${{ steps.semantic.outputs.last-head || 'HEAD' }} --pkgVersion ${{ steps.semantic.outputs.next-version }} --tag ${{ steps.semantic.outputs.channel }}
run: yarn nx affected --target publish --parallel 3 --base ${{ steps.semantic.outputs.last-head || 'HEAD' }} --pkgVersion ${{ steps.semantic.outputs.next-version }} --tag ${{ steps.semantic.outputs.channel }}
- name: Finish Release
run: npx semantic-release
2 changes: 1 addition & 1 deletion e2e/nx-core-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": {
"executor": "nx:run-commands",
"options": {
"command": "nx run nx-core:build --with-deps"
"command": "nx run nx-core:build"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/nx-jest-playwright-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": {
"executor": "nx:run-commands",
"options": {
"command": "nx run nx-jest-playwright:build --with-deps"
"command": "nx run nx-jest-playwright:build"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/nx-npm-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": {
"executor": "nx:run-commands",
"options": {
"command": "nx run nx-npm:build --with-deps"
"command": "nx run nx-npm:build"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion e2e/nx-serverless-e2e/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"prepare": {
"executor": "nx:run-commands",
"options": {
"command": "nx run nx-serverless:build --with-deps"
"command": "nx run nx-serverless:build"
}
}
},
Expand Down
2 changes: 1 addition & 1 deletion nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"targetDefaults": {
"publish": {
"dependsOn": ["build", "^publish"],
"dependsOn": ["^publish"],
"inputs": ["production", "^production"]
},
"build": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
run: yarn script set-push-variables
- name: Set build
id: set-build
run: yarn script set-affected --target build --base ${{ steps.variables.outputs.base }} --with-deps
run: yarn script set-affected --target build --base ${{ steps.variables.outputs.base }}
- name: Set e2e
id: set-e2e
run: yarn script set-affected --target e2e --base ${{ steps.variables.outputs.base }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
- name: Set publish
id: set-publish
# We only want to build publishable libs, hence target publish
run: yarn script set-affected --target publish --base ${{ steps.semantic.outputs.last-head || 'HEAD' }} --with-deps
run: yarn script set-affected --target publish --base ${{ steps.semantic.outputs.last-head || 'HEAD' }}
outputs:
next-version: ${{ steps.semantic.outputs.next-version || '0.0.0' }}
publish-comma: ${{ steps.set-publish.outputs.comma-separated }}
Expand Down

0 comments on commit dea6c8c

Please sign in to comment.