Jenkins+Gitlab+Ansible自動化部署(三)

接Jenkins+Gitlab+Ansible自動化部署(一)http://www.javashuo.com/article/p-tzgbojrp-kw.html 和(二)https://www.cnblogs.com/zd520pyx1314/p/10213549.html html

Jenkins是一個開源持續集成工具,提供了軟甲你開發的持續集成服務,支持主流軟件配置管理,配合實現軟件配置管理,持續集成功能。是主流的運維開發平臺,兼容全部主流開發環境,插件市場可與海量業內主流開發工具實現集成,Job爲配置單位與日誌管理,使運維與開發人員能協同工做。豐富的權限管理劃分不一樣Job不一樣角色;強大的負載均衡功能,保證咱們項目的可靠性。java

Jenkins的安裝、配置與管理node

添加Jenkins yum倉庫git

官網地址
https://pkg.jenkins.io/redhat-stable/

安裝web

[root@jenkins ~]# wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
[root@jenkins ~]# rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
安裝Java
[root@jenkins ~]# yum install -y java
[root@jenkins ~]# java -version
openjdk version "1.8.0_191"
OpenJDK Runtime Environment (build 1.8.0_191-b12)
OpenJDK 64-Bit Server VM (build 25.191-b12, mixed mode)
安裝Jenkins
[root@jenkins ~]# yum list | grep 'jenkins'
jenkins.noarch  
[root@jenkins ~]# yum install -y jenkins

建立Jenkins系統服務用戶並配置shell

建立Jenkins系統服務用戶
[root@jenkins ~]# useradd deploy
[root@jenkins ~]# cp /etc/sysconfig/jenkins{,.bak}
[root@jenkins ~]# vim /etc/sysconfig/jenkins
# 大約在29行,改成deploy用戶
29 JENKINS_USER="deploy"
# 肯定Jenkins端口號8080
56 JENKINS_PORT="8080"
更改目錄權限
[root@jenkins ~]# chown -R deploy:deploy /var/lib/jenkins
[root@jenkins ~]# chown -R deploy:deploy /var/log/jenkins/
啓動Jenkins
[root@jenkins ~]# systemctl start jenkins
[root@jenkins ~]# lsof -i:8080
# 這裏發現端口沒起來,查看日誌發現
[root@jenkins ~]# cat /var/log/jenkins/jenkins.log
java.io.FileNotFoundException: /var/cache/jenkins/war/META-INF/MANIFEST.MF (Permission denied)
# 而後賦予deploy目錄權限
[root@jenkins ~]# chown -R deploy:deploy /var/cache/jenkins/
[root@jenkins ~]# systemctl restart jenkins
[root@jenkins ~]# lsof -i:8080
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
java    4086 deploy  163u  IPv6  49665      0t0  TCP *:webcache (LISTEN)
啓動成功

登陸jenkins web管理界面vim

點擊「Start using jenkins」安全

Jenkins Job構建架構

Freestyle Job與Pipeline Job區別:負載均衡

Freestyle Job須要在頁面添加模塊配置項與參數完成配置;每一個Job僅能實現一個開發功能;沒法將配置代碼化,不利於Job配置遷移與版本控制;邏輯相對簡單,無需額外學習成本。

Pipeline Job匹配持續集成與持續交付的概念;全部模塊、參數配置均可以體現爲一個pipeline腳本;可定義多個stage構建一個管道工做集;全部配置代碼化,方便Job配置遷移與版本控制;須要Pipeline腳本語法基礎。

Jenkins Job構建之環境準備(添加Jenkins後臺git client user與email)

1.配置Jenkins server本地GItlab DNS

[root@jenkins ~]# vim /etc/hosts
# 文件末尾添加以下一條記錄
192.168.244.130 gitlab.example.com

2.安裝git client,curl工具依賴

[root@jenkins ~]# yum install -y git curl

3. 關閉系統git http.sslVerify安全認證

[root@jenkins ~]# git config --system http.sslVerify false
[root@jenkins ~]# echo $?
0

4.添加Jenkins後臺git client user與email

首先登陸Jenkins web管理頁面

在Git plugin選項中填寫如下信息,點擊保存

接下來添加憑據,點擊「憑據」

 

點擊「全局憑據」

點擊「添加憑據」

添加完成會提示以下圖所示

接着添加一個Jenkins freestyle job

點擊「New 任務」

填寫描述信息

添加參數

接着點擊添加「文本參數」 

添加完成後點擊「save」便可,接着回到Jenkins首頁,點擊剛纔建立的「test-freestyle-job」黑色小三角,找到「configure」選項,開始添加git源碼管理

使用root登陸gitlab,複製test-repo倉庫地址

粘貼至下面

 

 

接着進行「build 配置」

在如下框內粘貼

#!/bin/sh

export PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin"

# Print env variable
echo "[INFO] Print env variable"
echo "Current deployment envrionment is $deploy_env" >> test.properties
echo "THe build is $version" >> test.properties
echo "[INFO] Done..."

# Check test properties
echo "[INFO] Check test properties"
if [ -s test.properties ]
then
  cat test.properties
  echo "[INFO] Done..."
else
  echo "test.properties is empty"
fi

echo "[INFO] Build finished..."

接下來點擊「Build with Parameters」

提示失敗,點擊紅色失敗按鈕,查看日誌並解決

能夠看出仍是以前的git有點問題,回到test-freestyle-job配置項,查看並確認

而後從新構建

能夠看到已經成功構建。

接下來演示Jenkins Pipeline Job構建過程

Pipeline基礎架構

1.全部代碼包裹在pipeline{}層內

2.stages{}層用來包含該pipeline全部stage子層

3.stage{}層用來包含具體咱們須要編寫任務的steps{}子層

4.steps{}用來添加咱們具體須要調用的模塊語句

agent區域

  • agent定義pipeline在哪裏運行,能夠使用any,none,或具體的Jenkins node主機名等;例如:假定咱們要特指在node1上執行,能夠寫成:agent{node1 {label 'node1'}}。

environment區域

  • 「變量名稱=變量值」定義咱們的環境變量;
  • 能夠定義全局環境變量,應用全部stage任務
  • 能夠定義stage環境變量,應用單獨的stage任務

script區域(可選)

  • 在steps內定義script{};
  • groovy腳本語言;
  • 用來進行腳本邏輯運算;

經常使用steps區域

  • echo:打印輸出
  • sh:調用Linux系統shell命令
  • git url:調用git模塊進行git相關操做

開始構建Jenkins Pipeline Job

首先登陸到Jenkins web 管理頁

點擊「New 任務」

添加描述信息

 

添加pipeline script

 

pipeline script腳本內容(用上述複製下來的ID粘貼至credentialsId後)

#!groovy

pipeline {
    agent {node {label 'master'}}

    environment {
        PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"
    }

    parameters {
        choice(
            choices: 'dev\nprod',
            description: 'choose deploy environment',
            name: 'deploy_env'
            )
        string (name: 'version', defaultValue: '1.0.0', description: 'build version')
    }

    stages {
        stage("Checkout test repo") {
            steps{
                sh 'git config --global http.sslVerify false'
                dir ("${env.WORKSPACE}") {
                    git branch: 'master', credentialsId:"b974bdfd-bb73-4f0a-8a0d-85d867681ed0", url: 'https://root@gitlab.example.com/root/test-repo.git'
                }
            }
        }
        stage("Print env variable") {
            steps {
                dir ("${env.WORKSPACE}") {
                    sh """
                    echo "[INFO] Print env variable"
                    echo "Current deployment environment is $deploy_env" >> test.properties
                    echo "The build is $version" >> test.properties
                    echo "[INFO] Done..."
                    """
                }
            }
        }
        stage("Check test properties") {
            steps{
                dir ("${env.WORKSPACE}") {
                    sh """
                    echo "[INFO] Check test properties"
                    if [ -s test.properties ]
                    then 
                        cat test.properties
                        echo "[INFO] Done..."
                    else
                        echo "test.properties is empty"
                    fi
                    """

                    echo "[INFO] Build finished..."
                }
            }
        }
    }
}

「保存」以後,點擊「當即構建」

報錯,點擊查看報錯信息

根據錯誤提示:沒有找到對應參數的變量,是由於首次構建pipeline job時,參數沒有被引用到當前pipeline job當中,返回test-pipeline-job主界面,此時的「當即構建」按鈕會變爲「Build with Parameters」,點擊「Build with Parameters」

能夠看到第二次構建是成功的,點擊#2前的藍色圓球查看輸出信息

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/test-pipeline-job
[Pipeline] {
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Checkout test repo)
[Pipeline] sh
+ git config --global http.sslVerify false
[Pipeline] dir
Running in /var/lib/jenkins/workspace/test-pipeline-job
[Pipeline] {
[Pipeline] git
 > git rev-parse --is-inside-work-tree # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://root@gitlab.example.com/root/test-repo.git # timeout=10
Fetching upstream changes from https://root@gitlab.example.com/root/test-repo.git
 > git --version # timeout=10
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://root@gitlab.example.com/root/test-repo.git +refs/heads/*:refs/remotes/origin/*
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef
 > git branch -a -v --no-abbrev # timeout=10
 > git branch -D master # timeout=10
 > git checkout -b master dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef
Commit message: "Merge branch 'release-1.0.0' into 'master'"
 > git rev-list --no-walk dd39fbeeb70dd5e2d545dfe084c3d540d106d6ef # timeout=10
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Print env variable)
[Pipeline] dir
Running in /var/lib/jenkins/workspace/test-pipeline-job
[Pipeline] {
[Pipeline] sh
+ echo '[INFO] Print env variable'
[INFO] Print env variable
+ echo 'Current deployment environment is dev'
+ echo 'The build is 1.0.0'
+ echo '[INFO] Done...'
[INFO] Done...
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Check test properties)
[Pipeline] dir
Running in /var/lib/jenkins/workspace/test-pipeline-job
[Pipeline] {
[Pipeline] sh
+ echo '[INFO] Check test properties'
[INFO] Check test properties
+ '[' -s test.properties ']'
+ cat test.properties
Current deployment environment is dev
The build is 1.0.0
+ echo '[INFO] Done...'
[INFO] Done...
[Pipeline] echo
[INFO] Build finished...
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

能夠看到輸出狀態爲「SUCCESS」,證實構建成功。

相關文章
相關標籤/搜索