Skip to content

Commit

Permalink
Improve CI speed, add nx (segmentio#1651)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Oct 25, 2023
1 parent 0cf6c0a commit b3127ae
Show file tree
Hide file tree
Showing 19 changed files with 241 additions and 78 deletions.
184 changes: 122 additions & 62 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,116 +7,175 @@ on:
pull_request:

jobs:
test-and-build:
test:
name: Unit tests
runs-on: ubuntu-20.04

timeout-minutes: 30

strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v2
# See nx recipe: https://nx.dev/recipes/ci/monorepo-ci-github-actions
- uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0 # nx recipe

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: yarn

- name: Get yarn cache directory path
id: yarn-cache-dir-path
run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Use Github Personal Access Token
run: git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf ssh://[email protected]/

- uses: actions/cache@v2
id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
- uses: nrwl/nx-set-shas@v3 # nx recipe

- name: Install Dependencies
run: yarn install --frozen-lockfile
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Build (Affected)
run: NODE_ENV=production yarn nx affected -t build --parallel=3 # nx recipe

- name: Test (Affected)
run: yarn nx affected -t test --parallel=3 # nx recipe

lint:
name: Lint
runs-on: ubuntu-20.04
timeout-minutes: 20
strategy:
matrix:
node-version: [18.x]

steps:
- uses: actions/checkout@v3
with:
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
restore-keys: |
${{ runner.os }}-yarn-
persist-credentials: false
fetch-depth: 0 # nx recipe

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: yarn

- name: Use Github Personal Access Token
run: git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf ssh://[email protected]/

- uses: nrwl/nx-set-shas@v3 # nx recipe

- name: Install Dependencies
run: yarn install --frozen-lockfile
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Build
run: NODE_ENV=production yarn build
- name: Build # TODO: This monorepo should be refactored so packages can be linted invidually. "affected" will not work ATM.
run: NODE_ENV=production yarn build # nx recipe

- name: Lint
env:
NODE_OPTIONS: '--max-old-space-size=4096'
run: yarn lint

- name: Validate
run: yarn validate
validate:
name: Validate
runs-on: ubuntu-20.04
timeout-minutes: 20
strategy:
matrix:
node-version: [18.x]

- name: Test
run: yarn test
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false
fetch-depth: 0 # nx recipe

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: yarn

- name: Use Github Personal Access Token
run: git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf ssh://[email protected]/

- uses: nrwl/nx-set-shas@v3 # nx recipe

- name: Install Dependencies
run: yarn install --frozen-lockfile
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Assert yarn.lock is up-to-date
run: bash scripts/assert-lockfile-updated.sh

- name: Build # TODO: This monorepo should be refactored so packages can be linted invidually. "affected" will not work ATM.
run: NODE_ENV=production yarn build # nx recipe

- name: Validate Definitions
run: yarn validate

- name: destination-subscriptions size
- name: Destination Subscription Size
run: |
if $(lerna changed | grep -q destination-subscriptions); then
yarn subscriptions size
fi
# browser-tests-destination:
# # env: # Disable saucelabs - we blew through our quota.
# # SAUCE_USERNAME: ${{secrets.SAUCE_USERNAME}}
# # SAUCE_ACCESS_KEY: ${{secrets.SAUCE_ACCESS_KEY}}
- name: Assert generated types are up-to-date
run: bash scripts/assert-types-updated.sh

# runs-on: ubuntu-20.04

# timeout-minutes: 20

# strategy:
# matrix:
# node-version: [18.x]

# steps:
# - uses: actions/checkout@master
browser-destination-bundle-qa:
name: Browser Destination Bundle QA
# env: # Disable saucelabs - we blew through our quota.
# SAUCE_USERNAME: ${{secrets.SAUCE_USERNAME}}
# SAUCE_ACCESS_KEY: ${{secrets.SAUCE_ACCESS_KEY}}
runs-on: ubuntu-20.04
timeout-minutes: 20
strategy:
matrix:
node-version: [18.x]

# - name: Use Node.js ${{ matrix.node-version }}
# uses: actions/setup-node@v2
# with:
# node-version: ${{ matrix.node-version }}
# registry-url: 'https://registry.npmjs.org'
steps:
- uses: actions/checkout@v3
with:
persist-credentials: false

# - name: Get yarn cache directory path
# id: yarn-cache-dir-path
# run: echo "::set-output name=dir::$(yarn cache dir)"
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
registry-url: 'https://registry.npmjs.org'
cache: yarn

# - uses: actions/cache@v2
# id: yarn-cache # use this to check for `cache-hit` (`steps.yarn-cache.outputs.cache-hit != 'true'`)
# with:
# path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
# key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
# restore-keys: |
# ${{ runner.os }}-yarn-
- name: Use Github Personal Access Token
run: git config --global url."https://${{ secrets.GH_PAT }}@github.com/".insteadOf ssh://[email protected]/

# - name: Install Dependencies
# run: yarn install --frozen-lockfile
# env:
# NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
- name: Install Dependencies
run: yarn install --frozen-lockfile
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

# - name: Build
# run: NODE_ENV=production yarn build:browser-destinations && yarn browser build-web
- name: Build
run: NODE_ENV=production yarn build:browser-bundles

# - name: Run Saucelabs Tests
# working-directory: packages/browser-destinations-integration-tests
# shell: bash
# run: |
# yarn start-destination-server &
# yarn test:sauce
# - name: Run Saucelabs Tests
# working-directory: packages/browser-destinations-integration-tests
# shell: bash
# run: |
# yarn start-destination-server &
# yarn test:sauce

browser-tests-core:
name: 'Browser tests: actions-core'
runs-on: ubuntu-20.04

timeout-minutes: 10
Expand Down Expand Up @@ -164,6 +223,7 @@ jobs:
run: yarn test-browser

snyk:
name: Snyk
runs-on: ubuntu-20.04

timeout-minutes: 5
Expand Down
1 change: 1 addition & 0 deletions .nxignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
packages/cli-internal
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ For more detailed instruction, see the following READMEs:

### Local development

This is a monorepo with multiple packages leveraging [`lerna`](https://github.com/lerna/lerna) with [Yarn Workspaces](https://classic.yarnpkg.com/en/docs/workspaces):
This is a monorepo with multiple packages leveraging:

- [`lerna`](https://github.com/lerna/lerna) for publishing
- [`nx`](https://nx.dev) for dependency-tree aware building, linting, testing, and caching (migration away from `lerna` in progress!).
- [Yarn Workspaces](https://classic.yarnpkg.com/en/docs/workspaces) for package symlinking and hoisting.

Structure:

- `packages/ajv-human-errors` - a wrapper around [AJV](https://ajv.js.org/) errors to produce more friendly validation messages
- `packages/browser-destinations` - destination definitions that run on device via Analytics 2.0
Expand Down Expand Up @@ -66,9 +72,8 @@ yarn login

# Requires node 18.12.1, optionally: nvm use 18.12.1
yarn --ignore-optional
yarn bootstrap
yarn build
yarn install
yarn build

# Run unit tests to ensure things are working! For partners who don't have access to internal packages, you can run:
yarn test-partners
Expand Down
2 changes: 1 addition & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export NODE_ENV=test
## Code Coverage
Code coverage is automatically collected upon completion of `yarn test`. Results may be inspected by examining the HTML report found at `coverage/lcov-report/index.html`, or directly in your IDE if _lcov_ is supported.
Code coverage is collected upon completion of `yarn test --coverage`. Results may be inspected by examining the HTML report found at `coverage/lcov-report/index.html`, or directly in your IDE if _lcov_ is supported.
## Post Deployment Change Testing
Expand Down
37 changes: 37 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"$schema": "./node_modules/nx/schemas/nx-schema.json",
"namedInputs": {
"sharedGlobals": ["{workspaceRoot}/nx.json", "{workspaceRoot}/tsconfig.json"],
"default": ["{projectRoot}/**/*", "sharedGlobals"],
"production": [
"default",
"{projectRoot}/tsconfig.json",
"{projectRoot}/tsconfig.build.json",
"{projectRoot}/webpack.config*",
"{projectRoot}/babel.config*",
"!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
"!{projectRoot}/**/test/**/*"
]
},
"tasksRunnerOptions": {
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": ["build", "test"]
}
}
},
"targetDefaults": {
"build": {
"inputs": ["production", "^production"],
"dependsOn": ["^build"]
},
"test": {
"inputs": ["default", "^production"],
"dependsOn": ["build"]
}
},
"affected": {
"defaultBase": "main"
}
}
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"cli-internal": "yarn workspace @segment/actions-cli-internal",
"core": "yarn workspace @segment/actions-core",
"bootstrap": "lerna bootstrap",
"build": "./bin/run generate:types && lerna run build --stream --ignore @segment/actions-cli-internal && yarn browser build-web",
"build:browser-destinations": "yarn lerna run build --scope=@segment/destinations-manifest --include-dependencies --stream && yarn browser build-web",
"build": "nx run-many -t build",
"build:browser-bundles": "nx build @segment/destinations-manifest && nx build-web @segment/browser-destinations",
"types": "./bin/run generate:types",
"validate": "./bin/run validate",
"lint": "ls -d ./packages/* | xargs -I {} eslint '{}/**/*.ts' --cache",
"subscriptions": "yarn workspace @segment/destination-subscriptions",
"test": "lerna run test --stream",
"test": "nx run-many -t test",
"test-partners": "lerna run test --stream --ignore @segment/actions-core --ignore @segment/actions-cli --ignore @segment/ajv-human-errors",
"test-browser": "bash scripts/test-browser.sh",
"typecheck": "lerna run typecheck --stream",
Expand Down Expand Up @@ -68,7 +68,7 @@
"prettier": "^2.4.1",
"process": "^0.11.10",
"timers-browserify": "^2.0.12",
"ts-jest": "^27.0.0",
"ts-jest": "^27.0.7",
"ts-node": "^9.1.1",
"typescript": "4.3.5",
"ws": "^8.5.0"
Expand Down
5 changes: 5 additions & 0 deletions packages/actions-shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
},
"jest": {
"preset": "ts-jest",
"globals": {
"ts-jest": {
"isolatedModules": true
}
},
"testEnvironment": "node",
"modulePathIgnorePatterns": [
"<rootDir>/dist/"
Expand Down
5 changes: 5 additions & 0 deletions packages/browser-destination-runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@
},
"jest": {
"preset": "ts-jest",
"globals": {
"ts-jest": {
"isolatedModules": true
}
},
"testEnvironment": "node",
"modulePathIgnorePatterns": [
"<rootDir>/dist/"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"test:sauce": "wdio wdio.conf.sauce.ts",
"test:local": "wdio wdio.conf.local.ts",
"start-destination-server": "yarn ts-node src/server/start-destination-server.ts",
"browser-destinations:build": "NODE_ENV=production yarn lerna run build --scope=@segment/browser-destinations --include-dependencies --stream"
"browser-destinations:build": "NODE_ENV=production yarn nx build-web @segment/browser-destinations"
},
"devDependencies": {
"@wdio/cli": "^7.26.0",
Expand Down
Loading

0 comments on commit b3127ae

Please sign in to comment.