zl程序教程

您现在的位置是:首页 >  其他

当前栏目

gitlab添加yml文件.gitlab-ci.yml

文件 添加 gitlab CI yml
2023-09-14 09:00:34 时间

一、前言

没有使用或不熟悉gitlab的同学,对在gitlab上新建项目、添加yml文件(用于CI/CD控制)这两个操作会感到茫然,下面我将实现方法作以说明。

二、实现方法

1、在gitlab中新建项目(以空私有项目为例)

添加+  -》 New project -》填写相关内容 Create Project 

 

 

 

 2、添加yml文件

选择项目-》set up CI/CD -》 自己写或者使用模板 -》commit changes

 

 

 

 

 3、展示流水线(pipeline)的效果:编译(build)、测试(test,包括test1,test2)、部署(deploy)的流水线

以模板的改写为例

.gitlab-ci.yml中键入

# This file is a template, and might need editing before it works on your project.
# see https://docs.gitlab.com/ce/ci/yaml/README.html for all available options

# you can delete this line if you're not using Docker
image: busybox:latest

before_script:
  - echo "Before script section"
  - echo "For example you might run an update here or install a build dependency"
  - echo "Or perhaps you might print out some debugging details"
  - ping -c 5 127.0.0.1
   
after_script:
  - echo "After script section"
  - echo "For example you might do some cleanup here"
  - ping -c 6 127.0.0.1
   
build1:
  stage: build
  script:
    - echo "Do your build here"
    - ping -c 10 127.0.0.1
   
test1:
  stage: test
  script: 
    - echo "Do a test here"
    - echo "For example run a test suite"
    - ping -c 11 127.0.0.1
   
test2:
  stage: test
  script: 
    - echo "Do another parallel test here"
    - echo "For example run a lint test"
    - ping -c 12 127.0.0.1
   
deploy1:
  stage: deploy
  script:
    - echo "Do your deploy here"
    - ping -c 20 127.0.0.1

  

yml中脚本内容的说明:

(1)一个yml文件,就是一个流水线(pipeline)

(2)该流水线中包含了3个阶段(stage,包括build、test、deploy)

(3)每个阶段包含了1到2个作业(job)。

build阶段:包含build1作业

test阶段:包含test1和test2作业(共2个作业)

deploy阶段:包含deploy1作业

注意:当只有一个runner时,作业之间的相互关系是串行的,不是并行的。即前面的作为没有执行完成、或者执行失败,后面的作业就都不再执行。

 

 (4)关键词作业

before_script关键词作业:会在阶段中的每个自定义作业执行前,都重新执行一遍。
after_script关键词作业:会在阶段中的每个自定义作业执行后(不论成功还是失败),都重新执行一遍。