前言
之前小玩了一下Jenkins的Free-style專案,接著來玩玩Pipeline吧~
建立Pipeline
點選新增作業

輸入item name並選Pipeline

設定
General

Build Triggers

Build Triggers

Pipeline
pipeline {
agent any
stages {
stage('Stage 1') {
steps {
echo 'Hello stage 1'
}
}
stage('Stage 2') {
steps {
echo 'Hello stage 2'
}
}
}
}

建立成功

接著來測試看看,按下馬上建置,旁邊就會出現建置的過程和有無成功

也可以按Log來看過程哦


接著來玩玩用gitlab push來觸發
但這邊有個小問題,因為設定時需要對應Pipeline專案名稱,
但因為現在的名稱有空白,網頁url有空白會有問題,所以要先換一下名稱

接著到GitLab中設定jenkins ci


接著push一個commit,可以看到Jenkins被觸發囉

結合go test
因為要build go的執行檔,所以要先裝go plugin
安裝plugin並重啟

接著點選Global Tool Configuration

輸入名稱及指定的golang版本

接著先來拿credential id等等會用到,找到等等要pull的專案的credential




來修改專案的Pipeline
pipeline {
agent any
tools {
go 'go_1_13_15'
}
environment {
GOPATH = ''
CGO_ENABLED = 0
GO111MODULE = 'on'
}
stages {
stage('Checkout git project') {
steps {
git branch: 'master',
credentialsId: 'a0abXXXX-XXXX-XXXX-XXXX-XXXX668679eb',
url: 'http://gitlab.example.com/Zonda/todolist-server.git'
sh "ls -lat"
}
}
stage('Compile') {
steps {
sh 'go mod download'
sh 'go build'
}
}
stage('Test') {
steps {
sh 'go test -cover ./...'
}
}
}
}

更新完後按下馬上建置,就成功囉

Jenkinsfile
剛剛是在Jenkins上做設定,但這樣有個問題是,其他人不知道目前Pipeline的流程,以及可能會依據不同的功能出現Pipeline會需要修改,因此現在就是要讓設定跟著git repo一起版控拉
首先在Pipeline中變更設定成Pipeline script from SCM
,並填完相關資訊

接著在專案中建立Jenkinsfile
pipeline {
agent any
tools {
go 'go_1_13_15'
}
environment {
GOPATH = ''
CGO_ENABLED = 0
GO111MODULE = 'on'
}
stages {
stage('Compile') {
steps {
sh 'go mod download'
sh 'go build'
}
}
stage('Test') {
steps {
sh 'go test -cover ./...'
}
}
}
}
在commit並push上remote,因我們的pipeline有設定push trigger,因此Pipeline就會開始動拉

Reference
Create a Continuous Deployment Pipeline with Golang and Jenkins
Building Go projects using modules on Jenkins