gitbeaker/.gitlab-ci.yml
2023-05-22 00:16:22 -04:00

415 lines
10 KiB
YAML

stages:
- install
- build
- lint
- format
- test
- release
# Fallback variables
variables:
PLAYWRIGHT_VERSION: $CI_JOB_IMAGE
TEST_ID: $CI_JOB_ID
CC_TEST_REPORTER_ID: $CC_TEST_REPORTER_ID
# Cache Config
.cache:install:modules: &cacheinstallmodules
key:
files:
- yarn.lock
paths:
- node_modules
.cache:install:cc: &cachecodeclimate
key: codeclimate-0.11.1
paths:
- tmp/codeclimate
.cache:install:yarn: &cacheinstallyarn
key: yarn-$CI_JOB_IMAGE
paths:
- .yarn
.cache:nx: &cachenx
key:
files:
- yarn.lock
paths:
- node_modules/.cache/nx
#Link and Install all required dependancies
install:
image: node:18-alpine
stage: install
before_script:
- apk add --no-cache libc6-compat jq
- echo "PLAYWRIGHT_VERSION=$(yarn info @playwright/test -A --json | jq ".children.Version")" >> install.env
script:
- yarn
cache:
- <<: *cacheinstallmodules
policy: pull-push
- <<: *cacheinstallyarn
policy: pull-push
artifacts:
reports:
dotenv: install.env
build:
stage: build
image: node:18-alpine
needs:
- install
cache:
- <<: *cacheinstallmodules
policy: pull
- <<: *cacheinstallyarn
policy: pull
- <<: *cachenx
policy: pull-push
before_script:
- apk add --no-cache libc6-compat git
script:
- yarn build
artifacts:
paths:
- packages/core/dist/
- packages/cli/dist/
- packages/rest/dist/
- packages/requester-utils/dist/
expire_in: 2d
# Lint all code, tests and supporting documentation (README, CHANGELOG etc)
lint:
stage: lint
image: node:18-alpine
needs:
- install
cache:
- <<: *cacheinstallmodules
policy: pull
- <<: *cacheinstallyarn
policy: pull
- <<: *cachenx
policy: pull-push
before_script:
- apk add --no-cache libc6-compat
script: yarn lint
format:
stage: format
image: node:18-alpine
needs:
- install
cache:
- <<: *cacheinstallmodules
policy: pull
- <<: *cacheinstallyarn
policy: pull
before_script:
- apk add --no-cache libc6-compat
script: yarn format
# Tests
.test:base:
stage: test
image: node:18-alpine
needs:
- install
cache:
- <<: *cacheinstallmodules
policy: pull
- <<: *cacheinstallyarn
policy: pull
- <<: *cachenx
policy: pull-push
before_script:
- apk add --no-cache libc6-compat
.test:unit:base:
extends: .test:base
needs:
- install
- test:unit:setup
cache:
- <<: *cacheinstallmodules
policy: pull
- <<: *cacheinstallyarn
policy: pull
- <<: *cachecodeclimate
policy: pull
- <<: *cachenx
policy: pull-push
coverage: /All files[^|]*\|[^|]*\s+([\d\.]+)/
before_script:
- apk add --no-cache libc6-compat git
after_script:
- cd $PKG_PATH
- ../../tmp/codeclimate/cc-test-reporter format-coverage -t lcov -o coverage/cc-coverage.json coverage/lcov.info
artifacts:
when: always
paths:
- $PKG_PATH/coverage/cc-coverage.json
reports:
junit: $PKG_PATH/reports/*junit.xml
coverage_report:
coverage_format: cobertura
path: $PKG_PATH/coverage/cobertura-coverage.xml
.test:types:base:
extends: .test:base
needs:
- build
.test:integration:base:
extends: .test:base
only:
refs:
- /^pr-[0-9]+$/
- main
- next
needs:
- install
- test:live:setup
.test:e2e:base:
extends: .test:integration:base
needs:
- build
- test:live:setup
## Unit Tests
test:unit:setup:
stage: test
image: alpine/curl
script:
- mkdir -p tmp/codeclimate
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./tmp/codeclimate/cc-test-reporter
- ls tmp/codeclimate/
- chmod +x ./tmp/codeclimate/cc-test-reporter
cache:
- <<: *cachecodeclimate
policy: pull-push
test:unit:utils:
extends: .test:unit:base
script: yarn test:unit --projects=@gitbeaker/requester-utils
variables:
PKG_PATH: 'packages/requester-utils'
test:unit:core:
extends: .test:unit:base
script: yarn test:unit --projects=@gitbeaker/core
variables:
PKG_PATH: 'packages/core'
test:unit:rest:
extends: .test:unit:base
script: yarn test:unit --projects=@gitbeaker/rest
variables:
PKG_PATH: 'packages/rest'
test:unit:cli:
extends: .test:unit:base
script: yarn test:unit --projects=@gitbeaker/cli
variables:
PKG_PATH: 'packages/cli'
test:unit:teardown:
stage: test
image: alpine/curl
except:
refs:
- /^pr-[0-9]+$/
needs:
- test:unit:utils
- test:unit:core
- test:unit:rest
- test:unit:cli
cache:
- <<: *cachecodeclimate
policy: pull
before_script:
- mkdir coverage
- ls tmp/codeclimate
script:
- ./tmp/codeclimate/cc-test-reporter sum-coverage packages/*/coverage/cc-coverage.json -p 4 -o ./coverage/codeclimate.json
- ./tmp/codeclimate/cc-test-reporter upload-coverage -i ./coverage/codeclimate.json
## Types Tests
test:types:utils:
extends: .test:types:base
variables:
PKG_PATH: 'packages/requester-utils'
script: yarn test:types --projects=@gitbeaker/requester-utils
test:types:core:
extends: .test:types:base
variables:
PKG_PATH: 'packages/core'
script: yarn test:types --projects=@gitbeaker/core
test:types:rest:
extends: .test:types:base
variables:
PKG_PATH: 'packages/rest'
script: yarn test:types --projects=@gitbeaker/rest
## Live Test Setup and Clean Up
test:live:setup:
image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine
stage: test
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: always
- if: $CI_COMMIT_BRANCH == "next" || $CI_COMMIT_BRANCH =~ /^pr-[0-9]+$/
when: manual
needs:
- build
allow_failure: false
variables:
GITLAB_PERSONAL_ACCESS_TOKEN: gitbeaker
CONTAINER_NAME: gitlab-${CI_PIPELINE_ID}
GITLAB_ROOT_PASSWORD: gitbeaker
before_script:
- apk add jq curl
script:
- echo $GCLOUD_SERVICE_KEY | base64 -d | gcloud auth activate-service-account --key-file=-
- gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
- |
GITLAB_URL=$(gcloud compute instances create-with-container $CONTAINER_NAME \
--project gitbeaker \
--machine-type=e2-standard-2 \
--zone=northamerica-northeast1-b \
--tags http-server,https-server \
--container-image docker.io/gitlab/gitlab-ce:latest \
--container-command '/bin/sh' \
--container-arg="-c" \
--container-arg="printf '#!/usr/bin/env ruby \nu = User.first \nu.admin = true \nu.save! \nt = PersonalAccessToken.new({ user: u, name: \"gitbeaker\", scopes: [\"api\", \"read_user\"]}) \nt.set_token(\"${GITLAB_PERSONAL_ACCESS_TOKEN}\") \nt.save! \nputs t.token\n' > /opt/gitlab/embedded/service/gitlab-rails/db/fixtures/production/40_access_token.rb && /assets/wrapper" \
--container-env ^~^GITLAB_ROOT_PASSWORD=${GITLAB_ROOT_PASSWORD}~PERSONAL_ACCESS_TOKEN=${GITLAB_PERSONAL_ACCESS_TOKEN}~GITLAB_OMNIBUS_CONFIG="gitlab_rails['monitoring_whitelist'] = ['0.0.0.0/0', '172.17.0.1'];" \
--metadata SELF_DESTRUCT_INTERVAL_MINUTES=15 \
--metadata-from-file startup-script=./scripts/shutdown.sh \
--format='get(networkInterfaces[0].accessConfigs[0].natIP)'\
| head -2 | tail -1 )
- GITLAB_URL="http://${GITLAB_URL}"
- echo "GITLAB_URL=${GITLAB_URL}" >> intergration.env
- echo "GITLAB_PERSONAL_ACCESS_TOKEN=${GITLAB_PERSONAL_ACCESS_TOKEN}" >> intergration.env
- cat intergration.env
- echo "Waiting for service to start"
- |
sleep 180
while [[ "$(curl --fail --silent -X GET "$GITLAB_URL/-/readiness?all=1" --insecure | jq -r '.master_check[0].status')" != "ok" ]]; do echo "Polling service - not alive yet"; sleep 10; done;
echo "Service is up and running!"
artifacts:
reports:
dotenv: intergration.env
test:live:teardown:
stage: test
image: gcr.io/google.com/cloudsdktool/cloud-sdk:alpine
needs:
- test:integration:rest
- test:e2e:core
- test:e2e:cli
- test:e2e:rest
when: always
only:
refs:
- /^pr-[0-9]+$/
- main
- next
variables:
GIT_STRATEGY: none
script:
- echo $GCLOUD_SERVICE_KEY | base64 -d | gcloud auth activate-service-account --key-file=-
- gcloud --quiet config set project ${GOOGLE_PROJECT_ID}
- gcloud compute instances delete gitlab-${CI_PIPELINE_ID} --zone=northamerica-northeast1-b
## Integration Tests
test:integration:rest:
extends: .test:integration:base
script: yarn test:integration --projects=@gitbeaker/rest
## End-to-End Tests
test:e2e:core:
extends: .test:e2e:base
script: yarn test:e2e --projects=@gitbeaker/core
test:e2e:rest:
extends: .test:e2e:base
image: mcr.microsoft.com/playwright:v1.31.0-focal
before_script:
# reinstall swc due to missing binding for ubuntu image
- yarn add @swc/core
script: yarn test:e2e --projects=@gitbeaker/rest
test:e2e:cli:
extends: .test:e2e:base
script: yarn test:e2e --projects=@gitbeaker/cli
# Releases
.release:base:
stage: release
image: node:18-alpine
variables:
GIT_STRATEGY: clone
GIT_DEPTH: 0
cache:
- <<: *cacheinstallmodules
policy: pull
- <<: *cacheinstallyarn
policy: pull
before_script:
- apk add --no-cache libc6-compat git openssh
- npm config set //registry.npmjs.org/:_authToken $NPM_TOKEN
- git config --global user.name "Autobot"
- git config --global user.email "ci@gitlab.com"
- git remote set-url origin https://jdalrymple:${GITHUB_TOKEN}@github.com/jdalrymple/gitbeaker.git
- git branch $CI_COMMIT_BRANCH HEAD
- git checkout $CI_COMMIT_BRANCH
## Canary or RC Special Releases
release:special:
extends: .release:base
needs:
- build
- test:integration:rest
- test:e2e:core
- test:e2e:cli
- test:e2e:rest
only:
refs:
- /^pr-[0-9]+$/
script:
- echo "Checking for special release..."
- |
if [[ $CI_COMMIT_BRANCH =~ ^pr-[0-9]*$ ]]; then
pr_id="${CI_COMMIT_BRANCH//[!0-9]/}"
export CI_MERGE_REQUEST_ID=$pr_id
export CI_MERGE_REQUEST_SOURCE_BRANCH_NAME=$CI_COMMIT_BRANCH
labels=$(yarn auto label --pr $pr_id)
if [[ "$labels" =~ "release:canary" ]]; then
yarn release:canary
fi
fi
## Production
release:
extends: .release:base
needs:
- build
- test:integration:rest
- test:e2e:core
- test:e2e:cli
- test:e2e:rest
- test:unit:teardown
only:
refs:
- main
- next
script: yarn release