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

806次阅读
没有评论

本文档用于描述 .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:

Keyword Description
after_script job 完成后执行的命令
allow_failure 当 job 的状态为失败时,不会导致 pipline 失败结束
artifacts List of files and directories to attach to a job on success.
before_script job 开始前要执行的命令
cache 用来指定需要在 job 之间缓存的文件或目录。只能使用该项目工作空间内的路径。(定义一组文件列表,可在后续运行中使用)
coverage Code coverage settings for a given job.
dependencies Restrict which artifacts are passed to a specific job by providing a list of jobs to fetch artifacts from.
environment 给部署 job 的环境命名(用于定义 job 部署到特殊的环境中)
except Control when jobs are not created.
extends Configuration entries that this job inherits from.
image 使用 Docker 的镜像
include Include external YAML files.
inherit Select which global defaults all jobs inherit.
interruptible Defines if a job can be canceled when made redundant by a newer run.
needs Execute jobs earlier than the stage ordering.
only Control when jobs are created.
pages Upload the result of a job to use with GitLab Pages.
parallel How many instances of a job should be run in parallel.
release Instructs the runner to generate a release object.
resource_group Limit job concurrency.
retry job 可接受失败的次数,例: retry: 2,第一次成功后便不再尝试,若第一次失败则再次尝试, 尝试 3 次
rules List of conditions to evaluate and determine selected attributes of a job, and whether or not it’s created.
script 在 runner 中运行的 shell 脚本
secrets The CI/CD secrets the job needs.
services 用于 docker 服务
stage 定义构建阶段
tags 可以从允许运行此项目的所有 Runners 中选择特定的 Runners 来执行 jobs。
timeout Define a custom job-level timeout that takes precedence over the project-wide setting.
trigger Defines a downstream pipeline trigger.
variables 定义 job 级别的变量
when When 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"
正文完
 
评论(没有评论)