Jenkins自动构建docker镜像,并推送到阿里云
这篇主要介绍使用Jenkins自动从git仓库拉取代码并构建镜像,最后推送到阿里云
Jenkins安装 请参考:https://www.cuiwei.net/p/1392307197
ssh连接gitee 请参考:https://www.cuiwei.net/p/1475072228
准备
- 配置证书
- 了解两个插件:
docker-build-step
和Version Number
,其中Version Number
不是必须的
Version Number
支持生成更复杂的版本信息。
在 “某任务” - “配置” - “构建环境” 找到 “Create a formatted version number”。这里配置 增加变量BUILD_VERSION
,格式为 ${BUILD_DATE_FORMATTED, "yyyy-MM-dd"}.${BUILD_NUMBER}
。
构建镜像时使用这个变量BUILD_VERSION
看结果,默认的镜像名称是BUILD_NUMBER
,现在已经变成${BUILD_DATE_FORMATTED, "yyyy-MM-dd"}.${BUILD_NUMBER}
docker-build-step
Set Docker URL In Jenkins global configuration, you need to specify Docker REST API URL.
Jenkins -> Manage Jenkins -> Configure System -> Docker Builder
- Configure Docker server REST API URL
- For Linux nodes, set the local socket
unix:///var/run/docker.sock
- For other nodes, you may need to set something like
tcp://127.0.0.1:2375
- For Linux nodes, set the local socket
- Test the connection.
具体操作
拉取master的代码 -> Creat/build image -> Tag image -> Push image -> Remove image
日志
[Docker] INFO: Step 1/1 : FROM busybox:latest
[Docker] INFO:
[Docker] INFO: ---> 9d5226e6ce3f
[Docker] INFO: Successfully built 9d5226e6ce3f
[Docker] INFO: Successfully tagged 2022-12-01.28:latest
[Docker] INFO: Build image id:9d5226e6ce3f
[Docker] INFO: start tagging image 2022-12-01.28:latest in registry.cn-hangzhou.aliyuncs.com/cuiw/test as 28
[Docker] INFO: Tagged image 2022-12-01.28:latest in registry.cn-hangzhou.aliyuncs.com/cuiw/test as 28
[Docker] INFO: Pushing image registry.cn-hangzhou.aliyuncs.com/cuiw/test:28
[Docker] INFO: Done pushing image registry.cn-hangzhou.aliyuncs.com/cuiw/test:28
[Docker] INFO: Removed image 2022-12-01.28:latest
Finished: SUCCESS
常见问题
问题1
[Docker] ERROR: Failed to create docker image: connect(..) failed: Permission denied: /var/run/docker.sock
依次执行以下命令,然后重启就好了
gpasswd -a jenkins docker
newgrp docker
问题2
[Docker] ERROR: Failed to exec start:null
[Docker] ERROR: Failed to create docker image: null
ERROR: Build step failed with exception
java.util.NoSuchElementException
at java.base/java.util.ArrayDeque.removeFirst(ArrayDeque.java:363)
...
这个问题很严重,折腾我好几天。有时行,有时不行(很多时候都不行),暂时无解!
参考
https://www.jianshu.com/p/5c3e6f2c5d15
https://blog.csdn.net/qq_39905409/article/details/122449380