Pipeline 流水线
11.6.1. cache
Java 缓存设置
image: maven:3.5.0-jdk-8 variables: MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository" cache: paths: - .m2/repository/ - target/ stages: - build - test - package build: stage: build script: mvn compile unittest: stage: test script: mvn test package: stage: package script: mvn package artifacts: paths: - target/java-project-0.0.1-SNAPSHOT.jar
Node 缓存设置
cache: paths: - node_modules - dist # variables: # GIT_STRATEGY: clone # GIT_STRATEGY: fetch # GIT_CHECKOUT: "false" stages: - build - test - deploy build-job: stage: build only: - master - testing - development script: - echo "Compiling the code..." # - cnpm cache verify - cnpm install - cnpm run build:stage # - cnpm run build:prod - echo "Compile complete." test-job: stage: test variables: GIT_STRATEGY: none only: - master - testing - development script: - echo "Running unit tests..." - sed -i 's#192.168.20.180#192.168.30.4#g' dist/umi.*.js - ls dist/* # - rm -rf *.tar.gz # - tar zcvf www.netkiller.cn.$(date -u +%Y-%m-%d.%H%M%S).tar.gz dist # - ls *.tar.gz - echo "Test complete." artifacts: name: "$CI_PROJECT_NAME" paths: - dist/* # - ./*.tar.gz deploy-test-job: stage: deploy variables: GIT_STRATEGY: none only: - testing - development script: - echo "Deploying application..." - rsync -auzv dist/* www@192.168.30.10:/opt/www.netkiller.cn/ - echo "Application successfully deployed." deploy-prod-job: stage: deploy only: - master script: - echo "Deploying application..." - rsync -auzv --delete dist/* www@192.168.30.10:/opt/www.netkiller.cn/ - echo "Application successfully deployed."
11.6.1.1. Cache Key
缓存在所有流水线间是共享的,如果同时有两个JOB在跑,缓存就可能受到影响,这时可以使用 cache key 解决。
对每个分支的每个 job 使用不同的 cache : cache: key: ${CI_COMMIT_REF_SLUG} 每个分支的每个 job 使用不同的 stage: cache: key: "$CI_JOB_NAME-$CI_COMMIT_REF_SLUG" 分支之间需要共享 cache,但是 pipeline 中的 job 之间的 cache 是相互独立的: cache: key: "$CI_JOB_STAGE-$CI_COMMIT_REF_SLUG" 缓存只在相同 CI_PIPELINE_ID 中共享 cache: key: ${CI_PIPELINE_ID}
11.6.1.2. 禁用 Cache
当定义了全局 cahce 后,想在 job 中禁用 Cache
cache: paths: - node_modules - dist job: cache: {}
11.6.1.3. 定义多个缓存
test-job: stage: build cache: - key: files: - Gemfile.lock paths: - vendor/ruby - key: files: - yarn.lock paths: - .yarn-cache/ script: - bundle install --path=vendor - yarn install --cache-folder .yarn-cache - echo Run tests...
11.6.2. stages
定义 stages
stages: - build - test - deploy
11.6.2.1. 依赖关系
dependencies 可以设置 job 的依赖关系
image: mileschou/php-testing-base:7.0 stages: - build - test - deploy build_job: stage: build script: - composer install cache: untracked: true artifacts: paths: - vendor/ test_job: stage: test script: - php vendor/bin/codecept run dependencies: - build_job deploy_job: stage: deploy script: - echo Deploy OK only: - release when: manual
11.6.2.2. 禁用 stage
出于某种原因,我们想禁用某些 stage。可以在 job 前加一个 “.” 禁用它。
.deploy: image: maven:3.6-jdk-11 tags: - shell script: - 'mvn deploy -s ci_settings.xml' # only: # - main
11.6.3. variables
job1: variables: FOLDERS: src test docs script: - | for FOLDER in $FOLDERS do echo "The path is root/${FOLDER}" done
11.6.3.1. 列出所有环境变量
使用 export 列出所有环境变量
build-job: image: maven:3.8.2-openjdk-17 stage: build # variables: # accessKeyId: 123456 # accessSecret: 654321 tags: - docker before_script: - export - cat src/main/resources/application.properties script: - mvn clean package -Dmaven.test.skip=true - ls target/*.jar artifacts: name: "$CI_PROJECT_NAME" paths: - target/*.jar
$ export 21declare -x CI="true" 22declare -x CI_API_V4_URL="http://192.168.30.5/api/v4" 23declare -x CI_BUILDS_DIR="/builds" 24declare -x CI_BUILD_BEFORE_SHA="213825d0cfd133aadb2648b0c1236f834e98972b" 25declare -x CI_BUILD_ID="4705" 26declare -x CI_BUILD_NAME="build-job" 27declare -x CI_BUILD_REF="61fe2acb56474b4b2ffb289de2c7d93afe514354" 28declare -x CI_BUILD_REF_NAME="development" 29declare -x CI_BUILD_REF_SLUG="development" 30declare -x CI_BUILD_STAGE="build" 31declare -x CI_BUILD_TOKEN="[MASKED]" 32declare -x CI_COMMIT_AUTHOR="neo <neo@t.com>" 33declare -x CI_COMMIT_BEFORE_SHA="213825d0cfd133aadb2648b0c1236f834e98972b" 34declare -x CI_COMMIT_BRANCH="development" 35declare -x CI_COMMIT_DESCRIPTION="" 36declare -x CI_COMMIT_MESSAGE="更新.gitlab-ci.yml文件" 37declare -x CI_COMMIT_REF_NAME="development" 38declare -x CI_COMMIT_REF_PROTECTED="true" 39declare -x CI_COMMIT_REF_SLUG="development" 40declare -x CI_COMMIT_SHA="61fe2acb56474b4b2ffb289de2c7d93afe514354" 41declare -x CI_COMMIT_SHORT_SHA="61fe2acb" 42declare -x CI_COMMIT_TIMESTAMP="2021-09-18T07:00:58+00:00" 43declare -x CI_COMMIT_TITLE="更新.gitlab-ci.yml文件" 44declare -x CI_CONCURRENT_ID="0" 45declare -x CI_CONCURRENT_PROJECT_ID="0" 46declare -x CI_CONFIG_PATH=".gitlab-ci.yml" 47declare -x CI_DEFAULT_BRANCH="development" 48declare -x CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX="192.168.30.5:80/neo/dependency_proxy/containers" 49declare -x CI_DEPENDENCY_PROXY_PASSWORD="[MASKED]" 50declare -x CI_DEPENDENCY_PROXY_SERVER="192.168.30.5:80" 51declare -x CI_DEPENDENCY_PROXY_USER="gitlab-ci-token" 52declare -x CI_DISPOSABLE_ENVIRONMENT="true" 53declare -x CI_JOB_ID="4705" 54declare -x CI_JOB_IMAGE="maven:3.8.2-openjdk-17" 55declare -x CI_JOB_JWT="[MASKED]" 56declare -x CI_JOB_NAME="build-job" 57declare -x CI_JOB_STAGE="build" 58declare -x CI_JOB_STARTED_AT="2021-09-18T07:01:07Z" 59declare -x CI_JOB_STATUS="running" 60declare -x CI_JOB_TOKEN="[MASKED]" 61declare -x CI_JOB_URL="http://192.168.30.5/neo/alertmanager-webhook/-/jobs/4705" 62declare -x CI_NODE_TOTAL="1" 63declare -x CI_PAGES_DOMAIN="example.com" 64declare -x CI_PAGES_URL="http://neo.example.com/alertmanager-webhook" 65declare -x CI_PIPELINE_CREATED_AT="2021-09-18T07:00:58Z" 66declare -x CI_PIPELINE_ID="1866" 67declare -x CI_PIPELINE_IID="100" 68declare -x CI_PIPELINE_SOURCE="push" 69declare -x CI_PIPELINE_URL="http://192.168.30.5/neo/alertmanager-webhook/-/pipelines/1866" 70declare -x CI_PROJECT_CLASSIFICATION_LABEL="" 71declare -x CI_PROJECT_DIR="/builds/neo/alertmanager-webhook" 72declare -x CI_PROJECT_ID="23" 73declare -x CI_PROJECT_NAME="alertmanager-webhook" 74declare -x CI_PROJECT_NAMESPACE="neo" 75declare -x CI_PROJECT_PATH="neo/alertmanager-webhook" 76declare -x CI_PROJECT_PATH_SLUG="neo-alertmanager-webhook" 77declare -x CI_PROJECT_REPOSITORY_LANGUAGES="java" 78declare -x CI_PROJECT_ROOT_NAMESPACE="neo" 79declare -x CI_PROJECT_TITLE="Alertmanager Webhook" 80declare -x CI_PROJECT_URL="http://192.168.30.5/neo/alertmanager-webhook" 81declare -x CI_PROJECT_VISIBILITY="public" 82declare -x CI_REGISTRY_PASSWORD="[MASKED]" 83declare -x CI_REGISTRY_USER="gitlab-ci-token" 84declare -x CI_REPOSITORY_URL="http://gitlab-ci-token:[MASKED]@192.168.30.5/neo/alertmanager-webhook.git" 85declare -x CI_RUNNER_DESCRIPTION="development" 86declare -x CI_RUNNER_EXECUTABLE_ARCH="linux/amd64" 87declare -x CI_RUNNER_ID="23" 88declare -x CI_RUNNER_REVISION="58ba2b95" 89declare -x CI_RUNNER_SHORT_TOKEN="GP-ozvd6" 90declare -x CI_RUNNER_TAGS="docker" 91declare -x CI_RUNNER_VERSION="14.2.0" 92declare -x CI_SERVER="yes" 93declare -x CI_SERVER_HOST="192.168.30.5" 94declare -x CI_SERVER_NAME="GitLab" 95declare -x CI_SERVER_PORT="80" 96declare -x CI_SERVER_PROTOCOL="http" 97declare -x CI_SERVER_REVISION="2da7c857960" 98declare -x CI_SERVER_URL="http://192.168.30.5