將自由風(fēng)格項目轉(zhuǎn)換為管道項目 CI/CD
如今,許多公司都使用Jenkins完成了他們的持續(xù)集成,測試和持續(xù)部署。他們中的大多數(shù)使用freestyle作為默認(rèn)項目類型,但這有其自身的局限性。根據(jù)需要,我最近開始將所有Freestyle遷移到Pipeline項目。那么什么時候觸發(fā)這些工作呢?開發(fā)人員/所有者通過推送/提交更新存儲庫后,jenkins作業(yè)將觸發(fā)這些作業(yè)-將生成一個二進(jìn)制文件,另一個將運行單元測試以檢查代碼覆蓋率。由于代碼覆蓋率單元測試需要大量時間才能完成,因此將這兩個任務(wù)分成兩個工作的必要性上升了。只要存儲庫中有更新,就會觸發(fā)此作業(yè),并在限制運行和執(zhí)行構(gòu)建前和構(gòu)建后步驟的計算機(jī)中檢入代碼。
自由風(fēng)格項目
全局配置
GitHub存儲庫配置

啟用webhook配置

基于Shell的構(gòu)建步驟

發(fā)布-根據(jù)結(jié)果構(gòu)建任務(wù)
觸發(fā)電子郵件通知,以在構(gòu)建執(zhí)行后通知項目所有者

為單元測試作業(yè)創(chuàng)建了相同的作業(yè)類型,在Build shell中進(jìn)行了很少的改動,并添加了一些單元測試代碼。
為什么要轉(zhuǎn)換成Pipeline項目?
Freestyle的主要問題之一是,它不允許超過1個存儲庫的SCM輪詢webhook觸發(fā)器。這是我們的主要擔(dān)憂,為管道遷移鋪平了道路。上面的快照涵蓋了將近7項任務(wù),而單元測試的任務(wù)數(shù)約為10。那么我們可以使用管道代碼來執(zhí)行所有任務(wù)。下面是從上面的Freestyle轉(zhuǎn)換而來的一個
- WSPACE = '/var/jenkins/workspace/Directory_Name/'
 - BRWSPACE = '/var/jenkins/workspace/'
 - pipeline {
 - agent {
 - node {
 - label 'Node_Name'
 - customWorkspace "${WSPACE}"
 - }
 - }
 - //清空構(gòu)建目錄
 - stages {
 - stage('Cleaning up the previous directory') {
 - steps {
 - echo 'Deleteing the directory'
 - sh "rm -rf /var/jenkins/workspace/Directory_Name/* "
 - }
 - }
 - // 下載代碼和依賴
 - stage('Checking out build repo and its dependencies') {
 - steps {
 - dir("${WSPACE}/RepoName") {
 - git branch: 'master',
 - credentialsId: 'UserName',
 - url: 'https://github.com/path/repo.git'
 - }
 - dir("${WSPACE}/dir") {
 - git branch: 'master',
 - credentialsId: 'UserName',
 - url: 'https://github.com/path/repo1.git'
 - }
 - dir("${WSPACE}/dir3") {
 - git branch: 'master',
 - credentialsId: 'UserName2',
 - url: 'https://github.com/path/repo4.git'
 - }
 - }
 - }
 - //執(zhí)行構(gòu)建
 - stage('Versioning and executing the build') {
 - steps {
 - dir ("${WSPACE}/repo1") {
 - script{
 - sh label: '', script: '''/usr/bin/env
 - cd /var/jenkins/workspace/
 - original=`cat patch_info`
 - MAJOR=`cat patch_info | cut -d "." -f1`
 - MINOR=`cat patch_info | cut -d "." -f2`
 - PATCH=`cat patch_info | cut -d "." -f3`
 - New_Value=`expr $PATCH + 1`
 - New_Version=$MAJOR.$MINOR.$New_Value
 - sed -i "s/$original/$New_Version/g" patch_info
 - echo "$New_Version"
 - cd /var/jenkins/workspace/path/repo4/
 - echo "Starting the Unit Testing"
 - export GOPATH=$HOME/go
 - export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
 - make format
 - make clean build
 - if make unit-test ; then
 - cd /var/jenkins/workspace/path/repo1/dir
 - else
 - cd /var/jenkins/workspace/path/repo2/dir2
 - fi
 - if make unit-test ; then
 - echo " unit testing completed"
 - fi
 - '''
 - }
 - }
 - }
 - }
 - //發(fā)布HTML報告
 - stage ('Publish HTML Report') {
 - steps {
 - dir ("/jenkins/workspace/") {
 - script{
 - sh label: '', script: '''/usr/bin/env
 - perl /jenkins/generate_build_meta_data.pl -jr http://gitlab.com:8080 -bNum ${BUILD_NUMBER} -bName ${JOB_NAME} -o /tmp -t /jenkins/template.html
 - export GOPATH=$HOME/go
 - export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin
 - cd /var/jenkins/workspace/path/repo1/service/
 - go tool cover -html=c.out -o coverage.html
 - cd /var/jenkins/workspace/path/repo2/dir3
 - go tool cover -html=c.out -o output.html
 - '''
 - publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false,
 - reportDir: '/tmp/${JOB_NAME}/${BUILD_NUMBER}', reportFiles: '${JOB_NAME}-${BUILD_NUMBER}-manifest.html',
 - reportName: 'Email Output Subject', reportTitles: ''])
 - }
 - }
 - }
 - }
 - //發(fā)送郵件
 - stage ('Send Email') {
 - steps {
 - dir ("${WSPACE}/repo4") {
 - emailext attachmentsPattern: '**/coverage.html,**/dir4.html', body: '${FILE, path="/tmp/${JOB_NAME}/${BUILD_NUMBER}/${JOB_NAME}-${BUILD_NUMBER}-manifest.html"}', subject: 'Unit testing Email Output ${BUILD_NUMBER} Successful', to: "EmailID@Domain2.com, EmailID2@Domain3.com"
 - }
 - }
 - }
 - }
 - }
 
上面的代碼為我們提供了編輯的空間及其凝聚力。管道作業(yè)的一個重要特征是階段的輸出以一種吸引人的方式呈現(xiàn),我發(fā)現(xiàn)這很容易理解正在進(jìn)行的過程。
總結(jié)
創(chuàng)建Freestyle或Pipeline項目完全取決于需求。在定制方面,Pipeline顯示了主要空間,因為自由風(fēng)格是啟動您的第一份Jenkins工作的簡便方法。


















 
 
 















 
 
 
 