1、部署gitlab
1.1 部署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
访问地址:http://IP:9999
初次会先设置管理员密码 ,然后登陆,默认管理员用户名root,密码就是刚设置的。
1.2 创建项目,提交测试代码
进入后先创建项目,提交代码,以便后面测试。
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镜像仓库
2.1 安装docker与docker-compose
# 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 解压离线包部署
# 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 在Jenkins主机配置Docker可信任
由于habor未配置https,还需要在docker配置可信任。
# cat /etc/docker/daemon.json
{"registry-mirrors": ["http://f1361db2.m.daocloud.io"],
"insecure-registries": ["192.168.31.70"]
}
# systemctl restart docker
3、部署Jenkins
3.1 准备JDK和Maven环境
# 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
使用/root/.ssh中私钥访问gitlab
3.2 安装插件
系统管理-->插件管理-->Installed
搜索git/pipeline,点击安装。
4、发布测试
4.1 添加参数化构建
This project is parameterized -> String Parameter
Name:Branch # 变量名,下面脚本中调用
Default Value:master # 默认分支
Description:发布的代码分支 # 描述
4.2 Pipeline脚本
#!/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 添加凭据
1、添加拉取git代码凭据,并获取id替换到上面git_auth变量值。
2、添加拉取harbor镜像凭据,并获取id替换到上面docker_registry_auth变量值。