基於Docker構建Jenkins CI平臺
阿新 • • 發佈:2022-02-25
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
gitlab/gitlab-ce: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
# 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 82: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/maven:/usr/local/maven \
-v /usr/local/jdk:/usr/local/jdk \
-v /etc/localtime:/etc/localtime \
--name jenkins jenkins/jenkins:lts
使用/root/.ssh中私鑰訪問gitlab
3.2 安裝外掛
系統管理-->外掛管理-->Installed
搜尋git/pipeline,點選安裝。
sed -i 's/http:\/\/updates.jenkins- ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g' default.json && sed -i 's/http:\/\/www.google.com/https:\/\/www.baidu.com/g' default.json
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 lizhenliang
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變數值。