Docker 部署jenkins

简介

Jenkins是一个基于java开发的开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。

搭建jenkins

安装docker服务,配置仓库地址,检查版本号:

  • /etc/sysctl.conf 文件里面开启lptables转发功能,在下面加入:
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 0
net.ipv4.conf.all.rp_filter = 0
  • 拉取gogs/gogs镜像 :
docker pull gogs/gogs
  • 拉取数据库镜像mysql
docker pull mysql:5.7
  • 启动gsgo和mysql容器:
docker run -d -p 90:3000 --name gogs gogs/gogs
docker run -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 --name gogs-mysqld mysql:5.7
  • 进入mysql容器,创建gogs数据库:
docker exec -it gogs-mysqld  /bin/bash
mysql -uroot -p123456
create database gogs;
  • 访问网页:
IP地址:90

 

  • 点了安装后点击立即注册:

  • 在gogs服务中创建新的仓库:

  • 安装软件包:
yum install -y git
  • 复制git到本地,构建私有仓库:
git clone http://192.168.20.3:90/wenzhi/wenzhi.git
注意这个wenzhi后面需要cd进入

安装部署jenkins服务:

  • 上传四个软件包:apache-maven-3.5.0-bin.tar.gz apache-tomcat-7.0.56.tar.gz jdk-8u144-linux-x64.tar.gz jenkins.war
-rw-r--r-- 1 root root   8534562 Apr  7  2017 apache-maven-3.5.0-bin.tar.gz
-rw-r--r-- 1 root root   8894241 Mar 14 08:46 apache-tomcat-7.0.56.tar.gz
-rw-r--r-- 1 root root 185515842 Mar 14 08:53 jdk-8u144-linux-x64.tar.gz
-rw-r--r-- 1 root root  71017525 May 30 09:24 jenkins.war
  • 创建jdk64目录并将jdk-8u144-linux-x64.tar.gz 移动到jdk64 里面:
mkdir /usr/jdk64/
tar -zxvf jdk-8u144-linux-x64.tar.gz -C /usr/jdk64/
  • 将文件apache-maven-3.5.0-bin.tar.gz 解压到/usr/local/ 里面:
tar -zxvf apache-maven-3.5.0-bin.tar.gz -C /usr/local/
  • 配置环境变量,在/etc/profile 里面加入:
export JAVA_HOME=/usr/jdk64/jdk1.8.0_144
export CLASSPATH=$:CLASSPATH:$JAVA_HOME/lib/
export PATH=$PATH:$JAVA_HOME/bin
  • 运行文件
source /etc/profile
  • 查看java版本:
java -version
  • 解压tomcat,把jenkins.war包放到Tomcat服务器的webapps目录,并且启动tomcat服务器:
tar -zxvf apache-tomcat-7.0.56.tar.gz -C /opt/
cp jenkins.war /opt/apache-tomcat-7.0.56/webapps/
  • 进入/opt/apache-tomcat-7.0.56/bin/ 目录加上权限并运行脚本文件:
chmod +x *.sh
./startup.sh
  • 访问网页:
http://IP地址:8080/jenkins

  • /root/.jenkins/secrets/initialAdminPassword 查看密码:
cat /root/.jenkins/secrets/initialAdminPassword

等待安装即可:

  • 创建用户:

  • /opt/apache-tomcat-7.0.56/conf/server.xml 加入
URIEncoding="UTF-8"

  • /opt/apache-tomcat-7.0.56/conf/tomcat-users.xml 下面加入
  <role rolename="manager-gui"/>
  <role rolename="manager-script"/>
  <role rolename="manager-jmx">
  <role rolename="manager-status">
  <user username="wenzhi" password="wenzhi" roles="manager-gui,manager-script,manager-jmx,manager-status"/>

  • 下载这三个插件分别为:

Maven integration,Deploy to container,Git

  • 找到全局工具配置:

  • 在jdk里面加入:
注意!

路径是自己配置的

jdk1.8.0_144
/usr/jdk64/jdk1.8.0_144

  • 在Maven加入:
注意!
路径是自己配置的
apache-maven-3.5.0
/usr/local/apache-maven-3.5.0

  • 进入jenkins主页,创建一个新项目,名称为qcloud:

  • 在qcloud项目中,选择“源码管理”标签,选中Git按钮填写Git源:

  • 在qcloud项目中,选择“构建触发器”标签,选中Build whenever a SNAPSHOT dependency is built 和轮询 SCM 复选框:
H/1 * * * *

  • 在qcloud项目中,选择构建环境标签,在Build列表的Root POM文本加入:
course-maven/pom.xml
  • Goals and options加入:
clean install
  • /usr/local/apache
  • 解压repository.zip文件:
unzip repository.zip
  • 将文件移动到/opt/maven-repository/目录底下
mv repository/* /opt/maven-repository/
  • 将项目上传服务器(我这上传至/root),然后进入项目所在文件:

    • 解压xiandian-master
    • -maven-3.5.0/conf/settings.xml 里面加入:
<localRepository>/opt/maven-repository</localRepository>

注意上方这个文件名为repository他是在opt底下哦!
mkdir /opt/maven-repository
    • 上传repository.zip 到opt目录下面
  • .zip 文件:
unzip xiandian-master.zip
cd xiandian-master/
  • 依次输入以下命令:
git init    //创建一个空的git仓库或者重新初始化一个现有的仓库

git add course-maven    //将修改文件由工作区提交到暂存区,可以多次提交

git config --global user.email "2868614015@qq.com"     //作者邮箱

git config --global user.name "root"    //作者名字

git commit -am "add course -maven"        //将文件从暂存区提交到版本库
  • 然后准备上传到仓库:
git remote add origin 仓库地址

例如:
git remote add origin http://192.168.20.3:90/root/wenzhi.git
git push -u origin master
  • 网页查看仓库上传的文件:

  • 进入wenzhi/course-maven/src/main/resources/jdbc.properties编写数据库

  • 替换course-maven/pom.xml为:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    	<modelVersion>4.0.0</modelVersion>

    	<groupId>com.xd.cloud</groupId>
    	<artifactId>course</artifactId>
    	<packaging>war</packaging>
    	<version>1.0</version>

    	<properties>
    		<org.springframework-version>4.0.0.RELEASE</org.springframework-version>
    		<org.hibernate-version>4.3.1.Final</org.hibernate-version>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    		<shiro.version>1.2.3</shiro.version>
    	</properties>

    	<dependencies>
    		<dependency>
    			<groupId>org.apache.httpcomponents</groupId>
    			<artifactId>httpclient</artifactId>
    			<version>4.5.2</version>
    		</dependency>

    		<dependency>
    			<groupId>org.apache.httpcomponents</groupId>
    			<artifactId>httpcore</artifactId>
    			<version>4.4.4</version>
    		</dependency>

    		<dependency>
    			<groupId>org.apache.httpcomponents</groupId>
    			<artifactId>httpmime</artifactId>
    			<version>4.5.1</version>
    		</dependency>

    		<dependency>
    			<groupId>ant</groupId>
    			<artifactId>ant</artifactId>
    			<version>1.6.5</version>
    		</dependency>

    		<!-- servlet -->
    		<dependency>
    			<groupId>javax.servlet</groupId>
    			<artifactId>servlet-api</artifactId>
    			<version>2.5</version>
    		</dependency>
    		<!-- freemarker -->
    		<dependency>
    			<groupId>org.freemarker</groupId>
    			<artifactId>freemarker</artifactId>
    			<version>2.3.20</version>
    		</dependency>
    		<dependency>
    			<groupId>com.jagregory.shiro</groupId>
    			<artifactId>shiro-freemarker-tags</artifactId>
    			<version>0.1</version>
    		</dependency>

    		<!-- Jackson -->
    		<dependency>
    			<groupId>org.codehaus.jackson</groupId>
    			<artifactId>jackson-mapper-asl</artifactId>
    			<version>1.9.13</version>
    		</dependency>

    		<!--utils -->
    		<dependency>
    			<groupId>com.google.guava</groupId>
    			<artifactId>guava</artifactId>
    			<version>15.0-rc1</version>
    		</dependency>

    		<!-- Apache Commons -->
    		<dependency>
    			<groupId>commons-lang</groupId>
    			<artifactId>commons-lang</artifactId>
    			<version>2.6</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-collections</groupId>
    			<artifactId>commons-collections</artifactId>
    			<version>3.2.1</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-beanutils</groupId>
    			<artifactId>commons-beanutils</artifactId>
    			<version>1.8.3</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-dbutils</groupId>
    			<artifactId>commons-dbutils</artifactId>
    			<version>1.5</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-fileupload</groupId>
    			<artifactId>commons-fileupload</artifactId>
    			<version>1.3</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-codec</groupId>
    			<artifactId>commons-codec</artifactId>
    			<version>1.8</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-configuration</groupId>
    			<artifactId>commons-configuration</artifactId>
    			<version>1.10</version>
    		</dependency>
    		<dependency>
    			<groupId>commons-io</groupId>
    			<artifactId>commons-io</artifactId>
    			<version>2.4</version>
    		</dependency>

    		<dependency>
    			<groupId>commons-pool</groupId>
    			<artifactId>commons-pool</artifactId>
    			<version>1.5.6</version>
    		</dependency>

    		<dependency>
    			<groupId>javax.mail</groupId>
    			<artifactId>mail</artifactId>
    			<version>1.4.4</version>
    		</dependency>

    		<!-- slf4j -->
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-api</artifactId>
    			<version>1.7.21</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>1.7.21</version>
    		</dependency>

    		<!-- mysql driver -->
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.15</version>
    		</dependency>

    		<!-- c3p0 datasource -->
    		<dependency>
    			<groupId>c3p0</groupId>
    			<artifactId>c3p0</artifactId>
    			<version>0.9.1.2</version>
    		</dependency>

    		<!-- JUnit -->
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.11</version>
    			<scope>provided</scope>
    		</dependency>

    		<!-- spring 4 -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-webmvc</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>${org.springframework-version}</version>
    			<exclusions>
    				<!-- Exclude Commons Logging in favor of SLF4j -->
    				<exclusion>
    					<groupId>commons-logging</groupId>
    					<artifactId>commons-logging</artifactId>
    				</exclusion>
    			</exclusions>
    		</dependency>

    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<!--<dependency> -->
    		<!--<groupId>org.springframework</groupId> -->
    		<!--<artifactId>spring-aspects</artifactId> -->
    		<!--<version>${org.springframework-version}</version> -->
    		<!--</dependency> -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-expression</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-web</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<!--<dependency> -->
    		<!--<groupId>org.springframework</groupId> -->
    		<!--<artifactId>spring-jdbc</artifactId> -->
    		<!--<version>${org.springframework-version}</version> -->
    		<!--</dependency> -->
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-beans</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>

    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>${org.springframework-version}</version>
    		</dependency>
    		<!--<dependency> -->
    		<!--<groupId>org.springframework</groupId> -->
    		<!--<artifactId>spring-jms</artifactId> -->
    		<!--<version>${org.springframework-version}</version> -->
    		<!--</dependency> -->
    		<!--<dependency> -->
    		<!--<groupId>org.springframework</groupId> -->
    		<!--<artifactId>spring-test</artifactId> -->
    		<!--<version>${org.springframework-version}</version> -->
    		<!--<scope>test</scope> -->
    		<!--</dependency> -->

    		<!-- AspectJ -->
    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjrt</artifactId>
    			<version>1.7.4</version>
    		</dependency>

    		<dependency>
    			<groupId>org.aspectj</groupId>
    			<artifactId>aspectjweaver</artifactId>
    			<version>1.7.4</version>
    		</dependency>

    		<!-- hiberante 4 -->
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-core</artifactId>
    			<version>${org.hibernate-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-validator</artifactId>
    			<version>${org.hibernate-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-entitymanager</artifactId>
    			<version>${org.hibernate-version}</version>
    		</dependency>
    		<dependency>
    			<groupId>javax.validation</groupId>
    			<artifactId>validation-api</artifactId>
    			<version>1.0.0.GA</version>
    			<scope>provided</scope>
    		</dependency>

    		<!--enable hibernate to use its proxy model -->
    		<dependency>
    			<groupId>org.javassist</groupId>
    			<artifactId>javassist</artifactId>
    			<version>3.18.1-GA</version>
    		</dependency>
    		<!--sitemesh -->
    		<dependency>
    			<groupId>opensymphony</groupId>
    			<artifactId>sitemesh</artifactId>
    			<version>2.4.2</version>
    		</dependency>

    		<!--<dependency> -->
    		<!--<groupId>org.jboss.logging</groupId> -->
    		<!--<artifactId>jboss-logging</artifactId> -->
    		<!--<version>3.1.0.CR2</version> -->
    		<!--</dependency> -->

    		<!-- apache shiro -->
    		<dependency>
    			<groupId>org.apache.shiro</groupId>
    			<artifactId>shiro-core</artifactId>
    			<version>${shiro.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.shiro</groupId>
    			<artifactId>shiro-web</artifactId>
    			<version>${shiro.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.shiro</groupId>
    			<artifactId>shiro-ehcache</artifactId>
    			<version>${shiro.version}</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.shiro</groupId>
    			<artifactId>shiro-spring</artifactId>
    			<version>${shiro.version}</version>
    		</dependency>

    		<dependency>
    			<groupId>org.apache.shiro</groupId>
    			<artifactId>shiro-cas</artifactId>
    			<version>${shiro.version}</version>
    		</dependency>

    		<!--百度编辑器 -->
    		<dependency>
    			<groupId>com.baidu.ueditor</groupId>
    			<artifactId>ueditor</artifactId>
    			<version>1.1.1</version>
    		</dependency>

    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-annotations</artifactId>
    			<version>2.8.5</version>
    		</dependency>

    		<dependency>
    			<groupId>org.json</groupId>
    			<artifactId>json</artifactId>
    			<version>1.0</version>
    		</dependency>

    		<dependency>
    			<groupId>net.sf.json-lib</groupId>
    			<artifactId>json-lib</artifactId>
    			<version>2.4</version>
    			<classifier>jdk15</classifier>
    		</dependency>

    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi</artifactId>
    			<version>3.10-FINAL</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-ooxml</artifactId>
    			<version>3.10-FINAL</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.poi</groupId>
    			<artifactId>poi-scratchpad</artifactId>
    			<version>3.10-FINAL</version>
    		</dependency>
    		<dependency>
    			<groupId>xml-apis</groupId>
    			<artifactId>xml-apis</artifactId>
    			<version>2.0.2</version>
    		</dependency>
    		<dependency>
    			<groupId>org.apache.xmlbeans</groupId>
    			<artifactId>xmlbeans</artifactId>
    			<version>2.6.0</version>
    		</dependency>
    		<dependency>
    			<groupId>stax</groupId>
    			<artifactId>stax-api</artifactId>
    			<version>1.0.1</version>
    		</dependency>
    		<!-- memcached -->
    		<dependency>
    			<groupId>com.danga</groupId>
    			<artifactId>memcached</artifactId>
    			<version>2.6.6</version>
    		</dependency>
    		<!-- 腾讯云API -->
    		<!-- https://mvnrepository.com/artifact/com.qcloud/cos_api -->
    		<dependency>
    			<groupId>com.qcloud</groupId>
    			<artifactId>cos_api</artifactId>
    			<version>4.4</version>
    		</dependency>

    		<dependency>
    			<groupId>com.qcloud</groupId>
    			<artifactId>qcloud-vod</artifactId>
    			<version>1.0.0</version>
    		</dependency>

    		<!-- https://mvnrepository.com/artifact/com.jacob/jacob -->
    		<dependency>
    			<groupId>com.jacob</groupId>
    			<artifactId>jacob</artifactId>
    			<version>1.10</version>
    		</dependency>

    		<!-- https://mvnrepository.com/artifact/org.pacesys/openstack4j -->
    		<dependency>
    			<groupId>org.pacesys</groupId>
    			<artifactId>openstack4j</artifactId>
    			<version>3.0.3</version>
    			<classifier>withdeps</classifier>
    		</dependency>


    	</dependencies>

    	<build>
    		<finalName>ROOT</finalName>
    		<plugins>
    			<plugin>
    				<groupId>org.eclipse.jetty</groupId>
    				<artifactId>jetty-maven-plugin</artifactId>
    				<version>9.3.0.M2</version>
    			</plugin>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<configuration>
    					<source>1.7</source>
    					<target>1.7</target>
    				</configuration>
    			</plugin>
    			<!-- 配置jre/lib/rt.jar ,jre7+被删除 -->
    			<!-- <plugin> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> 
    				<configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> 
    				<optimize>true</optimize> <debug>true</debug> <showDeprecation>true</showDeprecation> 
    				<showWarnings>false</showWarnings> <compilerArguments> <verbose /> <bootclasspath>${java.home}/lib/rt.jar;${java.home}/lib/jce.jar</bootclasspath> 
    				</compilerArguments> </configuration> </plugin> -->
    		</plugins>
    	</build>
    </project>
  • 返回Jenkins进入qcloud项目,点击构建:

使用war包

  • ROORT.war的文件在/root/.jenkins/workspace/qcloud/course-maven/target路径下:
cd /root/.jenkins/workspace/qcloud/course-maven/target

  • /opt/apache-tomcat-7.0.56/webapps/ 的ROOT文件删除:
rm -rf ROOT

/root/.jenkins/workspace/qcloud/course-maven/target 底下的ROOT和ROOT.war文件移动到/opt/apache-tomcat-7.0.56/webapps/底下:

mv ROOT /opt/apache-tomcat-7.0.56/webapps/
mv ROOT.war /opt/apache-tomcat-7.0.56/webapps/
  • /opt/apache-tomcat-7.0.56/bin/ 底下执行脚本:
./shutdown.sh

./startup.sh
  • 将这个course.sql 文件上传上来,然后给他上传进去容器:
docker cp course.sql gogs-mysqld:/root/
  • 然后进入数据库容器:
docker exec -it gogs-mysqld /bin/bash
  • 进入数据库:
mysql -uroot -p123456
  • 创建course数据库,并导入数据:
create database course;

use course;

source /root/course.sql;
  • 退出数据库修改/opt/apache-tomcat-7.0.56/webapps/ROOT/WEB-INF/classes/底下的jdbc.properties文件:

  • 修改/opt/apache-tomcat-7.0.56/conf/server.xml 端口:

  • 进入/opt/apache-tomcat-7.0.56/bin/ 执行脚本:
./shutdown.sh

./startup.sh
  • 然后访问网页:

  • 这边看见网页都不显示图片接下来我们上传:

  • 解压上传的这个文件:
unzip upload.zip -d /opt/apache-tomcat-7.0.56/webapps/ROOT/
  • 然后刷新网页:

这边就看见显示的图片了

  • 这边在查看下jenkins是否能访问:

  • 接下来我们将云课堂第一张图片显示出来:

/opt/apache-tomcat-7.0.56/webapps/ROOT/upload/image/20170620/ 底下上传一张图片记住自己的名字:

  • 进入容器数据库:
docker exec -it gogs-mysqld /bin/bash
  • 进入数据库:
mysql -uroot -p123456
use course;
  • 查看表内容:
show tables;
  • 查看t_course 表信息:
select * from t_course;
  • 上传图片:
update t_course set thumb='/upload/image/20170620/15.jpg' where id=17;
update t_course set level='' where id=17;
  • 刷新网页然后访问:

这边你也可以继续上传自己的课程修改/opt/apache-tomcat-7.0.56/webapps/ROOT/WEB-INF/classes/xd.properties 底下文件:

  • 重新启动脚本,然后去页面上传:

版权声明:
作者:小李
链接:https://xianshengli.cc/docker-%e9%83%a8%e7%bd%b2jenkins/
来源:先生不吃香菜
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Docker 部署jenkins
简介 Jenkins是一个基于java开发的开源 CI&CD 软件,用于自动化各种任务,包括构建、测试和部署软件。 搭建jenkins 安装docker服务,配置仓库地址,检查版……
文章目录
关闭
目 录