jenkins triggers the automatic construction of docker image, uploads it to harbor and publishes it

Keywords: Linux Docker git Tomcat GitLab

I. making Dockerfile file

1. Deploy on 172.19.2.51

mkdir -pv /opt/git
git clone http://172.19.2.140:18080/lvqingshan/gcgj.git
cd /opt/git/gcgj
scp app@172.19.2.1:/home/app/portal-tomcat/webapps/portal.war ./
scp app@192.168.37.34:/home/app/portal-tomcat/conf/server.xml ./

vim Dockerfile
FROM tomcat:7.0.77-jre8
ADD server.xml /usr/local/tomcat/conf
RUN rm -rf /usr/local/tomcat/webapps/*
COPY portal.war /usr/local/tomcat/webapps/ROOT.war
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]

2. Test whether the dockerfile can work normally

docker build -t gcgj/portal .
docker run -p 38080:8080 -idt gcgj/portal:latest
git add -A
git commit
git push -u origin master
gitlab Account number lvqingshan
//Password abcd1234

II. Configure and log into harbor warehouse (the warehouse is 172.19.2.139)

1. Configure the warehouse private key on 192.168.13.45

mkdir -pv /etc/docker/certs.d/172.19.2.139/
vim /etc/docker/certs.d/172.19.2.139/ca.crt
-----BEGIN CERTIFICATE-----
MIIDvjCCAqagAwIBAgIUQzFZBuFh7EZLOzWUYZ10QokL+BUwDQYJKoZIhvcNAQEL
BQAwZTELMAkGA1UEBhMCQ04xEDAOBgNVBAgTB0JlaUppbmcxEDAOBgNVBAcTB0Jl
aUppbmcxDDAKBgNVBAoTA2s4czEPMA0GA1UECxMGU3lzdGVtMRMwEQYDVQQDEwpr
dWJlcm5ldGVzMB4XDTE3MDcwNDA4NTMwMFoXDTIyMDcwMzA4NTMwMFowZTELMAkG
A1UEBhMCQ04xEDAOBgNVBAgTB0JlaUppbmcxEDAOBgNVBAcTB0JlaUppbmcxDDAK
BgNVBAoTA2s4czEPMA0GA1UECxMGU3lzdGVtMRMwEQYDVQQDEwprdWJlcm5ldGVz
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyWgHFV6Cnbgxcs7X7ujj
APnnMmotzNnnTRhygJLCMpCZUaWYrdBkFE4T/HGpbYi1R5AykSPA7FCffFHpJIf8
Gs5DAZHmpY/uRsLSrqeP7/D8sYlyCpggVUeQJviV/a8L7PkCyGq9DSiU/MUBg4CV
Dw07OT46vFJH0lzTaZJNSz7E5QsekLyzRb61tZiBN0CJvSOxXy7wvdqK0610OEFM
T6AN8WfafTH4qmKWulFBJN1LjHTSYfTZzCL6kfTSG1M3kqG0W4B2o2+TkNLVmC9n
gEKdeh/yQmQWfraRkuWiCorJZGxte27xpjgu7u62sRyCm92xQRNgp5RiGHxP913+
HQIDAQABo2YwZDAOBgNVHQ8BAf8EBAMCAQYwEgYDVR0TAQH/BAgwBgEB/wIBAjAd
BgNVHQ4EFgQUDFiYOhMMWkuq93iNBoC1Udr9wLIwHwYDVR0jBBgwFoAUDFiYOhMM
Wkuq93iNBoC1Udr9wLIwDQYJKoZIhvcNAQELBQADggEBADTAW0FPhfrJQ6oT/WBe
iWTv6kCaFoSuWrIHiB9fzlOTUsicrYn6iBf+XzcuReZ6qBILghYGPWPpOmnap1dt
8UVl0Shdj+hyMbHzxR0XzX12Ya78Lxe1GFg+63XbxNwOURssd9DalJixKcyj2BW6
F6JG1aBQhdgGSBhsCDvG1zawqgZX/h4VWG55Kv752PYBrQOtUH8CS93NfeB5Q7bE
FOuyvGVd1iO40JQLoFIkZuyxNh0okGjfmT66dia7g+bC0v1SCMiE/UJ9uvHvfPYe
qLkSRjIHH7FH1lQ/AKqjl9qrpZe7lHplskQ/jynEWHcb60QRcAWPyd94OPrpLrTU
64g=
-----END CERTIFICATE-----

2. Log in to the warehouse

docker login 172.19.2.139
Username: admin
Password: Cmcc@1ot

3. Upload image test

Create gcgj warehouse on Gabor before push

docker tag gcgj/portal:latest 192.168.13.45/gcgj/portal
docker login -p admin -u Cmcc@1ot -e 172.19.2.139
docker push 192.168.13.45/gcgj/portal

III. jenkins configuration

1. Configure the parameterized construction process in general

New String Parameter

Name: VERSION

Default value: [empty]

Description: Please enter version number

2. Source management Git settings

Repository URL by http://172.19.2.140:18080/lvqingshan/gcgj.git

3. Set Gitlab to automatically trigger the build if there is a change

Check whether the gitlab project changes once a minute

*/1 * * * *

4.Execute shell settings

There are two ways to control version. When the build is automatically triggered or the version number is empty, the timestamp is used as the version. When the version number is filled, the filled version number is used

imagesid=`docker images | grep -i gcgj | awk '{print $3}'| head -1`
project=/var/lib/jenkins/jobs/build-docker-router-portal/workspace

if [ -z "$VERSION" ];then
	VERSION=`date +%Y%m%d%H%M`
fi

echo $VERSION

if docker ps -a|grep -i gcgj;then
   docker rm -f gcgj
fi

if [ -z "$imagesid" ];then
	echo $imagesid "is null"
else
	docker rmi -f $imagesid 
fi

docker build -t gcgj/portal:$VERSION $project

docker run -p 38080:8080 -idt --name gcgj gcgj/portal:$VERSION


docker tag gcgj/portal:$VERSION 172.19.2.139/gcgj/portal:$VERSION
docker login -u admin -p Cmcc@1ot 172.19.2.139
docker push 172.19.2.139/gcgj/portal:$VERSION


Posted by Valect on Fri, 06 Dec 2019 15:07:19 -0800