GitLab CI脚本书写

1、GitLab CI脚本基本语法

GitLab CI Runner常用的有两种,一种是Shell的,另一种是Docker的,采用Docker Runner的话需要指定具体镜像,脚本中可以通过tags指定执行该作业的Runner

基本语法案例

image: xxx # 基础镜像,如果使用docker runner但是没有指定镜像时默认采用此镜像

before_script: # 定义在每个作业的脚本之前运行的命令
  - xxx

after_script: # 定义在每个作业的脚本之后运行的命令
  - xxx

stages: # 定义作业可以使用的阶段,如果任何一步作业失败,则不会执行下一步的作业
  - build
  - test
  - deploy

build: # 工作
  stage: build # build阶段
  tags: # 指定运行该脚本的Runner
    - dev-build
  only: # 定义作业将要运行的分支和标签的名称
    - tags # 打tag时才会执行
    - web # 只有在web界面进行操作才会执行
  script: # 由Runner执行的shell脚本
    - build_image

2、使用GitLab CI+jib实现自动打包镜像

pom.xml中jib相关配置

            <plugin>
                <groupId>com.google.cloud.tools</groupId>
                <artifactId>jib-maven-plugin</artifactId>
                <version>1.6.1</version>
                <configuration>
                    <from>
                        <image>${base_image}</image>
                    </from>
                    <to>
                        <image>${registry_url}/${registry_project}/${project.artifactId}:${image_tag}</image>
                        <auth>
                            <username>${registry_username}</username>
                            <password>${registry_password}</password>
                        </auth>
                    </to>
                    <allowInsecureRegistries>true</allowInsecureRegistries>
                    <container>
                        <jvmFlags>
                            <jvmFlag>-Djava.security.edg=file:/dev/./urandom</jvmFlag>
                        </jvmFlags>
                        <user>app:app</user>
                    </container>
                </configuration>
            </plugin>

.gitlab-ci.yml文件

stages:
  - build

build:
  stage: build
  tags:
    - dev-build
  only:
    - tags
    - web
  script:
    - build_image

.auto_devops: &auto_devops |

  function build_image(){
      mvn compile jib:build \
      -Dbase_image=基础镜像地址 \
      -Dregistry_url=镜像仓库地址 \
      -Dregistry_project=镜像仓库中的项目名 \
      -Dregistry_username=用户名 \
      -Dregistry_password=密码 \
      -Dimage_tag=$CI_COMMIT_REF_NAME 
  }

before_script:
  - *auto_devops

build使用的Runner为Shell Runner,$CI_COMMIT_REF_NAME可以得到在Gitlab打tag时指定的tag,最终实现的效果就是通过在GitLab上打tag使用jib自动打包镜像

发布了192 篇原创文章 · 获赞 447 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/qq_40378034/article/details/104873348