jenkins自动化部署搭建-创新互联

文章目录
    • jenkins自动化部署搭建
      • 1. 准备工作
      • 2. jenkins.war下载
      • 3. 启动jenkins
      • 4. 查看启动日志
      • 5. 访问jenkins
      • 6. jenkins配置
        • 6.1 Manage Jenkins中对环境进行配置
        • 6.2 新建自动部署项目
      • 7. 搭建完成,点击启动

目前创新互联建站已为上1000+的企业提供了网站建设、域名、网络空间、绵阳服务器托管、企业网站设计、西市网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。jenkins自动化部署搭建 1. 准备工作

需要先搭建java环境:jdk、maven、git、docker
参考文档:java环境安装

2. jenkins.war下载

官网地址
官网中有LTS和每周发布的两个版本,尽量选择LTS版本,LTS版本下方可看到以前的发行版,最新版本的需要java11以及以上

[root@localhost jenkins]# wget --no-check-certificate https://mirrors.jenkins.io/war-stable/2.346.1/jenkins.war
3. 启动jenkins
[root@localhost jenkins]# nohup java -jar  /usr/local/jenkins/jenkins.war >/usr/local/jenkins/jenkins.out &
4. 查看启动日志

启动成功并初始化管理员密码在日志中:419dfb18c7d4486cbf00907d52689aaa

[root@localhost local]# tail -100f jenkins/jenkins.out 
十二月 09, 2022 2:40:10 下午 Main verifyJavaVersion
警告: You are running Jenkins on Java 1.8, support for which will end on or after September 1, 2022. Please refer to the documentation for details on upgrading to Java 11: https://www.jenkins.io/redirect/upgrading-jenkins-java-version-8-to-11
Running from: /usr/local/jenkins/jenkins.war
webroot: $user.home/.jenkins
2022-12-09 06:40:12.899+0000 [id=1]     INFO    org.eclipse.jetty.util.log.Log#initialized: Logging initialized @2960ms to org.eclipse.jetty.util.log.JavaUtilLog
2022-12-09 06:40:13.212+0000 [id=1]     INFO    winstone.Logger#logInternal: Beginning extraction from war file
2022-12-09 06:40:20.879+0000 [id=1]     WARNING o.e.j.s.handler.ContextHandler#setContextPath: Empty contextPath
2022-12-09 06:40:21.342+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: jetty-9.4.45.v20220203; built: 2022-02-03T09:14:34.105Z; git: 4a0c91c0be53805e3fcffdcdcc9587d5301863db; jvm 1.8.0_291-b10
2022-12-09 06:40:23.761+0000 [id=1]     INFO    o.e.j.w.StandardDescriptorProcessor#visitServlet: NO JSP Support for /, did not find org.eclipse.jetty.jsp.JettyJspServlet
2022-12-09 06:40:23.920+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: DefaultSessionIdManager workerName=node0
2022-12-09 06:40:23.920+0000 [id=1]     INFO    o.e.j.s.s.DefaultSessionIdManager#doStart: No SessionScavenger set, using defaults
2022-12-09 06:40:23.922+0000 [id=1]     INFO    o.e.j.server.session.HouseKeeper#startScavenging: node0 Scavenging every 600000ms
2022-12-09 06:40:32.510+0000 [id=1]     INFO    hudson.WebAppMain#contextInitialized: Jenkins home directory: /root/.jenkins found at: $user.home/.jenkins
2022-12-09 06:40:33.776+0000 [id=1]     INFO    o.e.j.s.handler.ContextHandler#doStart: Started w.@21362712{Jenkins v2.346.1,/,file:///root/.jenkins/war/,AVAILABLE}{/root/.jenkins/war}
2022-12-09 06:40:34.865+0000 [id=1]     INFO    o.e.j.server.AbstractConnector#doStart: Started ServerConnector@79e4c792{HTTP/1.1, (http/1.1)}{0.0.0.0:8080}
2022-12-09 06:40:34.865+0000 [id=1]     INFO    org.eclipse.jetty.server.Server#doStart: Started @24928ms
2022-12-09 06:40:34.866+0000 [id=22]    INFO    winstone.Logger#logInternal: Winstone Servlet Engine running: controlPort=disabled
2022-12-09 06:40:37.154+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Started initialization
2022-12-09 06:40:37.173+0000 [id=34]    INFO    jenkins.InitReactorRunner$1#onAttained: Listed all plugins
2022-12-09 06:41:23.904+0000 [id=34]    INFO    jenkins.InitReactorRunner$1#onAttained: Prepared all plugins
2022-12-09 06:41:24.205+0000 [id=34]    INFO    jenkins.InitReactorRunner$1#onAttained: Started all plugins
2022-12-09 06:41:24.244+0000 [id=27]    INFO    jenkins.InitReactorRunner$1#onAttained: Augmented all extensions
2022-12-09 06:41:26.136+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: System config loaded
2022-12-09 06:41:26.137+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: System config adapted
2022-12-09 06:41:26.137+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: Loaded all jobs
2022-12-09 06:41:26.141+0000 [id=28]    INFO    jenkins.InitReactorRunner$1#onAttained: Configuration for all jobs updated
2022-12-09 06:41:26.283+0000 [id=48]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$1: Started Download metadata
2022-12-09 06:41:26.373+0000 [id=48]    INFO    hudson.util.Retrier#start: Attempt #1 to do the action check updates server
2022-12-09 06:41:28.276+0000 [id=29]    INFO    jenkins.install.SetupWizard#init: 

*************************************************************
*************************************************************
*************************************************************

Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:

419dfb18c7d4486cbf00907d52689aaa

This may also be found at: /root/.jenkins/secrets/initialAdminPassword

*************************************************************
*************************************************************
*************************************************************

2022-12-09 06:43:31.057+0000 [id=29]    INFO    jenkins.InitReactorRunner$1#onAttained: Completed initialization
2022-12-09 06:43:31.888+0000 [id=21]    INFO    hudson.lifecycle.Lifecycle#onReady: Jenkins is fully up and running
2022-12-09 06:43:39.659+0000 [id=48]    INFO    h.m.DownloadService$Downloadable#load: Obtained the updated data file for hudson.tasks.Maven.MavenInstaller
2022-12-09 06:43:39.660+0000 [id=48]    INFO    hudson.util.Retrier#start: Performed the action check updates server successfully at the attempt #1
2022-12-09 06:43:39.672+0000 [id=48]    INFO    hudson.model.AsyncPeriodicWork#lambda$doRun$1: Finished Download metadata. 133,383 ms
5. 访问jenkins

http://+ip+8080 (可以配置域名访问,我这里配置的域名访问,用ip+端口也可以访问)
启动成功需要输入初始化密码,输入后选择安装推荐插件
此处的密码即为第四步中的密码
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jzHpfMIC-1672024910306)(https://finger-guitar.oss-cn-hangzhou.aliyuncs.com/jenkins/2.png)]

此处需要配置国内镜像
官方下载插件慢 更新下载地址

cd {你的Jenkins工作目录}/updates #进入更新配置位置

sed -i ‘s/http://updates.jenkins-ci.org/download/https://mirrors.tuna.tsinghua.edu.cn/jenkins/g’ default.json && sed -i ‘s/http://www.google.com/https://www.baidu.com/g’ default.json

[root@localhost updates]# cd /root/.jenkins/updates/
[root@localhost updates]# ll
总用量 2992
-rw-r--r--. 1 root root 2723189 12月  9 14:43 default.json
-rw-r--r--. 1 root root   40867 12月  9 15:06 hudson.plugins.gradle.GradleInstaller
-rw-r--r--. 1 root root    5569 12月  9 15:06 hudson.tasks.Ant.AntInstaller
-rw-r--r--. 1 root root    6178 12月  9 14:43 hudson.tasks.Maven.MavenInstaller
-rw-r--r--. 1 root root  279724 12月  9 15:06 hudson.tools.JDKInstaller
[root@localhost updates]#
[root@localhost updates]# sed -i 's/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
[root@localhost updates]#

重启jenkins即可
avatar

至此,jenkins安装成功
avatar
接下来为配置项

6. jenkins配置 6.1 Manage Jenkins中对环境进行配置

avatar
avatar
avatar
avatar

6.2 新建自动部署项目

avatar
avatar
avatar
avatar
脚本:

#!/bin/bash
#maven打包
mvn clean package
echo 'package ok!'
echo 'build start!'
cd ./abc
service_name="abc"
service_prot=0000
#查看镜像id
IID=$(docker images | grep "$service_name" | awk '{print $3}')
echo "IID $IID"
if [ -n "$IID" ]
then
    echo "exist $SERVER_NAME image,IID=$IID"
    #删除镜像
    docker rmi -f $service_name
    echo "delete $SERVER_NAME image"
    #构建
    docker build -t $service_name .
    echo "build $SERVER_NAME image"
else
    echo "no exist $SERVER_NAME image,build docker"
    #构建
    docker build -t $service_name .
    echo "build $SERVER_NAME image"
fi
#查看容器id
CID=$(docker ps | grep "$SERVER_NAME" | awk '{print $1}')
echo "CID $CID"
if [ -n "$CID" ]
then
    echo "exist $SERVER_NAME container,CID=$CID"
    #停止
    docker stop $service_name
    #删除容器
    docker rm $service_name
else
    echo "no exist $SERVER_NAME container"
fi
#启动
docker run -d --name $service_name --net=host -p $service_prot:$service_prot $service_name
#查看启动日志
#docker logs -f  $service_name

avatar

7. 搭建完成,点击启动

avatar
avatar

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


网页标题:jenkins自动化部署搭建-创新互联
本文URL:http://azwzsj.com/article/ddcjcp.html