简体   繁体   English

如何使用Jenkins管道释放docker镜像

[英]How to release docker image with Jenkins pipeline

I have a project to build a docker image, in my case it's a extension of mongo image which contains (inside the container) data. 我有一个构建docker镜像的项目,在我的例子中,它是mongo image的扩展,它包含(在容器内)数据。 And I use this mongo docker image for integration tests purpose. 我使用这个mongo docker镜像进行集成测试。

At the moment, Maven is not used to build the project. 目前,Maven不习惯构建项目。 When I want to release a version of this image, I do it on my computer which is not ideal. 当我想发布这个图像的版本时,我在我的电脑上做这个并不理想。

I would like to kept a file which contains my current version of my docker image (like pom.xml). 我想保留一个包含我当前版本的docker镜像的文件(如pom.xml)。

What should I do, add a pom.xml in my project for versionning and maven-release-plugin, add a simple version.txt which I update with during my release process ? 我该怎么办,在我的项目中为版本化和maven-release-plugin添加一个pom.xml,添加一个简单的version.txt,我在发布过程中更新了它?

Or is there a like "docker.version" file I do not know to do this ? 或者是否有类似“docker.version”文件,我不知道这样做?

I post an example of my project on GitHub : https://github.com/Ameausoone/mongodb-docker-with-data 我在GitHub上发布了我的项目示例: https//github.com/Ameausoone/mongodb-docker-with-data

First thing is to have a specific repo for each project that you have . 首先是为每个项目设置一个特定的回购 This helps not to mess up the docker images while pushing it to the repository. 这有助于在将Docker图像推送到存储库时弄乱它。

You can use the shell scripts/commands in the pipeline script to execute all the commands which are mentioned here. 您可以使用管道脚本中的shell脚本/命令来执行此处提到的所有命令。

There are multiple ways in which you push the docker images to the repository. 有多种方法可以将docker镜像推送到存储库。

Tagging based on Time and Build Number: 基于时间和内部编号的标记:

Take an example of auth-api, where we create an env variable called $DOCKER_TAG in the jenkins and set it to the time the build is created: 以auth-api为例,我们在jenkins中创建一个名为$ DOCKER_TAG的env变量,并将其设置为创建构建的时间:

DOCKER_TAG: $(date +%Y%m%d).$BUILD_NUMBER 

$BUILD_NUMBER is provided as part of Jenkins job. $ BUILD_NUMBER是Jenkins工作的一部分。 Now tag the docker image as follows: 现在标记docker镜像,如下所示:

docker tag DOCKER_IMAGE auth-api/<DOCKER_IMAGE>:<DOCKER_TAG>

(Assuming you have built the docker image and push it to the repository after tagging- above step.) (假设您已经构建了docker镜像并在标记上面的步骤之后将其推送到存储库。)

NOTE : Docker allows to push the image only if it matches the REPO-NAME) 注意:只有匹配REPO-NAME时,Docker才允许推送图像)

It helps to find out the docker image creation date and time along with the build number. 它有助于找出docker图像创建日期和时间以及内部版本号。 This image can be deployed to different environments like dev, test, prod. 此映像可以部署到dev,test,prod等不同的环境中。

Tagging based on the environment 根据环境进行标记

Tag the docker image based on the deployment environment, ie dev, test, prod. 根据部署环境标记docker镜像,即dev,test,prod。 Configure an environment variable in the pipeline script, using withenv , which may look something like this: 使用withenv在管道脚本中配置环境变量,它可能如下所示:

docker tag DOCKER_IMAGE auth-api/<DOCKER_IMAGE>:$ENV

This will help to have a deployment specific docker images. 这将有助于部署特定的docker镜像。

You can even tag the docker image based on git commit.[not tested personally] 你甚至可以根据git commit标记docker镜像。[未亲自测试]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM