DevOps GitLab CICD 實踐3——CI文件編寫

前置步驟:html

官方文檔

編寫結構相似Jenkins pineline流水線java

GitLab CI/CD Pipeline Configuration Referencelinux

GitLab CI/CD Pipeline Configuration Reference

GitLab CI/CD pipelines are configured using a YAML file called .gitlab-ci.yml within each project.git

The .gitlab-ci.yml file defines the structure and order of the pipelines and determines:github

  • What to execute using GitLab Runner.
  • What decisions to make when specific conditions are encountered. For example, when a process succeeds or fails.

This topic covers CI/CD pipeline configuration. For other CI/CD configuration information, see:spring

We have complete examples of configuring pipelines:docker

目標

  • maven項目自動打包
  • 自動測試
  • 自動鏡像構建並上傳

官方參數表列出了做業的可用參數:api

關鍵詞 描述
script 由Runner執行的Shell腳本。
image 使用泊塢窗圖像。也可用:image:nameimage:entrypoint
services 使用docker services圖像。也可用:services:nameservices:aliasservices:entrypoint,和services:command
before_script 覆蓋在做業以前執行的一組命令。
after_script 覆蓋做業後執行的一組命令。
stages 定義管道中的階段。
stage 定義做業階段(默認值:) test
only 建立做業時限制。也可用:only:refsonly:kubernetesonly:variables,和only:changes
except 在未建立做業時限制。也可用:except:refsexcept:kubernetesexcept:variables,和except:changes
tags 用於選擇Runner的標籤列表。
allow_failure 讓工做失敗。失敗的做業無助於提交狀態。
when 何時開始工做。也可用:when:manualwhen:delayed
environment 做業部署到的環境的名稱。也可用:environment:nameenvironment:urlenvironment:on_stop,和environment:action
cache 後續運行之間應緩存的文件列表。也可用:cache:pathscache:keycache:untracked,和cache:policy
artifacts 成功附加到做業的文件和目錄列表。也可用:artifacts:pathsartifacts:nameartifacts:untrackedartifacts:whenartifacts:expire_inartifacts:reports,和artifacts:reports:junit。 在GitLab 企業版,這些都是可供選擇:artifacts:reports:codequalityartifacts:reports:sastartifacts:reports:dependency_scanningartifacts:reports:container_scanningartifacts:reports:dastartifacts:reports:license_management,和artifacts:reports:performance
dependencies 做業所依賴的其餘做業,以便您能夠在它們之間傳遞工件。
coverage 給定做業的代碼覆蓋率設置。
retry 在發生故障的狀況下,能夠自動重試做業的次數和次數。
parallel 應該並行運行多少個做業實例。
trigger 定義下游管道觸發器。
include 容許此做業包含外部YAML文件。也可用:include:localinclude:fileinclude:template,和include:remote
extends 此做業將繼承的配置條目。
pages 上傳做業結果以用於GitLab Pages。
variables 在做業級別定義做業變量。

注: 參數typestype棄用緩存

參考

官方構建案例app

Deploy a Spring Boot application to Cloud Foundry with GitLab CI/CD

參考其腳本

Configure GitLab CI/CD to deploy your application

Now we need to add the GitLab CI/CD configuration file (.gitlab-ci.yml) to our project’s root. This is how GitLab figures out what commands need to be run whenever code is pushed to our repository. We will add the following .gitlab-ci.yml file to the root directory of the repository, GitLab will detect it automatically and run the steps defined once we push our code:

image: java:8

stages:
 - build
 - deploy

build:
 stage: build
 script: ./mvnw package
 artifacts:
 paths:
 - target/demo-0.0.1-SNAPSHOT.jar

production:
 stage: deploy
 script:
 - curl --location "https://cli.run.pivotal.io/stable?release=linux64-binary&source=github" | tar zx - ./cf login -u $CF_USERNAME -p $CF_PASSWORD -a api.run.pivotal.io - ./cf push  only:
 - master
複製代碼

配置

全局變量

進入工程CI設置配置全局變量腳本,包含鏡像倉庫登錄名稱、密碼、打包名稱等

配置全局變量目的在於配置腳本中不該該包含密碼等敏感信息

1554278638713.png

若但願使用GitLab內置環境變量,可參考官方表格

GitLab CI/CD environment variables

CI腳本

結合可用參數和樣例配置,根據已經存在的SpringBoot項目編寫響應的CI腳本.gitlab-ci.yml

image: docker:stable
services:
 - docker:dind

variables:
 DOCKER_DRIVER: overlay
 SPRING_PROFILES_ACTIVE: gitlab-ci

stages:
 - build
 - package

maven-build:
 image: maven:3-jdk-8
 stage: build
 script: "mvn package -B"
 artifacts:
 paths:
 - target/*.jar

docker-build:
 stage: package
 script:
 - docker build -t $CONTAINER_IMAGE:latest .
 - docker login -u $DOCKER_HUB_USER -p $DOCKER_HUB_PASS
 - docker push $CONTAINER_IMAGE:latest
複製代碼

該腳本對於知足腳本目標的工程均可複用,若須要測試,則增長相關的測試步驟便可

此處因爲工程包含外部依賴關係,不在構建時測試

自動構建

默認的觸發構建事件爲commit

觸發後會自動執行任務

1554278973797.png

1554279027476.png

Maven自動觸發構建

1554279066238.png

自動鏡像打包

1554279219445.png

1554279272711.png

自動上傳(推送)鏡像

1554279311054.png

執行結果

1554279508887.png

若執行失敗則會發送郵件給開發人員

1554279616575.png

CICD全套流程目標實現!

總結

無論使用GitLab CICD或者是使用Jenkins+Ansible的方式,目標都在於踐行DevOps,打通開發與運維流程,一旦配置好CICD流程,日後開發人員便再也不操心打包、構建等操做,能夠專一開發

相關文章
相關標籤/搜索