Skip to content

Commit

Permalink
ci: chocolatey caching
Browse files Browse the repository at this point in the history
Optimised Windows CI/CD setup by internalising and caching chocolatey packages

Fixes #397
  • Loading branch information
emmacasolin committed Jul 11, 2022
1 parent f9ac03c commit 1290813
Show file tree
Hide file tree
Showing 7 changed files with 294 additions and 254 deletions.
149 changes: 73 additions & 76 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ cache:
- ./tmp/ts-node-cache/
# Homebrew cache is only used by the macos runner
- ./tmp/Homebrew
# Chocolatey cache is only used by the windows runner
- ./tmp/chocolatey/
# `jest` cache is configured in jest.config.js
- ./tmp/jest/

Expand All @@ -41,74 +43,71 @@ stages:

image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner

# check:lint:
# stage: check
# needs: []
# script:
# - >
# nix-shell --run '
# npm run lint;
# '
# rules:
# # Runs on feature and staging commits and ignores version commits
# - if: $CI_COMMIT_BRANCH =~ /^(?:feature.*|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# # Runs on tag pipeline where the tag is a prerelease or release version
# - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# # Manually run on commits other than master and ignore version commits
# - if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != 'master' && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# when: manual
check:lint:
stage: check
needs: []
script:
- >
nix-shell --run '
npm run lint;
'
rules:
# Runs on feature and staging commits and ignores version commits
- if: $CI_COMMIT_BRANCH =~ /^(?:feature.*|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Runs on tag pipeline where the tag is a prerelease or release version
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Manually run on commits other than master and ignore version commits
- if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != 'master' && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
when: manual

# check:nix-dry:
# stage: check
# needs: []
# script:
# - nix-build -v -v --dry-run ./release.nix
# rules:
# # Runs on feature and staging commits and ignores version commits
# - if: $CI_COMMIT_BRANCH =~ /^(?:feature.*|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# # Runs on tag pipeline where the tag is a prerelease or release version
# - if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# # Manually run on commits other than master and ignore version commits
# - if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != 'master' && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# when: manual
check:nix-dry:
stage: check
needs: []
script:
- nix-build -v -v --dry-run ./release.nix
rules:
# Runs on feature and staging commits and ignores version commits
- if: $CI_COMMIT_BRANCH =~ /^(?:feature.*|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Runs on tag pipeline where the tag is a prerelease or release version
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Manually run on commits other than master and ignore version commits
- if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != 'master' && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
when: manual

# check:test-generate:
# stage: check
# needs: []
# script:
# - mkdir -p ./tmp
# - >
# nix-shell --run '
# ./scripts/check:test-generate.sh > ./tmp/check:test.yml
# '
# artifacts:
# when: always
# paths:
# - ./tmp/check:test.yml
# rules:
# # Runs on staging commits and ignores version commits
# - if: $CI_COMMIT_BRANCH =~ /^feature.*$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# # Manually run on commits other than master and staging and ignore version commits
# - if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(?:master|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# when: manual
check:test-generate:
stage: check
needs: []
script:
- mkdir -p ./tmp
- ./scripts/check-test-generate.sh > ./tmp/check-test.yml
artifacts:
when: always
paths:
- ./tmp/check-test.yml
rules:
# Runs on staging commits and ignores version commits
- if: $CI_COMMIT_BRANCH =~ /^feature.*$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Manually run on commits other than master and staging and ignore version commits
- if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(?:master|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
when: manual

# check:test:
# stage: check
# needs:
# - check:test-generate
# inherit:
# variables: false
# trigger:
# include:
# - artifact: tmp/check:test.yml
# job: check:test-generate
# strategy: depend
# rules:
# # Runs on staging commits and ignores version commits
# - if: $CI_COMMIT_BRANCH =~ /^feature.*$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# # Manually run on commits other than master and staging and ignore version commits
# - if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(?:master|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# when: manual
check:test:
stage: check
needs:
- check:test-generate
inherit:
variables: false
trigger:
include:
- artifact: tmp/check-test.yml
job: check:test-generate
strategy: depend
rules:
# Runs on staging commits and ignores version commits
- if: $CI_COMMIT_BRANCH =~ /^feature.*$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Manually run on commits other than master and staging and ignore version commits
- if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(?:master|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
when: manual

build:merge:
stage: build
Expand Down Expand Up @@ -143,17 +142,16 @@ build:platforms-generate:
needs: []
script:
- mkdir -p ./tmp
- ./scripts/build:platforms-generate.sh > ./tmp/build:platforms.yml
- ./scripts/build-platforms-generate.sh > ./tmp/build-platforms.yml
artifacts:
when: always
paths:
- ./tmp/build:platforms.yml
- ./tmp/build-platforms.yml
rules:
# Runs on staging commits and ignores version commits
- if: $CI_COMMIT_BRANCH =~ /^feature.*$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Manually run on commits other than master and staging and ignore version commits
- if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(?:master|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
when: manual
- if: $CI_COMMIT_BRANCH == 'staging' && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Runs on tag pipeline where the tag is a prerelease or release version
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/

build:platforms:
stage: build
Expand All @@ -163,15 +161,14 @@ build:platforms:
variables: false
trigger:
include:
- artifact: tmp/build:platforms.yml
- artifact: tmp/build-platforms.yml
job: build:platforms-generate
strategy: depend
rules:
# Runs on staging commits and ignores version commits
- if: $CI_COMMIT_BRANCH =~ /^feature.*$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Manually run on commits other than master and staging and ignore version commits
- if: $CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH !~ /^(?:master|staging)$/ && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
when: manual
- if: $CI_COMMIT_BRANCH == 'staging' && $CI_COMMIT_TITLE !~ /^[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/
# Runs on tag pipeline where the tag is a prerelease or release version
- if: $CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+(?:-.*[0-9]+)?$/

build:dist:
stage: build
Expand Down
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@
"google-protobuf": "^3.14.0",
"ip-num": "^1.3.3-0",
"isomorphic-git": "^1.8.1",
"jest-junit": "^13.2.0",
"jose": "^4.3.6",
"lexicographic-integer": "^1.1.0",
"multiformats": "^9.4.8",
Expand Down
180 changes: 180 additions & 0 deletions scripts/build-platforms-generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
#!/usr/bin/env bash

shopt -s globstar
shopt -s nullglob

# Quote the heredoc to prevent shell expansion
cat << "EOF"
default:
interruptible: true
variables:
GH_PROJECT_PATH: "MatrixAI/${CI_PROJECT_NAME}"
GH_PROJECT_URL: "https://${GITHUB_TOKEN}@github.com/${GH_PROJECT_PATH}.git"
GIT_SUBMODULE_STRATEGY: "recursive"
# Cache .npm
NPM_CONFIG_CACHE: "./tmp/npm"
# Prefer offline node module installation
NPM_CONFIG_PREFER_OFFLINE: "true"
# `ts-node` has its own cache
# It must use an absolute path, otherwise ts-node calls will CWD
TS_CACHED_TRANSPILE_CACHE: "${CI_PROJECT_DIR}/tmp/ts-node-cache"
TS_CACHED_TRANSPILE_PORTABLE: "true"
# Homebrew cache only used by macos runner
HOMEBREW_CACHE: "${CI_PROJECT_DIR}/tmp/Homebrew"
# Cached directories shared between jobs & pipelines per-branch per-runner
cache:
key: $CI_COMMIT_REF_SLUG
when: 'always'
paths:
- ./tmp/npm/
- ./tmp/ts-node-cache/
# Homebrew cache is only used by the macos runner
- ./tmp/Homebrew
# Chocolatey cache is only used by the windows runner
- ./tmp/chocolatey/
# `jest` cache is configured in jest.config.js
- ./tmp/jest/
stages:
- build # Cross-platform library compilation, unit tests
image: registry.gitlab.com/matrixai/engineering/maintenance/gitlab-runner
EOF

printf "\n"

# Each test directory has its own job
for test_dir in tests/**/*/; do
test_files=("$test_dir"*.test.ts)
if [ ${#test_files[@]} -eq 0 ]; then
continue
fi
# Remove trailing slash
test_dir="${test_dir%\/}"
# Remove `tests/` prefix
test_dir="${test_dir#*/}"
cat << EOF
build:linux $test_dir:
stage: build
needs: []
script:
- >
nix-shell --run '
npm test -- --ci --coverage --runInBand ${test_files[@]};
'
artifacts:
when: always
reports:
junit:
- ./tmp/junit/junit.xml
coverage_report:
coverage_format: cobertura
path: ./tmp/coverage/cobertura-coverage.xml
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
EOF
printf "\n"
done

# All top-level test files are accumulated into 1 job
test_files=(tests/*.test.ts)
cat << EOF
build:linux index:
stage: build
needs: []
script:
- >
nix-shell --run '
npm test -- --ci --coverage --runInBand ${test_files[@]};
'
artifacts:
when: always
reports:
junit:
- ./tmp/junit/junit.xml
coverage_report:
coverage_format: cobertura
path: ./tmp/coverage/cobertura-coverage.xml
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
EOF

printf "\n"

# Using shards to optimise tests
# In the future we can incorporate test durations rather than using
# a static value for the parallel keyword

# Number of parallel shards to split the test suite into
CI_PARALLEL=2

cat << "EOF"
build:windows:
stage: build
needs: []
EOF
cat << EOF
parallel: $CI_PARALLEL
EOF
cat << "EOF"
tags:
- windows
before_script:
- .\scripts\choco-install.ps1
- refreshenv
script:
- npm config set msvs_version 2019
- npm install --ignore-scripts
- $env:Path = "$(npm bin);" + $env:Path
- npm test -- --ci --coverage --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL --maxWorkers=50%
artifacts:
when: always
reports:
junit:
- ./tmp/junit/junit.xml
coverage_report:
coverage_format: cobertura
path: ./tmp/coverage/cobertura-coverage.xml
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
EOF

printf "\n"

cat << "EOF"
build:macos:
stage: build
needs: []
EOF
cat << EOF
parallel: $CI_PARALLEL
EOF
cat << "EOF"
tags:
- shared-macos-amd64
image: macos-11-xcode-12
variables:
HOMEBREW_NO_INSTALL_UPGRADE: "true"
HOMEBREW_NO_INSTALL_CLEANUP: "true"
HOMEBREW_NO_INSTALLED_DEPENDENTS_CHECK: "true"
HOMEBREW_NO_AUTO_UPDATE: "true"
before_script:
- eval "$(brew shellenv)"
- brew install node@16
- brew link --overwrite node@16
- hash -r
script:
- npm install --ignore-scripts
- export PATH="$(npm bin):$PATH"
- npm test -- --ci --coverage --shard=$CI_NODE_INDEX/$CI_NODE_TOTAL --maxWorkers=50%
artifacts:
when: always
reports:
junit:
- ./tmp/junit/junit.xml
coverage_report:
coverage_format: cobertura
path: ./tmp/coverage/cobertura-coverage.xml
coverage: '/All files[^|]*\|[^|]*\s+([\d\.]+)/'
EOF

printf "\n"
Loading

0 comments on commit 1290813

Please sign in to comment.