环境要求
-
已安装docker并设置为开机自启
-
已安装Docker-Compose
Jenkins
基于docker安装Jenkins
jenkins官网
https://www.jenkins.io/
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key
wget -O /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum -y install jenkins
编写docker-compose.yml文件
vi /usr/local/docker/jenkins_docker/docker-compose.yml
version: "3.1"
services:
jenkins:
image: jenkins/jenkins:2.401.2-lts
container_name: jenkins
ports:
- 8080:8080
- 50000:50000
volumes:
- ./data/:/var/jenkins_home/
首次启动会因为数据卷data目录没有权限导致启动失败,设置data目录写权限(/usr/local/docker/jenkins_docker/data)
chmod -R a+w data/
启动jenkins
在/usr/local/docker/jenkins_docker目录下执行
/usr/local/docker/jenkins_docker/docker-compose restart
访问地址
http://192.168.56.103:8080/
账号密码
root/root
配置源码拉取地址
点击构建下载源码
源码位置
/usr/local/docker/jenkins_docker/data/workspace
配置Maven构建代码
配置maven
配置jdk
新建任务
clean package -DskipTest
坑死人不偿命,有可能maven变了,地下的settings.xml不边,需要核对
发送到目标服务器
配置ssh
将spring-boot项目打包成docker镜像并在目标服务器运行
项目src同级添加docker目录,并在docker目录下创建DockerFile
FROM daocloud.io/library/java:8u40-jdk
COPY hello-jenkins-0.0.1-SNAPSHOT.jar /usr/local/
WORKDIR /usr/local
CMD java -jar hello-jenkins-0.0.1-SNAPSHOT.jar
解释:
- FROM daocloud.io/library/java:8u40-jdk 基于aocloud.io/library/java:8u40-jdk镜像构建
- COPY hello-jenkins-0.0.1-SNAPSHOT.jar /usr/local/ 将主机上得hello-jenkins-0.0.1-SNAPSHOT.jar复制到(生成的)容器内的/usr/local/ 目录下
- WORKDIR /usr/local 容器的工作目录,这意味着后续的命令将在这个目录下执行。你的可执行JAR文件也位于这个目录中。
- CMD java -jar hello-jenkins-0.0.1-SNAPSHOT.jar 容器启动时执行的默认命令
- 只需要修改一下jar即可
在docker目录下新建docker-compose.yml文件
version: "3.1"
services:
hello-jenkins:
build:
context: ./
dockerfile: DockerFile
image: hello-jenkins:v1.0.0
container_name: hello-jenkins
ports:
- 80:80
解释:
hello-jenkins
:
- 这是服务的名称,你可以根据需要将其更改为你喜欢的名称。
遇到的坑:关闭防火墙之后拉去不到代码了,开启防火墙之后就正常了.
ERROR: Error fetching remote repo 'origin'
hudson.plugins.git.GitException: Failed to fetch from https://gitee.com/LessAndfaster/hello-jenkins.git
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1003)
at hudson.plugins.git.GitSCM.retrieveChanges(GitSCM.java:1245)
at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1309)
at hudson.scm.SCM.checkout(SCM.java:540)
at hudson.model.AbstractProject.checkout(AbstractProject.java:1240)
at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:649)
at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:85)
at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:521)
at hudson.model.Run.execute(Run.java:1900)
at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:44)
at hudson.model.ResourceController.execute(ResourceController.java:101)
at hudson.model.Executor.run(Executor.java:442)
Caused by: hudson.plugins.git.GitException: Command "git fetch --tags --force --progress -- https://gitee.com/LessAndfaster/hello-jenkins.git +refs/heads/*:refs/remotes/origin/*" returned status code 128:
stdout:
stderr: fatal: unable to access 'https://gitee.com/LessAndfaster/hello-jenkins.git/': Could not resolve host: gitee.com
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2842)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandWithCredentials(CliGitAPIImpl.java:2185)
at org.jenkinsci.plugins.gitclient.CliGitAPIImpl$1.execute(CliGitAPIImpl.java:635)
at hudson.plugins.git.GitSCM.fetchFrom(GitSCM.java:1001)
... 11 more
ERROR: Error fetching remote repo 'origin'
SSH: Current build result is [FAILURE], not going to run.
Finished: FAILURE
参数化构建
cd /var/jenkins_home/workspace/hello-jenkins-parameter
git checkout $release
/var/jenkins_home/maven/bin/mvn package -DskipTests
Harbor
Habor需要2G左右的磁盘,请确保磁盘够用
vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://d8b3zdiw.mirror.aliyuncs.com"
],
"insecure-registries": [
"192.168.56.103:80"
]
}
注意:80一定要写,要不然很有可能走443,然后报安全问题
重启docker
systemctl daemon-reload
systemctl restart docker
重启docker后harbor只启动了一下6个服务,需要重启harbor
harbor-jobservice
harbor-db
registry
redis
registryctl
harbor-log
cd /usr/local/harbor
docker-compose restart
完整的harbor服务
harbor-jobservice
nginx
harbor-core
harbor-portal
harbor-db
registry
redis
registryctl
harbor-log
登录仓库
docker login -u DevOps -p 1qaz@WSX 192.168.56.103:80
注意:80一定要写(默认端口不一定就是80还有443)
添加镜像tag
docker tag 66a03cc3266e 192.168.56.103:80/hello-jenkins/hello-jenkins:v1.0.0
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.56.103:80/hello-jenkins/hello-jenkins v1.0.0 66a03cc3266e 28 hours ago 847MB
hello-jenkins v1.0.0 66a03cc3266e 28 hours ago 847MB
推送镜像
docker push 192.168.56.103:80/hello-jenkins/hello-jenkins:v1.0.0
在jenkins内部使用宿主机docker
修改宿主机docker.sock权限:
chown root:root /var/run/docker.sock
chmod o+rw /var/run/docker.sock
为jenkins添加数据卷
vim /usr/local/docker/jenkins_docker/docker-compose.yml
version: "3.1"
services:
jenkins:
image: jenkins/jenkins:2.401.2-lts
container_name: jenkins
ports:
- 8080:8080
- 50000:50000
volumes:
- ./data/:/var/jenkins_home/
- /usr/bin/docker:/usr/bin/docker
- /var/run/docker.sock:/var/run/docker.sock
- /etc/docker/daemon.json:/etc/docker/daemon.json
docker-compose up -d
补充:
docker-compose up -d 创建docker-compose中定义的服务,并启动容器.
docker-compose只会启动通过docker-compose up -d创建好的处于停止状态的容器
添加构建后操作
mv target/*.jar docker/
docker build -t hello-jenkins:$tag -f docker/DockerFile docker/
docker login -u admin -p Harbor12345 192.168.56.103:80
docker tag hello-jenkins:$tag 192.168.56.103:80/hello-jenkins/hello-jenkins:$tag
docker push 192.168.56.103:80/hello-jenkins/hello-jenkins:$tag
这里的当前目录是(项目目录)
/var/jenkins_home/workspace/hello-jenkins-parameter
默认会找Dockerfile,因为项目里面的名字是DockerFile所以要通过-f指定
部署
部署脚本
harbor_url=$1
harbor_project_name=$2
project_name=$3
tag=$4
port=$5
# 定义要追加输出的文件路径
output_file="/root/deploy.log"
imageName=$harbor_url/$harbor_project_name/$project_name:$tag
containerId=`docker ps -a | grep ${project_name} | awk '{print $1}'`
if [ "$containerId" != "" ] ; then
docker stop $containerId
docker rm $containerId
echo "Delete Container Success" | tee -a "$output_file"
fi
imageId=`docker images | grep ${project_name} | awk '{print $3}'`
if [ "$imageId" != "" ] ; then
docker rmi -f $imageId
echo "Delete Image Success" | tee -a "$output_file"
fi
docker login -u DevOps -p '1qaz@WSX' $harbor_url
docker pull $imageName
docker run -d -p $port:$port --name $project_name $imageName
echo "Start Container Success" | tee -a "$output_file"
echo $project_name | tee -a "$output_file"
添加构建后操作
Send build artifacts over SSH
deploy.sh 192.168.56.103:80 hello-jenkins ${JOB_NAME} $tag $container_port $port
k8s文章来源:https://www.toymoban.com/news/detail-855305.html
# 修改 hostname
hostnamectl set-hostname k8smaster
# 查看修改结果
hostnamectl status
# 设置 hostname 解析
echo "127.0.0.1 $(hostname)" >> /etc/hosts
流水线语法的位置(具体流水线上)文章来源地址https://www.toymoban.com/news/detail-855305.html
http://192.168.56.103:8080/job/pipeline4test/pipeline-syntax/
到了这里,关于基于Jenkins的CICD的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!