Gitlab-.gitlab-ci.yml的介绍与简单编写

发布于 2021-08-05  154 次阅读


本文档用于描述 .gitlab-ci.yml 语法,.gitlab-ci.yml 文件被用来管理项目的 runner 任务。如果想要快速的了解GitLab CI ,可查看快速引导。 从 7.12 版本开始,GitLab CI 使用YAML文件 (.gitlab-ci.yml) 来管理项目配置。该文件存放于项目仓库的根目录,它定义该项目如何构建。

.gitlab-ci.yml文件中指定了CI的触发条件、工作内容、工作流程,编写和理解此文件是CI实战中最重要的一步,该文件指定的任务内容总体构成了1个pipeline、1个pipeline包含不同的stage执行阶段、每个stage包含不同的具体job脚本任务。

.gitlab-ci.yml的配置项

Job keywords(任务关键词)

The keywords available for jobs are:

KeywordDescription
after_scriptjob完成后执行的命令
allow_failure当job的状态为失败时,不会导致pipline失败结束
artifactsList of files and directories to attach to a job on success.
before_scriptjob开始前要执行的命令
cache用来指定需要在job之间缓存的文件或目录。只能使用该项目工作空间内的路径。(定义一组文件列表,可在后续运行中使用)
coverageCode coverage settings for a given job.
dependenciesRestrict which artifacts are passed to a specific job by providing a list of jobs to fetch artifacts from.
environment给部署job的环境命名(用于定义job部署到特殊的环境中)
exceptControl when jobs are not created.
extendsConfiguration entries that this job inherits from.
image使用Docker的镜像
includeInclude external YAML files.
inheritSelect which global defaults all jobs inherit.
interruptibleDefines if a job can be canceled when made redundant by a newer run.
needsExecute jobs earlier than the stage ordering.
onlyControl when jobs are created.
pagesUpload the result of a job to use with GitLab Pages.
parallelHow many instances of a job should be run in parallel.
releaseInstructs the runner to generate a release object.
resource_groupLimit job concurrency.
retryjob可接受失败的次数,例: retry: 2 ,第一次成功后便不再尝试,若第一次失败则再次尝试,尝试3次
rulesList of conditions to evaluate and determine selected attributes of a job, and whether or not it’s created.
script在runner中运行的shell脚本
secretsThe CI/CD secrets the job needs.
services用于docker服务
stage定义构建阶段
tags可以从允许运行此项目的所有Runners中选择特定的Runners来执行jobs。
timeoutDefine a custom job-level timeout that takes precedence over the project-wide setting.
triggerDefines a downstream pipeline trigger.
variables定义job级别的变量
whenWhen to run job.

参考链接:

https://docs.gitlab.com/ee/ci/yaml/gitlab_ci_yaml.html

stage(常用)-总共5个阶段

参考:

按照阶段排序执行,同一阶段的并行(这里的同阶段也不是完全并行,从pipline中可以看出,前面的job先执行)

原因是由gitlab-runner的默认配置为一次执行一个任务

job:
  stage: .pre
  script:
    - echo "stage pre"

job1:
  stage: build
  script:
    - echo "stage build"

job2:
  stage: test
  script:
    - echo "stage test1"

job3:
  stage: test
  script:
    - echo "stage test2"

定义自己的stages

stages:
  - stage1
  - stage2
  - stage3
job:
  stage: stage1
  script:
    - echo "stage1"

job1:
  stage: stage2
  script:
    - echo "stage2"

job2:
  stage: stage3
  script:
    - echo "stage3"

job3:
  stage: stage3
  script:
    - echo "stage3"

image:指定一个Docker镜像作为基础运行环境(当前项目代码构建所需环境)

tages: 以从允许运行此项目的所有Runners中选择特定的Runners来执行jobs。

cache: 定义一组文件列表,可在后续运行中使用

image: node:alpine

stages:
  - install
  - build
  - deploy

cache:
  key: vitepro
  paths:
    - node_modules/
job_install:
  stage: install
  script:
    - npm init --yes
    - npm install
  tags:
    - docker_cicd


job_build:
  stage: build
  script:
    - npm init --yes
    - npm run build
  tags:
    - docker_cicd

job_deploy:
  stage: deploy
  script:
    - echo "start deploy"



image: node:alpine

stages:
  - install
  - build
  - deploy

cache:
  key: vitepro
  paths:
    - node_modules/
job_install:
  stage: install
  script:
    - npm init --yes
    - npm install
  tags:
    - docker_cicd


job_build:
  stage: build
  script:
    - npm init --yes
    - npm run build
  tags:
    - docker_cicd

job_deploy:
  stage: deploy
  image: docker 
  script:
    - docker build -t [Image Name] .
    - if [ $(docker ps -qa --filter name=mylive-container) ];then docker rm -f mylive-container;fi
    - docker run -d -p 8082:80 --name=mylive-container [Image Name]

当流水线使用docker build 的时候,所使用的gitlab-runner配置需要添加一项内容:

"/usr/bin/docker:/usr/bin/docker","/var/run/docker.sock:/var/run/docker.sock"