jenkins實現持續集成
- 搭建jenkins環境,安裝插件
- 建立pipeline公用類庫,文件夾vars,默認的
- 添加.groovy文件,可以由以下幾個類庫組成
- dockerImageBuild 負責構建項目鏡像
- dockerImageDeploy 負責將鏡像推到倉庫
- dockerServiceStart 負責啟動docker服務
- gitCheckout 負責遷出項目源碼
- servicePipeline 程序入口
- 根據項目添加對應的.jenkinsfile文件
- jenkins里建立文件夾,選擇公用類庫的倉庫,起個名字
- 在文件夾里建立新的Job,然后選擇對象的jenkinsfile文件
- 項目構建完成
相關源代碼分享
- gitCheckout源代碼
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
echo 'Checking out code from "' + config.repoUrl + '" with credentialsId "' + \
config.credentialsId + '" ...'
checkout([$class: 'GitSCM', branches: [[name: config.branches]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[credentialsId: config.credentialsId, url: config.repoUrl]]])
}
- servicePipeline.groovy源碼
def call(body) {
def config = [:]
body.resolveStrategy = Closure.DELEGATE_FIRST
body.delegate = config
body()
pipeline {
agent { label 'build-server' }
stages {
stage('Initialization') {
steps {
script {
echo "項目初始化"
}
}
}
stage('CI') {
steps{
script{
gitCheckout{
repoUrl = config.repoUrl
credentialsId = config.credentialsId
branches = config.branches
commit = config.commit
}
}
}
}
stage('Service') {
echo "啟動dockder服務"
}
}
}
}
- lind.jenkinsfile項目部署源碼
@Library("lind-library") _
servicePipeline ( {
repoUrl = "git@github.com:bfyxzls/LindDotNetCore.git"
credentialsId = "012f0d4e-47e2-48ce-8b9e-cd399e9b3d61"
branches = "dev"
})
事實上,上面的構建只是一個基礎版本,當你的構建有多種語言組成時,你需要為他們分別寫一個入口,如dotnet,nodejs,java,php,它們有自己的入口,然后再servicePipeline里去分別處理,前開發人員輸入語言類型,然后統一進行處理。