gitlab-ci/cd(三).gitlab-ci.yaml编码

gitlab-ci/cd的两大要素:gitlab-runner和.gitlab-ci.yaml

下面将讲述.gitlab-ci.yaml

常用的关键词:

  • stages:全局自定义阶段
  • script:shell脚本
  • stage:任务内阶段,必须从全局阶段中选

简单例子一:

stages:
  - jobbuild
  - build
  - test
  - deploy
job1:
  stage: jobbuild
  script:
    - echo "This job compiles code."
job2:
  stage: test
  script:
    - echo "This job tests the compiled code. It runs when the build stage completes."
job3:
  script:
    - echo "This job also runs in the test stage".
job4:
  stage: deploy
  script:
    - echo "This job deploys the code. It runs when the test stage completes."
job5:
  stage: .pre
  script:
    - echo 'my .pre job 5'

pipeline的顺序是 .pre<<build<<test<<deploy<<.post

注意:此时的job2和job3并不能并行,需要配置gitlab-runner实现

script 执行shell脚本

1、单行命令
shell> script: " echo 111"
2、执行多行命令
script :
  --uname -a
  -- bundle exec rspec      

常用关键词:retry(重试机制),最多重试2次

--------例子一
test:
  script: rspec
  retry: 2
--------例子二
test:
  script: rspec
  retry:
    max: 2
    when:
      - runner_system_failure
      - stuck_or_timeout_failure
  • always: Retry on any failure (default).
  • unknown_failure: Retry when the failure reason is unknown.
  • script_failure: Retry when the script failed.
  • api_failure: Retry on API failure.
  • stuck_or_timeout_failure: Retry when the job got stuck or timed out.
  • runner_system_failure: Retry if there is a runner system failure (for example, job setup failed).
  • missing_dependency_failure: Retry if a dependency is missing.
  • runner_unsupported: Retry if the runner is unsupported.
  • stale_schedule: Retry if a delayed job could not be executed.
  • job_execution_timeout: Retry if the script exceeded the maximum execution time set for the job.
  • archived_failure: Retry if the job is archived and can’t be run.
  • unmet_prerequisites: Retry if the job failed to complete prerequisite tasks.
  • scheduler_failure: Retry if the scheduler failed to assign the job to a runner.
  • data_integrity_failure: Retry if there is a structural integrity problem detected.

关键词:image 指定一个基础Docker镜像作为基础运行环境,经常用到的镜像有node java python docker

关键词:tags 用于指定runner,tags的取值范围是在该项目可见的runner tags中

关键词:only/except 限定当前任务执行条件.例如only“master”分支,或者 except (排除)“master”分支

关键词:when 是实现在发生故障或尽管发生故障时仍能运行的作业

关键词:缓存是将当前工作环境目录的一些文件,一些文件夹存储起来,用于在各个任务初始化的时候恢复