|NO.Z.00050|——————————|^^ 操作 ^^|——|CI/CD&Jenkins_Harbor操作.V07|--------------------------------------------|Jenkins向docker中部署專案.V01|
阿新 • • 發佈:2022-03-28
[CloudNative:CI/CD&Jenkins_Harbor操作.V07] [Applications.CloudNative] [|Jenkins向docker中部署專案|Post setps.配置shell指令碼|Jenkins遠端站點配置|Jenkins.Job執行構建|]
一、CI/CD.Jenkins.Jenkins向docker中部署專案
### --- Jenkins構建Job ~~~ # 通過jenkins向docker主機中部署專案 ~~~ ——>構建Maven風格的Job:程式碼地址:[email protected]:/home/git/easy-springmvc-maven.git ~~~ Dashboard——>新建Item——>任務名稱:maven-docker:構建一個maven專案——> ~~~ ——>Goals and options填寫:clean package -Dmaven.test.skip=true ~~~ ——>只有前面執行成功之後才會執行下面部分。
~~~ # 配置Post Steps
~~~ ——>指令碼中用到的倉庫和認證的賬號需要先在harbor新建好。
### --- 建立dockerfile:centos7.x 10.10.10.12 jenkins伺服器中配置 ~~~ 建立dockerfile [root@jenkins ~]# mkdir -pv /var/lib/jenkins/docker-file/maven-docker-test_war [root@jenkins ~]# vim /var/lib/jenkins/docker-file/maven-docker-test_war/Dockerfile # Version 1.0 # Base images. FROM tomcat:8.0.36-alpine # Author. MAINTAINER yanqi <[email protected]> # Add war. ADD maven-docker.war /usr/local/tomcat/webapps/ # Define working directory. WORKDIR /usr/local/tomcat/bin/ # Define environment variables. ENV PATH /usr/local/tomcat/bin:$PATH # Define default command. CMD ["catalina.sh", "run"] # Expose ports. EXPOSE 8080
二、Post setps.配置shell指令碼~~~ # 解釋說明 ~~~ # 目的:是把新產生的war包增加到tomcat中去 FROM tomcat:8.0.36-alpine // 從官方下載tomcat8.0.36 MAINTAINER yanqi <[email protected]> // 作者:作者郵箱 ADD maven-docker-test.war /usr/local/tomcat/webapps/ // 從jenkins本地檔案系統拷貝它的war包到/usr/local/tomcat/webapps的目錄下;專案名稱是maven-docker ENV PATH /usr/local/tomcat/bin:$PATH // 設定環境變數 CMD ["catalina.sh", "run"] // 最後去執行 EXPOSE 8080 // 埠
### --- 該實驗環境yum安裝方式修改工作目錄後的指令碼
~~~ Jenkins機器:編譯完成後,build生成一個新版本的映象,push到遠端docker倉庫
# Variables
JENKINS_WAR_HOME='/var/lib/jenkins/workspace/maven-docker/target'
DOCKERFILE_HOME='/var/lib/jenkins/docker-file/maven-docker-test_war'
HARBOR_IP='10.10.10.11'
REPOSITORIES='jenkins/maven-docker'
HARBOR_USER='yanqi'
HARBOR_USER_PASSWD='Harbor12345'
HARBOR_USER_EMAIL='[email protected]'
# Copy the newest war to docker-file directory.
\cp -f ${JENKINS_WAR_HOME}/easy-springmvc-maven.war ${DOCKERFILE_HOME}/maven-docker.war
# Delete image early version.
sudo docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}:80
IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'`
if [ -n "${IMAGE_ID}" ];then
sudo docker rmi ${IMAGE_ID}
fi
# Build image.
cd ${DOCKERFILE_HOME}
TAG=`date +%Y%m%d-%H%M%S`
sudo docker build -t ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} . &>/dev/null
# Push to the harbor registry.
sudo docker push ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} &>/dev/null
### --- 配置檔案說明
JENKINS_WAR_HOME='/var/lib/jenkins/workspace/maven-docker/target' // 定義環境變數:設定工作目錄
DOCKERFILE_HOME='/var/lib/jenkins/docker-file/maven-docker-test_war' // dockerfile檔案建立,上面已經建立完成。
HARBOR_IP='10.10.10.11' // 定義harbor伺服器的地址
REPOSITORIES='jenkins/maven-docker' // 庫的名字
HARBOR_USER='yanqi' // harbor倉庫的使用者名稱:yanqi
HARBOR_USER_PASSWD='Harbor12345' // Harbor倉庫的密碼:Harbor12345
sudo docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}:80 // jenkins主機登入harbor伺服器所用到的${HARBOR_USER}:Harbor主機的使用者 ${HARBOR_USER_PASSWD}主機的密碼 ${HARBOR_IP}:80主機的地址+埠
Login Succeeded //登入成功
IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'` // IMAGE_ID,通過awk拿去第三列。作為唯一的標識
sudo docker build -t ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} . &>/dev/null // .標識之前的那個目錄
sudo docker push ${HARBOR_IP}:80/${REPOSITORIES}:${TAG} &>/dev/null //push的時候也需要埠號
三、Post setps.配置shell指令碼
### --- 該實驗環境war包安裝方式修改工作目錄後的指令碼(war包部署時所使用的的指令碼檔案)
~~~ Jenkins機器:編譯完成後,build生成一個新版本的映象,push到遠端docker倉庫
# Variables
JENKINS_WAR_HOME='/root/.jenkins/workspace/maven-docker-test/target'
DOCKERFILE_HOME='/root/jenkins/docker-file/maven-docker-test_war'
HARBOR_IP='10.10.10.11'
REPOSITORIES='jenkins/maven-docker-test'
HARBOR_USER='yanqi'
HARBOR_USER_PASSWD='Harbor12345'
HARBOR_USER_EMAIL='[email protected]'
# Copy the newest war to docker-file directory.
\cp -f ${JENKINS_WAR_HOME}/easy-springmvc-maven.war ${DOCKERFILE_HOME}/maven-docker-test.war
# Delete image early version.
sudo docker login -u ${HARBOR_USER} -p ${HARBOR_USER_PASSWD} ${HARBOR_IP}
IMAGE_ID=`sudo docker images | grep ${REPOSITORIES} | awk '{print $3}'`
if [ -n "${IMAGE_ID}" ];then
sudo docker rmi ${IMAGE_ID}
fi
# Build image.
cd ${DOCKERFILE_HOME}
TAG=`date +%Y%m%d-%H%M%S`
sudo docker build -t ${HARBOR_IP}/${REPOSITORIES}:${TAG} . &>/dev/null
# Push to the harbor registry.
sudo docker push ${HARBOR_IP}/${REPOSITORIES}:${TAG} &>/dev/null
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
來自為知筆記(Wiz)