前置步骤: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流程,日后开发人员便再也不操心打包、构建等操做,能够专一开发