运维-gitlab-CICD-语法1

173 阅读2分钟

语法1:job

explain: 作业(流水线的一个阶段),每个作业至少要包含一个script

example:

job1:
    script: "execute-script-for-job1"
    
job2:
    script: "execute-script-for-job2"

语法2:script

explain: 执行shell脚本(linux),bat脚本(windows)

example:

job:
    script:
        - uname -a
        - echo "good!!!"

语法3:before_script

explain: 该命令在每个作业之前运行(执行的命令必须是数组格式),before_script失败会导致整个作业失败,后续作业也不再执行,但不会影响after_script运行。

语法4:after_script

explain: 用于定义将在每个作业(包括失败的作业)之后运行(执行的命令必须是数组格式)

语法5:stages(里面对应job里面的stage)

explain: 用于定义作业可以使用的阶段,并且是全局定义的。同一阶段的作业并行运行,不同阶段按顺序执行

语法6:stage的 .pre & .post

explain: .pre始终是整个管道的第一个运行阶段,.post始终是整个管道的最后一个运行阶段。用户定义的阶段都在两者 之间运行。.pre和.post的顺序无法更改。如果管道仅包含.pre或.post阶段的作业,则不会创建管道。

语法7:variables

explain: 定义变量,pipeline变量,job变量。job变量优先级最大

example:

before_script:
  - echo "before-script!!"

variables:
  DOMAIN: example.com

stages:
  - build
  - test
  - codescan
  - deploy

build:
  before_script:
    - echo "before-script in job"
  stage: build
  script:
    - echo "mvn clean"
    - echo "mvn install"
    - echo "$DOMAIN"
  after_script:
    - echo "after script in build job"

unittest:
  stage: test
  script:
    - echo "run test"

deploy:
  stage: deploy
  script:
    - echo "hello deploy"
    - sleep 2

codescan:
  stage: codescan
  script:
    - echo "codescan"
    - sleep 5

after_script:
  - echo "after-script"
  - echo "missing command"

最后关于并行执行作业

如果runner的线程数不够,虽然现实并行,实际可以看到有先后顺序

可修改runner的每次运行的作业数量。默认是1,可改为10。

修改文件
vim /etc/gitlab-runner/config.toml

修改点
concurrent = 10

image.png