前置步驟:html
編寫結構相似Jenkins pineline流水線java
GitLab CI/CD Pipeline Configuration Referencelinux
GitLab CI/CD Pipeline Configuration Reference
GitLab CI/CD pipelines are configured using a YAML file called
.gitlab-ci.yml
within each project.gitThe
.gitlab-ci.yml
file defines the structure and order of the pipelines and determines:github
- What to execute using GitLab Runner.
- What decisions to make when specific conditions are encountered. For example, when a process succeeds or fails.
This topic covers CI/CD pipeline configuration. For other CI/CD configuration information, see:spring
- GitLab CI/CD Variables, for configuring the environment the pipelines run in.
- GitLab Runner advanced configuration, for configuring GitLab Runner.
We have complete examples of configuring pipelines:docker
- For a quick introduction to GitLab CI, follow our quick start guide.
- For a collection of examples, see GitLab CI/CD Examples.
- To see a large
.gitlab-ci.yml
file used in an enterprise, see the.gitlab-ci.yml
file forgitlab-ce
.
官方參數表列出了做業的可用參數:api
關鍵詞 | 描述 |
---|---|
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
被棄用。緩存
官方構建案例app
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
結合可用參數和樣例配置,根據已經存在的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自動觸發構建
自動鏡像打包
自動上傳(推送)鏡像
若執行失敗則會發送郵件給開發人員
CICD全套流程目標實現!
無論使用GitLab CICD或者是使用Jenkins+Ansible的方式,目標都在於踐行DevOps,打通開發與運維流程,一旦配置好CICD流程,日後開發人員便再也不操心打包、構建等操做,能夠專一開發