官网地址 https://pkg.jenkins.io/redhat-stable/
[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
# 创建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))
启动成功
Freestyle Job需要在页面添加模块配置项与参数完成配置;每个Job仅能实现一个开发功能;无法将配置代码化,不利于Job配置迁移与版本控制;逻辑相对简单,无需额外学习成本。
Pipeline Job匹配持续集成与持续交付的概念;所有模块、参数配置都可以体现为一个pipeline脚本;可定义多个stage构建一个管道工作集;所有配置代码化,方便Job配置迁移与版本控制;需要Pipeline脚本语法基础。
[root@jenkins ~]# vim /etc/hosts
# 文件末尾添加如下一条记录
192.168.200.157 gitlab.example.com
[root@jenkins ~]# yum install -y git curl
[root@jenkins ~]# git config --system http.sslVerify false
[root@jenkins ~]# echo $?
0
[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]
#!/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..."
可以看到已经成功构建。
所有代码包裹在pipeline{}层内
stages{}层用来包含该pipeline所有stage子层
stage{}层用来包含具体我们需要编写任务的steps{}子层
steps{}用来添加我们具体需要调用的模块语句
首先登录到Jenkins web 管理页
点击“New 任务”
#!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..."
}
}
}
}
}
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”,证明构建成功。