1, the deployment gitlab
1.1 deployment gitlab
docker run -d \
--name gitlab \
-p 8443:443 \
-p 9999:80 \
-p 9998:22 \
-v $PWD/config:/etc/gitlab \
-v $PWD/logs:/var/log/gitlab \
-v $PWD/data:/var/opt/gitlab \
-v /etc/localtime:/etc/localtime \
lizhenliang/gitlab-ce-zh:latest
Access address: http: // IP: 9999
For the first time will set the admin password, then log in, the default administrator username root, password is just set.
1.2 Create a project, submit test code
After entering the project to create, submit the code so that the back test.
git clone http://192.168.31.62:9999/root/java-demo.git
git add .
git config --global user.email "[email protected]"
git config --global user.name "Your Name"
git commit -m 'all'
git push origin master
2, Harbor deploy mirroring warehouse
2.1 Installation docker-compose the docker
# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
# yum install docker-ce -y
# systemctl start docker
# systemctl enable docker
2.2 extract the offline package deployment
# tar zxvf harbor-offline-installer-v1.9.1.tgz 下载链接https://pan.baidu.com/s/1tFyDsfbfNeLf9YQSRrOIog
# cd harbor
# vi harbor.yml
hostname: 192.168.31.70
# ./prepare
# ./install.sh
2.3 disposed on Jenkins trusted host Docker
Since habor not https configuration, also you need to configure trusted in the docker.
# cat /etc/docker/daemon.json
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],
"insecure-registries": ["192.168.31.70"]
}
# systemctl restart docker
3, deployment Jenkins
3.1 Preparation JDK and Maven environment
# tar zxvf jdk-8u45-linux-x64.tar.gz
# mv jdk1.8.0_45 /usr/local/jdk
# tar zxf apache-maven-3.5.0-bin.tar.gz
# mv apache-maven-3.5.0 /usr/local/maven
docker run -d --name jenkins -p 80:8080 -p 50000:50000 -u root \
-v /opt/jenkins_home:/var/jenkins_home \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /usr/bin/docker:/usr/bin/docker \
-v /usr/local/apache-maven-3.5.0:/usr/local/maven \
-v /usr/local/jdk1.8.0_45:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
--name jenkins jenkins/jenkins:lts
Use /root/.ssh private key access gitlab
3.2 Plug
System Management -> Plugin Manager -> Installed
Search git / pipeline, click Install.
4, release test
4.1 add parametric building
This project is parameterized -> String Parameter
Name: Branch # variable name, call the following script
Default Value: master # default branch
Description: The original source branch # Description
4.2 Pipeline script
#!/usr/bin/env groovy
def registry = "192.168.31.70"
def project = "welcome"
def app_name = "demo"
def image_name = "${registry}/${project}/${app_name}:${Branch}-${BUILD_NUMBER}"
def git_address = "[email protected]:/home/git/java-demo.git"
def docker_registry_auth = "b37a147e-5217-4359-8372-17fd9a8edfcc"
def git_auth = "b3e33c8b-c7e0-47b9-baee-d7629d71f154"
pipeline {
agent any
stages {
stage('拉取代码'){
steps {
checkout([$class: 'GitSCM', branches: [[name: '${Branch}']], userRemoteConfigs: [[credentialsId: "${git_auth}", url: "${git_address}"]]])
}
}
stage('代码编译'){
steps {
sh """
JAVA_HOME=/usr/local/jdk
PATH=$JAVA_HOME/bin:/usr/local/maven/bin:$PATH
mvn clean package -Dmaven.test.skip=true
"""
}
}
stage('构建镜像'){
steps {
withCredentials([usernamePassword(credentialsId: "${docker_registry_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
sh """
echo '
FROM ${registry}/library/tomcat:v1
LABEL maitainer 张三
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
' > Dockerfile
docker build -t ${image_name} .
docker login -u ${username} -p '${password}' ${registry}
docker push ${image_name}
"""
}
}
}
stage('部署到Docker'){
steps {
sh """
REPOSITORY=${image_name}
docker rm -f tomcat-java-demo |true
docker container run -d --name tomcat-java-demo -p 88:8080 ${image_name}
"""
}
}
}
}
4.3 Add Credential
1, added pulling of git credentials, and to obtain a replacement top git_auth id variable value.
2, the mirror was added pulling harbor credentials, and acquires id value substituted into the variable docker_registry_auth above.