基于Docker的CI/CD流水线
环境准备
ip | 操作系统 |
---|---|
192.168.2.110 | centos 7.2 |
JDK安装
mkdir -p /opt/java
cd /opt/java
wget http://download.oracle.com/otn-pub/java/jdk/8u172-b11/a58eab1ec242421181065cdc37240b08/jdk-8u172-linux-x64.tar.gz
tar -zxvf jdk-8u172-linux-x64.tar.gz
配置环境变量
vim /etc/profile
加入
# Java Environment Path
export JAVA_HOME=/opt/jdk1.8.0_144
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
Maven安装
mkdir -p /opt/maven
cd /opt/maven
wget http://117.143.109.148/cache/mirrors.shu.edu.cn/apache/maven/maven-3/3.5.3/binaries/apache-maven-3.5.3-bin.tar.gz
tar -zxvf apache-maven-3.5.3-bin.tar.gz
配置环境变量
vim /etc/profile
加入
MAVEN_HOME=/opt/apache/apache-maven-3.5.3
export MAVEN_HOME
export PATH=${PATH}:${MAVEN_HOME}/bin
执行
source /etc/profile
项目版本控制工具gitlab、git
1.git安装
yum –y install git
2.gitlab安装
采用docker部署gitlab
mkdir -p /opt/docker/gitlab/config
mkdir -p /opt/docker/gitlab/logs
mkdir -p /opt/docker/gitlab/data
docker pull gitlab/gitlab-ce:latest
sudo docker run --detach \
--hostname dev.devstack.com \
--privileged=true \
-e 'GITLAB_PORT=3380' \
-e 'GITLAB_SSH_PORT=3322' \
--publish 3443:443 \
--publish 3380:80 \
--publish 3322:22 \
--name gitlab \
--volume /opt/docker/gitlab/config:/etc/gitlab \
--volume /opt/docker/gitlab/logs:/var/log/gitlab \
--volume /opt/docker/gitlab/data:/var/opt/gitlab \
gitlab/gitlab-ce:latest
下载镜像时间会长点,看网络情况,也可以科学上网,会好点
启动成功后,第一次登录使用的用户名和密码为 root 和 5iveL!fe,开始更改密码和配置ssh key
Jenkins安装
mkdir -p /opt/jenkins
cd /opt/jenkins
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.121.1/jenkins.war
nohup java -jar jenkins.war >/opt/jenkins/jenkins.log 2>&1 &
命令行参数 | 参数说明 |
---|---|
--httpPort=$HTTP_PORT | 使用标准http协议在端口$ HTTP_PORT上运行Jenkins侦听器。默认值为端口8080.要禁用(因为您使用的是https),请使用端口-1 |
--httpListenAddress=$HTTP_HOST | 将Jenkins绑定到由$ HTTP_HOST表示的IP地址。默认值为0.0.0.0 - 即侦听所有可用的接口。例如,要仅侦听来自localhost的请求,可以使用–httpListenAddress = 127.0.0.1 |
--httpsPort=$HTTP_PORT | 在端口$ HTTP_PORT上使用HTTPS协议 |
--httpsListenAddress=$HTTPS_HOST | 绑定Jenkins在$ HTTPS_HOST表示的IP地址上侦听HTTPS请求。 |
--prefix=$PREFIX | 运行Jenkins以在URL的末尾包含$ PREFIX。例如,要使Jenkins可以在http // myServer 8080 / jenkins上访问,请设置–prefix = / jenkins |
--ajp13Port=$AJP_PORT | 使用标准AJP13协议在端口$ AJP_PORT上运行Jenkins侦听器。默认值为端口8009.要禁用(因为您使用的是https),请使用端口-1。 |
--ajp13ListenAddress=$AJP_HOST | 将Jenkins绑定到由$ AJP_HOST表示的IP地址。默认值为0.0.0.0 - 即侦听所有可用的接口。 |
--argumentsRealm.passwd.$ADMIN_USER | 设置用户$ ADMIN_USER的密码。如果Jenkins安全性已打开,则必须以$ ADMIN_USER身份登录才能配置Jenkins或Jenkins项目。注意还必须指定此用户具有管理员角色。 (见下面的参数)。 |
--argumentsRealm.roles.$ADMIN_USER=admin | 将$ ADMIN_USER设置为管理用户,如果Jenkins的安全性已打开,可以配置Jenkins。有关详细信息,请参阅Jenkins安全。 |
-Xdebug -Xrunjdwp:transport=dt_socket,address=$DEBUG_PORT,server=y,suspend=n | 设置调试开关,您可以访问$ DEBUG_PORT上的调试。 |
logfile=$LOG_PATH/winstone_date +”%Y%m-%d_%H-%M”.log | 定义日志格式 |
-XX:PermSize=512M -XX:MaxPermSize=2048M -Xmn128M -Xms1024M -Xmx2048M | 参考Oracle Jave设置 |
访问Jenkins只需要在客户端浏览器输入:http://ip:8080/ ,即可
默认初始访问Jenkin需要管理员密码,密码位置:/root/.jenkins/secrets/initialAdminPassword cat /root/.jenkins/secrets/initialAdminPassword
进入选择插件安装界面,选择第一个(Install suggested plugins)
启动脚本
#!/bin/sh
DESC="Jenkins CI Server"
NAME=jenkins
PIDFILE=/var/run/$NAME.pid
RUN_AS=jenkins
COMMAND="java -- -jar /opt/jenkins/jenkins.war"
d_start() {
start-stop-daemon --start --quiet --background --make-pidfile --pidfile $PIDFILE --chuid $RUN_AS --exec $COMMAND
}
d_stop() {
start-stop-daemon --stop --quiet --pidfile $PIDFILE
if [ -e $PIDFILE ]
then rm $PIDFILE
fi
}
case $1 in
start)
echo -n "Starting $DESC: $NAME"
d_start
echo "."
;;
stop)
echo -n "Stopping $DESC: $NAME"
d_stop
echo "."
;;
restart)
echo -n "Restarting $DESC: $NAME"
d_stop
sleep 1
d_start
echo "."
;;
*)
echo "usage: $NAME {start|stop|restart}"
exit 1
;;
esac
exit 0
等待安装好插件后,登录Jenkins,配置jenkins需要的maven、jdk路径,并安装的需要插件
•发布插件 Deploy to container Plugin 必须
•Maven插件 Maven Integration plugin必须
docker仓库安装
docker私有仓库管理系统harbor的部署使用:https://my.oschina.net/orrin/blog/1820462
nexus安装
mkdir -p /opt/nexus
cd /opt/nexus
wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.14.3-02-bundle.tar.gz
tar xfvz nexus-2.14.3-02-bundle.tar.gz
cd nexus-2.14.3-02-bundle
启动nexus
nexus-2.14.3-02/bin/nexus start
访问页面:http://192.168.2.110:8081/nexus
注意:nexus的默认端口是8081,后面可以修改。 用默认账户和密码登录 账户:admin 密码:admin123
maven设置
docker-harbor密码设置
1、控制台执行:
mvn --encrypt-master-password <password harbor密码>
2、在{user.home}/.m2/目录创建settings-security.xml文件,将主密码(第一步生成的密码)写入:
<?xml version="1.0" encoding="UTF-8"?>
<settingsSecurity>
<master>{K/cB3R+gXBgH3LMUGFhs4FV9PKIgqKPsBNNrMM1w/dc=}</master>
</settingsSecurity>
3、控制台执行:
mvn --encrypt-password <password harbor密码>
将生成的密码写入到settings.xml的<services>中
<server>
<id>docker-harbor</id>
<username>admin</username>
<password>{RPYxhVXHfpkHXH30T4Y82RSycGbN9SQo0NB3F7FvGIQ=}</password>
<configuration>
<email>zhangorrin@126.cn</email>
</configuration>
</server>
nexus密码设置
将密码写入到settings.xml的<services>中
<server>
<id>releases</id>
<username>admin</username>
<password>admin123</password>
</server>
<server>
<id>snapshots</id>
<username>admin</username>
<password>admin123</password>
</server>
项目集成
1.修改pom.xml文件
<!-- 添加属性 -->
<properties>
<!-- docker仓库地址 -->
<docker.repostory>192.168.2.150</docker.repostory>
<!-- 项目名称 namespace -->
<docker.registry.name>abcd</docker.registry.name>
<!-- maven私服地址 -->
<repository.url>http://192.168.2.110:8081/nexus/content</repository.url>
</properties>
<!-- 添加profiles -->
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<spring.profiles.active>dev</spring.profiles.active>
</properties>
</profile>
<profile>
<id>test</id>
<properties>
<spring.profiles.active>test</spring.profiles.active>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<spring.profiles.active>prod</spring.profiles.active>
</properties>
</profile>
</profiles>
<!-- 添加resources -->
<resources><!-- 使用@@站位符,输出Dockerfile至docker文件夹 -->
<resource>
<directory>${project.basedir}/src/main/docker</directory>
<filtering>true</filtering>
<includes>
<include>**/Dockerfile</include>
</includes>
<targetPath>../docker</targetPath>
</resource>
<resource>
<directory>${project.basedir}/src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<!-- 添加插件 -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.13</version>
<executions>
<execution>
<phase>deploy</phase>
<goals>
<goal>build</goal>
</goals>
</execution>
<execution>
<id>tag-image</id>
<phase>deploy</phase>
<goals>
<goal>tag</goal>
</goals>
<configuration>
<image>
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${spring.profiles.active}-${project.version}
</image>
<newName>
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${spring.profiles.active}-${project.version}
</newName>
</configuration>
</execution>
<execution>
<id>push-image</id>
<phase>deploy</phase>
<goals>
<goal>push</goal>
</goals>
<configuration>
<imageName>
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${spring.profiles.active}-${project.version}
</imageName>
</configuration>
</execution>
</executions>
<configuration>
<!-- 私有仓库配置,需要settings.xml文件配合serverId对应的服务地址 -->
<serverId>docker-harbor</serverId>
<registryUrl>${docker.repostory}</registryUrl>
<!--强制docker在每次新的构建上覆盖镜像tags-->
<!-- <forceTags>true</forceTags> -->
<!--install阶段也上传,否则只有deploy阶段上传-->
<!--<pushImage>true</pushImage>-->
<dockerDirectory>${project.basedir}/target/docker</dockerDirectory>
<imageName>
${docker.repostory}/${docker.registry.name}/${project.artifactId}:${spring.profiles.active}-${project.version}
</imageName>
<imageTags>
<!--docker的tag为项目版本号、latest-->
<imageTag>${spring.profiles.active}.${project.version}</imageTag>
<imageTag>${spring.profiles.active}.latest</imageTag>
</imageTags>
<resources>
<rescource><!-- 将打包文件放入dockerDirectory指定的位置 -->
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</rescource>
</resources>
</configuration>
</plugin>
<repositories>
<repository>
<id>public</id>
<name>local private nexus</name>
<url>${repository.url}/groups/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>releases</id>
<url>${repository.url}/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>snapshots</id>
<url>${repository.url}/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>
Dockerfile文件:
# Version 0.0.1
FROM java:8
MAINTAINER orrin "zhangorrin@126.cn"
# 环境变量
ENV WORK_PATH /home/project/nmcp
ENV APP_NAME @project.build.finalName@.@project.packaging@
ENV APP_VERSION @project.version@
EXPOSE 10087
#USER
#USER user:group
#VOLUME
VOLUME ["/home/project", "/tmp/data"]
#ADD
#COPY
COPY $APP_NAME $WORK_PATH/
#LABEL
#STOPSIGNAL
#ARG
#ONBUILD
# WORKDIR
WORKDIR $WORK_PATH
RUN echo "Asia/Shanghai" > /etc/timezone
# ENTRYPOINT
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom"]
# CMD
CMD ["-jar", "@project.build.finalName@.@project.packaging@"]
2.配置Jenkins
输入项目名称,配置项目git地址及maven命令
选择配置后的项目,点击立即构建,构建完成后,即可在harbor上看到生成的镜像。