示例 .gitlab-ci.yaml 文件
如下内容,为示例 .gitlab-ci.yaml 文件,记录我们的常用指令及含义:
variables: PROJECT_FOLDER: /data/${CI_PROJECT_NAMESPACE}_${CI_PROJECT_NAME} stages: - build - test - deploy default: before_script: - # run some shell cmds ####### 项目构建 ####### build: stage: build tags: - "<tag of gitlab runner>" before_script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY script: - mvn $MAVEN_CLI_OPTS compile - docker-compose build - docker-compose push ####### 项目测试 ####### unit_tests: stage: test tags: - "<tag of gitlab runner>" script: - mvn $MAVEN_CLI_OPTS test integration_tests: stage: test tags: - "<tag of gitlab runner>" script: - mvn $MAVEN_CLI_OPTS test ####### 代码分析 ####### # https://docs.gitlab.com/ee/user/project/merge_requests/code_quality.html code_quality: stage: test except: - master tags: - "<tag of gitlab runner>" ####### 应用部署 ####### deploy_to_staging: stage: deploy only: - develop tags: - "<tag of gitlab runner>" before_script: - mkdir -pv ${PROJECT_FOLDER} && rsync -av ./ ${PROJECT_FOLDER}/ && cd ${PROJECT_FOLDER} && pwd - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY script: - docker-compose down - docker-compose pull - docker-compose up -d deploy_to_production: stage: deploy only: - master tags: - "<tag of gitlab runner>" before_script: - mkdir -pv ${PROJECT_FOLDER} && rsync -av ./ ${PROJECT_FOLDER}/ && cd ${PROJECT_FOLDER} && pwd - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY script: - docker-compose down - docker-compose pull - docker-compose up -d
.gitlab-ci.yaml
before_script
Using SSH keys with GitLab CI/CD | GitLab
GitLab CI/CD pipeline configuration reference | GitLab
通过 before_script 指令,可以在运行 script 指令前,执行某些命令
variables
GitLab CI/CD environment variables | GitLab
Go.gitlab-ci.yml · master · GitLab.org / GitLab CI Yml – Deprecated · GitLab
用于定义环境变量,以在后面进行引用。
include
GitLab CI/CD pipeline configuration reference | GitLab
通过 include 指令,可以引入其他路径(或其他仓库)的 gitlab-ci.yml 文件。
cache
Keyword reference for the .gitlab-ci.yml file / cache
作用:在多个 Job 之间,缓存指定的文件和目录。
为了缓存 maven 下载的 .m2/ 模块,我们修改 GitLab Runner Docker Execuotr 的配置,将宿主机的目录挂载到容器:
[runners.docker] ... volumes = ["/home:/home", "/root:/root"] ...
参考文献
The .gitlab-ci.yml file | GitLab
docs/configuration/advanced-configuration.md · master · GitLab.org / gitlab-runner · GitLab