Getting started and installing Jenkins

1. Experimental environment


selinux iptables off

CPU name IP system version
gitlab 10.10.10.200 rhel7.5
jenkins 10.10.10.10 rhel7.5
tomcat 10.10.10.11 rhel7.5

2. Install jenkins


1. Unzip the installation package

Download address: https://download.docker.com/linux/static/stable/x86_64/

[root@jenkins ~]# tar xf docker-18.06.3-ce.tgz
[root@jenkins ~]# cd docker/
[root@jenkins docker]# cp * /usr/bin/

2. Configure docker.service

[root@jenkins ~]# vim /etc/systemd/system/docker.service
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service
Wants=network-online.target
 
[Service]
Type=notify
 
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd
ExecReload=/bin/kill -s HUP $MAINPID
 
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity

# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
#TasksMax=infinity
TimeoutStartSec=0
 
# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes
 
# kill only the docker process, not all processes in the cgroup
KillMode=process
 
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s
 
[Install]
WantedBy=multi-user.target

3. Start docker

[root@jenkins ~]# chmod +x /etc/systemd/system/docker.service
[root@jenkins ~]# systemctl daemon-reload
[root@jenkins ~]# systemctl enable docker && systemctl restart docker

4. Modify the image source


[root@jenkins ~]# cat /etc/docker/daemon.json
{
 "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
}

[root@jenkins ~]# systemctl restart docker

3. gitlab installation


1. Installation

[root@gitlab ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
[root@gitlab ~]# yum install -y gitlab-ce

2. Initialize GitLab configuration

[root@gitlab ~]# gitlab-ctl reconfigure

Other commands:

# 启动 gitlab 服务
gitlab-ctl start
# 停止 gitlab 服务
gitlab-ctl stop

3. Browser login

http://10.10.10.200

[root@gitlab ~]# cat /etc/gitlab/initial_root_password

Insert image description here

Insert image description here

4. Change root password

Insert image description here

Insert image description here
Insert image description here

5. Upload project

Insert image description here

4. tomcat installation


1. Decompress

[root@server1 ~]# tar xf jdk-8u45-linux-x64.tar.gz
[root@server1 ~]# mv jdk1.8.0_45/ /usr/local/jdk

2. Declare variables

[root@server1 ~]# vim /etc/profile
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$PATH:$JAVA_HOME/bin

[root@server1 ~]# . /etc/profile

3. Install Tomcat

Official website download address: https://tomcat.apache.org/download-80.cgi

[root@server1 ~]# tar xf apache-tomcat-8.5.59.tar.gz
[root@server1 ~]# mv apache-tomcat-8.5.59 /usr/local/tomcat

4. Start Tomcat

[root@server1 ~]# /usr/local/tomcat/bin/startup.sh 
[root@server1 ~]# /usr/local/tomcat/bin/shutdown.sh       ###关闭

5. View Tomcat

Insert image description here

5. jenkins installation


1. Pull the image

[root@jenkins ~]# docker pull jenkins/jenkins:lts
[root@jenkins ~]# docker images
REPOSITORY             TAG          IMAGE ID       CREATED        SIZE
jenkins/jenkins        lts          825c3e86c65d   4 weeks ago    471MB

2. Unzip the file

JDK official download address: https://www.oracle.com/java/technologies/downloads/
JDK third-party download address: http://www.codebaoku.com/jdk/jdk-oracle-jdk1-8.html
maven download address :https://mirrors.aliyun.com/apache/maven/maven-3/

[root@jenkins ~]# tar xf jdk-8u45-linux-x64.tar.gz
[root@jenkins ~]# mv jdk1.8.0_45/ /usr/local/jdk
[root@jenkins ~]# tar xf apache-maven-3.5.0-bin.tar.gz
[root@jenkins ~]# mv apache-maven-3.5.0 /usr/local/maven

3. Configure maven Alibaba Cloud warehouse

[root@jenkins ~]# vim /usr/local/maven/conf/settings.xml
<mirrors>
  <mirror>
    <id>central</id>
    <mirrorOf>central</mirrorOf>
    <name>aliyun maven</name>
    <url>https://maven.aliyun.com/repository/public</url>
  </mirror>
</mirrors>

4. Run jenkins

docker run -d --name jenkins -p 8080:8080 -p 50000:50000 -u root \
-v /opt/jenkins_home:/var/jenkins_home \
-v /usr/local/maven:/usr/local/maven \
-v /usr/local/jdk:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
-v /usr/bin/docker:/usr/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock \
-e TZ="Asia/Shanghai" \
--restart=always \
jenkins/jenkins:lts

5. View password

[root@jenkins ~]# cd /opt/jenkins_home/
[root@jenkins jenkins_home]# sed -i ‘s#https://updates.jenkins.io/update-center.json#http://mirror.esuni.jp/jenkins/updates/update-center.json#g’ hudson.model.UpdateCenter.xml
[root@jenkins jenkins_home]# systemctl restart docker

[root@jenkins ~]# cat /opt/jenkins_home/secrets/initialAdminPassword
375dd3d01ef9411b833a8860d028683d

6. Log in via password

Insert image description here

7. Install plug-ins

Insert image description here
Insert image description here

8. Create an administrator user

Insert image description here

9. Installation and restart

Insert image description here
Insert image description here

10. Modify to domestic source

[root@jenkins ~]# cd /opt/jenkins_home/updates/
[root@jenkins updates]# sed -i 's/https:\/\/updates.jenkins.io\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json
[root@jenkins updates]# sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json

6. Publish via FreeStyle


Download git parameterand Publish Over SSHplug-in in advance, it is normal for the git parameter version to be lower than 0.95, otherwise an error will be reported: Retrieving Git references, the normal version cannot be obtained

1. Configure Tomcat SSH Server

Manage Jenkins -> Configure System -> Public over SSH

Insert image description here

2. Create FreeStyle project

Insert image description here

3. Configure git parameters

Insert image description here

4. git pull address setting

Insert image description here

5. maven compilation

JAVA_HOME=/usr/local/jdk
PATH=$PATH:$JAVA_HOME/bin
/usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true

Insert image description here

6. Tomcat server execution program

tomcat=/usr/local/tomcat
cd $tomcat/webapps
rm -rf $tomcat/webapps/ROOT
mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
# 部署新程序并重启Tomcat
mv /tmp/$JOB_NAME/*.war  $tomcat/webapps/ROOT.war
pid=$(ps -ef |grep $tomcat |egrep -v 'grep' |awk '{print $2}')
[ -n "$pid" ] && kill -9 $pid
export  JAVA_HOME=/usr/local/jdk
/bin/bash /usr/local/tomcat/bin/startup.sh

Insert image description here

7. Build and view the results

Insert image description here
Insert image description here

7. Publish through Maven


Install pluginMaven IntegrationVersion 3.22

1. Configure JDK and maven

System Management->Global Tool Configuration

Insert image description here

Insert image description here

2. Create a maven project

Insert image description here

3. Configure Git parameters

Insert image description here

4. git pull address setting

Insert image description here

5. maven compilation

clean package -DskipTests=true

Insert image description here

6. Tomcat server execution script

tomcat=/usr/local/tomcat
cd $tomcat/webapps
rm -rf $tomcat/webapps/ROOT
mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
# 部署新程序并重启Tomcat
mv /tmp/$JOB_NAME/*.war  $tomcat/webapps/ROOT.war
pid=$(ps -ef |grep $tomcat |egrep -v 'grep' |awk '{print $2}')
[ -n "$pid" ] && kill -9 $pid
export  JAVA_HOME=/usr/local/jdk
/bin/bash /usr/local/tomcat/bin/startup.sh

Insert image description here

7. Build and view the results

Insert image description here
Insert image description here

8. Publish through Pipeline


1. Create a pipeline project

Insert image description here

2. Configure git parameters

Insert image description here

3. Pipeline syntax

Insert image description here

4. git pull pipeline script

Insert image description here

5. maven compilation pipeline script

Insert image description here

6. Send and start the pipeline script

Insert image description here

7. Configure pipeline script

: If you want to use variables in Publish over ssh, use double quotes, such as sourceFiles (the default is single quotes).
If you want to implement a war package, you can download it: archiveArtifacts

pipeline {
    agent any 
    stages {
        stage('拉取代码') { 
            steps {
                checkout scmGit(branches: [[name: '$Branch']], extensions: [], userRemoteConfigs: [[credentialsId: 'e94bf465-be00-490b-9f24-856106362015', url: 'http://10.10.10.200/wielun/java-test.git']])
            }
        }
        stage('maven编译') { 
            steps {
                sh '''JAVA_HOME=/usr/local/jdk
                PATH=$PATH:$JAVA_HOME/bin
                /usr/local/maven/bin/mvn clean package -Dmaven.test.skip=true'''
            }
        }
        stage('发送到tomcat服务器并启动') { 
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: 'tomcat-test', transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: '''tomcat=/usr/local/tomcat
                cd $tomcat/webapps
                rm -rf $tomcat/webapps/ROOT
                mv *.war /data/backup/$(date +"%F_%T")_ROOT.war
                # 部署新程序并重启Tomcat
                mv /tmp/$JOB_NAME/*.war  $tomcat/webapps/ROOT.war
                pid=$(ps -ef |grep $tomcat |egrep -v \'grep\' |awk \'{print $2}\')
                [ -n "$pid" ] && kill -9 $pid
                export  JAVA_HOME=/usr/local/jdk
                /bin/bash /usr/local/tomcat/bin/startup.sh''', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '$JOB_NAME', remoteDirectorySDF: false, removePrefix: 'target', sourceFiles: 'target/*.war')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}

Insert image description here

8. Build and view the results

注:第一次看不到分支,构建一次就好了

Insert image description here

Insert image description here

Insert image description here

下一篇文章> Jenkins releases Kubernetes (K8s) cluster (based on containerd)

Guess you like

Origin blog.csdn.net/Dream_ya/article/details/130767039