Skip to content

Commit

Permalink
⚡ Feat: Add nx distributed cloud execution
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Cory authored and Will Cory committed Nov 19, 2023
1 parent 17b6ccf commit b5622aa
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 7 deletions.
6 changes: 0 additions & 6 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ runs:
with:
version: nightly

# Workaround for parallel building with forge:
# https://github.com/foundry-rs/foundry/issues/4736
- name: Setup forge hack
shell: bash
run: forge build

- name: Get pnpm store directory
shell: bash
run: |
Expand Down
149 changes: 149 additions & 0 deletions .github/workflows/nx.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
name: CI
on:
push:
branches:
- main
pull_request:
workflow_dispatch:

env:
NX_CLOUD_DISTRIBUTED_EXECUTION: true
NX_CLOUD_DISTRIBUTED_EXECUTION_AGENT_COUNT: 3
NX_BRANCH: ${{ github.event.number || github.ref_name }}
NX_CLOUD_ACCESS_TOKEN: ${{ secrets.NX_CLOUD_ACCESS_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

jobs:
main:
name: Nx Cloud - Main Job
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
name: Checkout [Pull Request]
if: ${{ github.event_name == 'pull_request' }}
with:
ref: ${{ github.event.pull_request.head.sha }}
fetch-depth: 0

- uses: actions/checkout@v4
name: Checkout [Default Branch]
if: ${{ github.event_name != 'pull_request' }}
with:
fetch-depth: 0

- uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
registry-url: https://registry.npmjs.org

- name: Set up Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.9

- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 8.10.2
run_install: false

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
shell: bash

- name: Check out the default branch
run: git branch --track main origin/main

- name: Initialize the Nx Cloud distributed CI run and stop agents when the build tasks are done
run: bunx nx-cloud start-ci-run --stop-agents-after=build

- name: Run commands in parallel
run: |
pids=()
# list of commands to be run on main has env flag NX_CLOUD_DISTRIBUTED_EXECUTION set to false
NX_CLOUD_DISTRIBUTED_EXECUTION=false bunx nx-cloud record -- bunx concurrently 'pnpm i' 'bun lint' 'bun sort-package-json' & pids+=($!)
# list of commands to be run on agents
bunx nx affected -t generate:docs,build:types,typecheck,test:coverage,build:dist --parallel=3 &
pids+=($!)
# run all commands in parallel and bail if one of them fails
for pid in ${pids[*]}; do
if ! wait $pid; then
exit 1
fi
done
exit 0
agents:
name: Agent ${{ matrix.agent }}
runs-on: ubuntu-latest
strategy:
matrix:
# Add more agents here as your repository expands
agent: [1, 2, 3]
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: actions/setup-node@v3
with:
node-version-file: ".nvmrc"
registry-url: https://registry.npmjs.org

- name: Set up Bun
uses: oven-sh/setup-bun@v1
with:
bun-version: 1.0.9

- uses: pnpm/action-setup@v2
name: Install pnpm
with:
version: 8.10.2
run_install: false

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Get pnpm store directory
shell: bash
run: |
echo "STORE_PATH=$(pnpm store path --silent)" >> $GITHUB_ENV
- uses: actions/cache@v3
name: Setup pnpm cache
with:
path: ${{ env.STORE_PATH }}
key: ${{ runner.os }}-pnpm-store-${{ hashFiles('**/pnpm-lock.yaml') }}
restore-keys: |
${{ runner.os }}-pnpm-store-
- name: Install dependencies
run: pnpm install --frozen-lockfile
shell: bash

- name: Start Nx Agent ${{ matrix.agent }}
run: bunx nx-cloud start-agent
env:
NX_AGENT_NAME: ${{ matrix.agent }}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"contributors": [
"Will Cory <willcory10@gmail.com>"
],
"type": "module",
"type": "commonjs",
"workspaces": [
"bundler/*",
"bundler",
Expand Down

0 comments on commit b5622aa

Please sign in to comment.