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

Jenkins+Gitlab+Ansible自动化部署

(2021-08-10)

一、Jenkins 介绍

  • Jenkins是一个开源持续集成工具,提供了软甲你开发的持续集成服务,支持主流软件配置管理,配合实现软件配置管理,持续集成功能。是主流的运维开发平台,兼容所有主流开发环境,插件市场可与海量业内主流开发工具实现集成,Job为配置单位与日志管理,使运维与开发人员能协同工作。丰富的权限管理划分不同Job不同角色;强大的负载均衡功能,保证我们项目的可靠性。

二、Jenkins 安装配置

1、添加Jenkins yum仓库

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

2、安装 Jenkins

[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_292"
OpenJDK Runtime Environment (build 1.8.0_292-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
安装Jenkins
[root@jenkins ~]# yum list | grep 'jenkins'
jenkins.noarch                              2.289.3-1.1                 
[root@jenkins ~]# yum install -y jenkins

3、创建Jenkins系统服务用户并配置

# 创建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 ~]# ss -anput| grep 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 ~]# ss -anput| grep 8080
tcp    LISTEN     0      50     [::]:8080               [::]:*                   users:(("java",pid=12724,fd=144))
启动成功

4、登录jenkins web管理界面

Jenkins+Gitlab+Ansible自动化部署(三)_第1张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第2张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第3张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第4张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第5张图片

  • 点击“Start using jenkins”

Jenkins+Gitlab+Ansible自动化部署(三)_第6张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第7张图片

三、Jenkins 构建

1、Freestyle Job与Pipeline Job区别

  • Freestyle Job需要在页面添加模块配置项与参数完成配置;每个Job仅能实现一个开发功能;无法将配置代码化,不利于Job配置迁移与版本控制;逻辑相对简单,无需额外学习成本。

  • Pipeline Job匹配持续集成与持续交付的概念;所有模块、参数配置都可以体现为一个pipeline脚本;可定义多个stage构建一个管道工作集;所有配置代码化,方便Job配置迁移与版本控制;需要Pipeline脚本语法基础。

2、Jenkins Job 构建

  • Jenkins Job构建之环境准备(添加Jenkins后台git client user与email)

1、配置Jenkins server本地GItlab DNS

[root@jenkins ~]# vim /etc/hosts
# 文件末尾添加如下一条记录
192.168.200.157 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管理页面

Jenkins+Gitlab+Ansible自动化部署(三)_第8张图片

  • 在Git plugin选项中填写以下信息,点击保存

Jenkins+Gitlab+Ansible自动化部署(三)_第9张图片

5、接下来添加凭据

  • 点击“凭据”

Jenkins+Gitlab+Ansible自动化部署(三)_第10张图片

  • 点击“全局凭据”

Jenkins+Gitlab+Ansible自动化部署(三)_第11张图片

  • 点击“添加凭据”

Jenkins+Gitlab+Ansible自动化部署(三)_第12张图片

  • 添加完成会提示如下图所示

Jenkins+Gitlab+Ansible自动化部署(三)_第13张图片

6、接着添加一个 Jenkins freestyle job

  • 点击“New 任务”

Jenkins+Gitlab+Ansible自动化部署(三)_第14张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第15张图片

  • 填写描述信息

Jenkins+Gitlab+Ansible自动化部署(三)_第16张图片

  • 添加参数
  • 安装插件 Extended Choice Parameter

Jenkins+Gitlab+Ansible自动化部署(三)_第17张图片

  • 接着点击添加“文本参数”

Jenkins+Gitlab+Ansible自动化部署(三)_第18张图片

  • 添加完成后点击“save”即可,接着回到Jenkins首页,点击刚才创建的“test-freestyle-job”黑色小三角,找到“configure”选项,开始添加git源码管理

Jenkins+Gitlab+Ansible自动化部署(三)_第19张图片

  • 使用root登录gitlab,复制test-repo仓库地址

Jenkins+Gitlab+Ansible自动化部署(三)_第20张图片

  • 粘贴至下面

Jenkins+Gitlab+Ansible自动化部署(三)_第21张图片

  • 配置 gitlab 认证公钥
[root@jenkins ~]# su - deploy
[deploy@jenkins ~]$ ssh-keygen -C [email protected]
Generating public/private rsa key pair.
Enter file in which to save the key (/home/deploy/.ssh/id_rsa):
Created directory '/home/deploy/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/deploy/.ssh/id_rsa.
Your public key has been saved in /home/deploy/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:E3srxrDm2eGg1Ws7DXmmgHN/jdGcygB3xUoju0hrGos [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|           .     |
|        . o o    |
|        .+ +     |
|      o ooo      |
|     o.=S+.o .   |
|    + *==o+.+    |
|   . B=o*O.=     |
|  E o= *=+* .    |
|    . o.=+       |
+----[SHA256]-----+
[deploy@jenkins ~]$ ls
[deploy@jenkins ~]$ cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDbhIFtoCHRZliFq5i0DnBdQFI/eqf33DyCXs/5LooiIN+YtwGyNKJAvB0IdNZgY++6RNqgMlX8uzYDUR7MzNTM/08M1/8EYGKuemM8XzBI7Pl5/d1/OmWqCNsMJLVVuRdp6WwJagxOHwTZM6iFkMcOsfL6EtWSkf8HPS4nTYeZdGN9spZJfl+vsT0aFn2lYQPXLJwT8H01dNAuekjDI8x11Vh6fnwqQr7M0Nk3O3MZFZoMHXzf4eyWykMlXqpOhgPMoytc0gnYElWMWePFXUQ2BF0DcDG617DECfgCBZMI6Ga596ZpU7r1Wp7ojXPqQyk4v+3ni+P1puE39Cmb7e9f [email protected]

Jenkins+Gitlab+Ansible自动化部署(三)_第22张图片

  • 接着进行“build 配置”

Jenkins+Gitlab+Ansible自动化部署(三)_第23张图片

  • 在以下框内粘贴
#!/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”

Jenkins+Gitlab+Ansible自动化部署(三)_第24张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第25张图片

  • 构建

Jenkins+Gitlab+Ansible自动化部署(三)_第26张图片

可以看到已经成功构建。

3、Jenkins Pipeline Job 构建

1、Pipeline 基础架构

  • 所有代码包裹在pipeline{}层内

  • stages{}层用来包含该pipeline所有stage子层

  • stage{}层用来包含具体我们需要编写任务的steps{}子层

  • steps{}用来添加我们具体需要调用的模块语句

2、agent 区域

  • agent定义pipeline在哪里运行,可以使用any,none,或具体的Jenkins node主机名等;例如:假定我们要特指在node1上执行,可以写成:agent{node1 {label ‘node1’}}。

3、environment区域

  • “变量名称=变量值”定义我们的环境变量;
  • 可以定义全局环境变量,应用所有stage任务
  • 可以定义stage环境变量,应用单独的stage任务

4、script区域(可选)

  • 在steps内定义script{};
  • groovy脚本语言;
  • 用来进行脚本逻辑运算;

5、常用steps区域

  • echo:打印输出
  • sh:调用Linux系统shell命令
  • git url:调用git模块进行git相关操作

6、开始构建 Jenkins Pipeline Job

  • 首先登录到Jenkins web 管理页

  • 点击“New 任务”

Jenkins+Gitlab+Ansible自动化部署(三)_第27张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第28张图片

  • 添加描述信息

Jenkins+Gitlab+Ansible自动化部署(三)_第29张图片

  • 添加pipeline script

在这里插入图片描述

Jenkins+Gitlab+Ansible自动化部署(三)_第30张图片

  • 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://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..."
                }
            }
        }
    }
}
  • “保存”之后,点击“立即构建”

Jenkins+Gitlab+Ansible自动化部署(三)_第31张图片

  • 报错,点击查看报错信息

Jenkins+Gitlab+Ansible自动化部署(三)_第32张图片

  • 根据错误提示:没有找到对应参数的变量,是因为首次构建pipeline job时,参数没有被引用到当前pipeline job当 中,返回test-pipeline-job主界面,此时的“立即构建”按钮会变为“Build with Parameters”,点击“Build with Parameters”

Jenkins+Gitlab+Ansible自动化部署(三)_第33张图片

Jenkins+Gitlab+Ansible自动化部署(三)_第34张图片

  • 可以看到第二次构建是成功的,点击#2前的蓝色圆球查看输出信息
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[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
The recommended git tool is: NONE
using credential 82eb8b0a-3396-41dc-b366-19b555f01a2a
 > git rev-parse --resolve-git-dir /var/lib/jenkins/workspace/test-pipeline-job/.git # timeout=10
Fetching changes from the remote Git repository
 > git config remote.origin.url https://gitlab.example.com/root/test-repo.git # timeout=10
Fetching upstream changes from https://gitlab.example.com/root/test-repo.git
 > git --version # timeout=10
 > git --version # 'git version 1.8.3.1'
using GIT_ASKPASS to set credentials 
 > git fetch --tags --progress https://gitlab.example.com/root/test-repo.git +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
Checking out Revision d043b03d188dad94a05dbd98965639de340fec96 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f d043b03d188dad94a05dbd98965639de340fec96 # timeout=10
 > git branch -a -v --no-abbrev # timeout=10
 > git checkout -b master d043b03d188dad94a05dbd98965639de340fec96 # timeout=10
Commit message: "Merge branch 'release-1.0.0' into 'master'"
First time build. Skipping changelog.
[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”,证明构建成功。

你可能感兴趣的:(#,运维,gitlab,jenkens,jenkins)