Skip to content

Commit

Permalink
ci: factorize CI with reusable workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
thomvaill committed Nov 20, 2024
1 parent 56baf9b commit 3816a92
Show file tree
Hide file tree
Showing 12 changed files with 297 additions and 206 deletions.
86 changes: 18 additions & 68 deletions .github/workflows/on-merge-to-develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,88 +16,38 @@ defaults:
shell: bash

jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc # normally the current LTS
cache: yarn
cache-dependency-path: yarn.lock
- run: yarn install --frozen-lockfile

# TODO: make dev & test work without having to build everything (inspiration: https://github.com/Izhaki/mono.ts)
# - run: yarn typescript
- run: yarn build
- run: yarn format
- run: yarn lint
quality-checks:
uses: ./.github/workflows/reusable-quality-checks.yml

load-nodejs-supported-versions:
runs-on: ubuntu-latest
outputs:
node_versions: ${{ steps.get-versions.outputs.node_versions }}
steps:
- name: Get supported Node.js versions from JSON
id: get-versions
run: |
node_versions=$(cat .github/supported_nodejs_versions.json | jq -r '.versions | @json')
echo "::set-output name=node_versions::$node_versions"
uses: ./.github/workflows/reusable-load-nodejs-supported-versions.yml

# We test the current LTS on all OSes and other supported versions only on Ubuntu for performance reasons
# All the other os/version tuples will be tested in the post-release job
# We test the current LTS on all OSes and other supported versions are only tested on Ubuntu for performance reasons
# All the other os/version tuples will be tested in the post-release E2E workflow
tests-per-nodejs-version:
needs: load-nodejs-supported-versions
strategy:
matrix:
node-version: ${{ fromJson(needs.load-supported-versions.outputs.node_versions) }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch all history to make Jest snapshot tests work
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: yarn
cache-dependency-path: yarn.lock
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
- name: E2E tests
run: |
yarn link-cli
echo "$(yarn global bin)" >> $GITHUB_PATH
yarn e2e
uses: ./.github/workflows/reusable-tests.yml
with:
os: ubuntu-latest
node-version: ${{ matrix.node-version }}

tests-per-other-os:
tests-LTS-per-other-os:
strategy:
matrix:
os: [windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch all history to make Jest snapshot tests work
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc # normally the current LTS
cache: yarn
cache-dependency-path: yarn.lock
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
- name: E2E tests
run: |
yarn link-cli
echo "$(yarn global bin)" >> $GITHUB_PATH
yarn e2e
uses: ./.github/workflows/reusable-tests.yml
with:
os: ${{ matrix.os }}
node-version-file: .nvmrc # current LTS

publish-pages:
needs:
- quality
- quality-checks
- tests-per-nodejs-version
- tests-per-other-os
- tests-LTS-per-other-os
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -106,7 +56,7 @@ jobs:
fetch-depth: 0 # required by Log4brains to work correctly (needs the whole Git history)
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc # normally the current LTS
node-version-file: .nvmrc # current LTS
cache: yarn
cache-dependency-path: yarn.lock
- run: yarn install --frozen-lockfile
Expand Down Expand Up @@ -139,7 +89,7 @@ jobs:
run: git fetch --depth=1 origin +refs/tags/*:refs/tags/* || true
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc # normally the current LTS
node-version-file: .nvmrc # current LTS
cache: yarn
cache-dependency-path: yarn.lock
registry-url: https://registry.npmjs.org/ # needed by lerna to push to NPM
Expand Down
31 changes: 16 additions & 15 deletions .github/workflows/on-release-tag-alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,23 @@ on:
tags:
- v*-alpha.*

defaults:
run:
shell: bash

jobs:
get-log4brains-version:
uses: ./.github/workflows/reusable-get-log4brains-version.yml

wait-for-npm-version-to-be-published:
needs: get-log4brains-version
uses: ./.github/workflows/reusable-wait-for-npm-version-to-be-published.yml
with:
npm-package: log4brains
npm-version: ${{ needs.get-log4brains-version.outputs.version }}

build-and-push:
needs: wait-for-npm-version-to-be-published
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -17,18 +32,4 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push the latest alpha Docker image
run: |
export MDF_BRANCH_TAG=alpha
i=0
while ! make -C docker release version-tag branch-tag
do
if [[ ${i} -gt 20 ]]
then
echo "Failure for more than 10 minutes -> abort"
exit 1
fi
echo "Maybe the package is not available yet on NPM. Let's wait 30s..."
sleep 30
((i=i+1))
done
run: MDF_BRANCH_TAG=alpha make -C docker release version-tag branch-tag
67 changes: 25 additions & 42 deletions .github/workflows/on-release-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,35 @@ on:
tags:
- v*

defaults:
run:
shell: bash

jobs:
get-log4brains-version:
uses: ./.github/workflows/reusable-get-log4brains-version.yml

wait-for-npm-version-to-be-published:
needs: get-log4brains-version
uses: ./.github/workflows/reusable-wait-for-npm-version-to-be-published.yml
with:
npm-package: log4brains
npm-version: ${{ needs.get-log4brains-version.outputs.version }}

load-nodejs-supported-versions:
runs-on: ubuntu-latest
outputs:
node_versions: ${{ steps.get-versions.outputs.node_versions }}
steps:
- name: Get supported Node.js versions from JSON
id: get-versions
run: |
node_versions=$(cat .github/supported_nodejs_versions.json | jq -r '.versions | @json')
echo "::set-output name=node_versions::$node_versions"
uses: ./.github/workflows/reusable-load-nodejs-supported-versions.yml

e2e:
needs: load-nodejs-supported-versions
e2e-tests:
needs:
- load-nodejs-supported-versions
- get-log4brains-version
- wait-for-npm-version-to-be-published
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
node-version: ${{ fromJson(needs.load-supported-versions.outputs.node_versions) }}
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # required by Log4brains to work correctly (needs the whole Git history)
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
- name: Run npm install
run: |
log4brains_version="$(cat lerna.json | jq -r .version)"
i=0
while ! npm install -g log4brains@${log4brains_version}
do
if [[ ${i} -gt 20 ]]
then
echo "Failure for more than 10 minutes -> abort"
exit 1
fi
echo "Maybe the package is not available yet on NPM. Let's wait 30s..."
sleep 30
((i=i+1))
done
- name: Tests # TODO: test the preview and add some assertions :-) For now we just check the exit codes. Or manage to run e2e-tests/e2e-launcher.js from here
run: |
log4brains --version
log4brains init --defaults
log4brains adr list
log4brains build
uses: ./.github/workflows/reusable-e2e-tests.yml
with:
npm-package-fullname: log4brains@${{ needs.get-log4brains-version.outputs.version }}
os: ${{ matrix.os }}
node-version: ${{ matrix.node-version }}
65 changes: 10 additions & 55 deletions .github/workflows/pull-request-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,17 @@ defaults:
shell: bash

jobs:
quality:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc # normally the current LTS
cache: yarn
cache-dependency-path: yarn.lock
- run: yarn install --frozen-lockfile
quality-checks:
uses: ./.github/workflows/reusable-quality-checks.yml

# TODO: make dev & test work without having to build everything (inspiration: https://github.com/Izhaki/mono.ts)
# - run: yarn typescript
- run: yarn build
- run: yarn format
- run: yarn lint

tests-per-os:
# We only test the LTS on all OSes for performance reasons
# Other versions will be tested on the main workflow after the merge
tests-LTS-per-os:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # fetch all history to make Jest snapshot tests work
- uses: actions/setup-node@v4
with:
node-version-file: .nvmrc # normally the current LTS; we test other versions in the main Build workflow because it's too slow
cache: yarn
cache-dependency-path: yarn.lock
- run: yarn install --frozen-lockfile

- name: Check for yarn.lock changes
id: yarn-lock
run: git diff --quiet origin/develop HEAD -- yarn.lock
continue-on-error: true
# - steps.yarn-lock.outcome == 'success' --> yarn.lock was not changed
# - steps.yarn-lock.outcome == 'failure' --> yarn.lock was changed

# We have to build all the packages before the tests
# Because init-log4brains's integration tests use @log4brains/cli, which uses @log4brains/core
# TODO: we should separate tests that require built packages of the others, to get a quicker feedback
# Once it's done, we should add "yarn test" in each package's preVersion script
- run: yarn build

- name: Test changed packages
if: ${{ steps.yarn-lock.outcome == 'success' }}
run: yarn test --since origin/develop

- name: Test all packages (because yarn.lock has changed)
if: ${{ steps.yarn-lock.outcome == 'failure' }}
run: yarn test

- name: E2E tests
run: |
yarn link-cli
echo "$(yarn global bin)" >> $GITHUB_PATH
yarn e2e
uses: ./.github/workflows/reusable-tests.yml
with:
only-changed-packages: true # for performance
os: ${{ matrix.os }}
node-version-file: .nvmrc # current LTS
42 changes: 42 additions & 0 deletions .github/workflows/reusable-e2e-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
on:
workflow_call:
inputs:
npm-package-fullname:
required: true
type: string
os:
required: true
type: string
# Set only one of these two inputs:
node-version:
required: false
type: string
node-version-file:
required: false
type: string

defaults:
run:
shell: bash

jobs:
e2e-tests:
runs-on: ${{ inputs.os }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # required by Log4brains to work correctly (needs the whole Git history)
- uses: actions/setup-node@v4
with:
node-version-file: ${{ inputs.node-version-file }}
node-version: ${{ inputs.node-version }}

- run: npm install -g ${{ inputs.npm-package-fullname }}

# TODO: test the preview and add some assertions :-) For now we just check the exit codes. Or manage to run e2e-tests/e2e-launcher.js from here
- name: Tests
run: |
log4brains --version
log4brains init --defaults
log4brains adr list
log4brains build
22 changes: 22 additions & 0 deletions .github/workflows/reusable-get-log4brains-version.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
on:
workflow_call:
outputs:
version:
description: "Log4brains current version"
value: ${{ jobs.get-log4brains-version.outputs.version }}

defaults:
run:
shell: bash

jobs:
get-log4brains-version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get-version.outputs.version }}
steps:
- name: Get log4brains version from lerna.json
id: get-version
run: |
version="$(cat lerna.json | jq -r .version)"
echo "::set-output name=version::$version"
Loading

0 comments on commit 3816a92

Please sign in to comment.