Docker 版 GitLab CICD 實踐3——CI檔案編寫
- maven專案自動打包
-
自動測試 - 自動映象構建並上傳
官方引數表列出了作業的可用引數:
關鍵詞 | 描述 |
---|---|
script |
由Runner執行的Shell指令碼。 |
image |
使用泊塢窗影象。也可用: image:name 和 image:entrypoint 。 |
services |
使用docker services影象。也可用: services:name , services:alias , services:entrypoint ,和 services:command 。 |
before_script |
覆蓋在作業之前執行的一組命令。 |
after_script |
覆蓋作業後執行的一組命令。 |
stages |
定義管道中的階段。 |
stage |
定義作業階段(預設值:) test 。 |
only |
建立作業時限制。也可用: only:refs , only:kubernetes , only:variables ,和 only:changes 。 |
except |
在未建立作業時限制。也可用: except:refs , except:kubernetes , except:variables ,和 except:changes 。 |
tags |
用於選擇Runner的標籤列表。 |
allow_failure |
讓工作失敗。失敗的作業無助於提交狀態。 |
when |
什麼時候開始工作。也可用: when:manual 和 when:delayed 。 |
environment |
作業部署到的環境的名稱。也可用: environment:name , environment:url , environment:on_stop ,和 environment:action 。 |
cache |
後續執行之間應快取的檔案列表。也可用: cache:paths , cache:key , cache:untracked ,和 cache:policy 。 |
artifacts |
成功附加到作業的檔案和目錄列表。也可用: artifacts:paths , artifacts:name , artifacts:untracked , artifacts:when , artifacts:expire_in , artifacts:reports ,和 artifacts:reports:junit 。 在GitLab企業版,這些都是可供選擇: artifacts:reports:codequality , artifacts:reports:sast , artifacts:reports:dependency_scanning , artifacts:reports:container_scanning , artifacts:reports:dast , artifacts:reports:license_management ,和 artifacts:reports:performance 。 |
dependencies |
作業所依賴的其他作業,以便您可以在它們之間傳遞工件。 |
coverage |
給定作業的程式碼覆蓋率設定。 |
retry |
在發生故障的情況下,可以自動重試作業的次數和次數。 |
parallel |
應該並行執行多少個作業例項。 |
trigger |
定義下游管道觸發器。 |
include |
允許此作業包含外部YAML檔案。也可用: include:local , include:file , include:template ,和 include:remote 。 |
extends |
此作業將繼承的配置條目。 |
pages |
上傳作業結果以用於GitLab Pages。 |
variables |
在作業級別定義作業變數。 |
注:引數 types
和 type
被 棄用 。
參考
官方構建案例
Deploy a Spring Boot application to Cloud Foundry with GitLab CI/CD
參考其指令碼
Configure GitLab CI/CD to deploy your application
Now we need to add the GitLab CI/CD configuration file ( .gitlab-ci.yml
) to our project’s root. This is how GitLab figures out what commands need to be run whenever code is pushed to our repository. We will add the following .gitlab-ci.yml
file to the root directory of the repository, GitLab will detect it automatically and run the steps defined once we push our code:
image: java:8 stages: - build - deploy build: stage: build script: ./mvnw package artifacts: paths: - target/demo-0.0.1-SNAPSHOT.jar production: stage: deploy script: - curl --location "https://cli.run.pivotal.io/stable?release=linux64-binary&source=github" | tar zx - ./cf login -u $CF_USERNAME -p $CF_PASSWORD -a api.run.pivotal.io - ./cf push only: - master 複製程式碼
配置
全域性變數
進入工程CI設定配置全域性變數指令碼,包含映象倉庫登陸名稱、密碼、打包名稱等
配置全域性變數目的在於配置指令碼中 不應該 包含密碼等敏感資訊

若希望使用GitLab內建環境變數,可參考官方表格
GitLab CI/CD environment variables
CI指令碼
結合可用引數和樣例配置,根據已經存在的SpringBoot專案編寫響應的CI指令碼 .gitlab-ci.yml
image: docker:stable services: - docker:dind variables: DOCKER_DRIVER: overlay SPRING_PROFILES_ACTIVE: gitlab-ci stages: - build - package maven-build: image: maven:3-jdk-8 stage: build script: "mvn package -B" artifacts: paths: - target/*.jar docker-build: stage: package script: - docker build -t $CONTAINER_IMAGE:latest . - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASS - docker push $CONTAINER_IMAGE:latest 複製程式碼
該指令碼對於滿足指令碼目標的工程都可複用,若需要測試,則增加相關的測試步驟即可
此處由於工程包含外部依賴關係,不在構建時測試
自動構建
預設的觸發構建事件為 commit
等
觸發後會自動執行任務


Maven自動觸發構建

自動映象打包


自動上傳(推送)映象
