我想使用 Spring Boot 应用程序的 CI/CD (Jenkins) 创建 docker 镜像,并将该镜像推送到私有(private) nexus docker 注册表。 如何避免将我的 docker 凭据添加到 POM 文件并将它们放在 GIT 中?我应该在哪里传递/放置凭据?
或者我应该使用docker登录
,docker推送
在jenkins中手动推送图像?
我按照本教程 ( https://docs.spring.io/spring-boot/docs/2.4.0/maven-plugin/reference/htmlsingle/#build-image-example-publish ) 操作,我的 POM 如下所示:
<project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>docker.example.com/library/${project.artifactId}</name>
<publish>true</publish>
</image>
<docker>
<publishRegistry>
<username>user</username>
<password>secret</password>
<url>https://docker.example.com/v1/</url>
<email><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e590968097a5809d8488958980cb868a88" rel="noreferrer noopener nofollow">[email protected]</a></email>
</publishRegistry>
</docker>
</configuration>
</plugin>
</plugins>
</build>
</project>
最佳答案
您可以在 POM 中使用变量并在调用 maven 时传递它们。您应该将凭据存储在 jenkins 凭据管理器中
例如:
...
<properties>
<DOCKER_REGISTRY/>
<DOCKER_REGISTRY_USER/>
<DOCKER_REGISTRY_PASSWORD/>
<DOCKER_IMAGE_NAME>${DOCKER_REGISTRY}/${project.artifactId}:${project.version}</DOCKER_IMAGE_NAME>
</properties>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<image>
<name>${DOCKER_IMAGE_NAME}</name>
<publish>true</publish>
</image>
<docker>
<publishRegistry>
<username>${DOCKER_REGISTRY_USER}</username>
<password>${DOCKER_REGISTRY_PASSWORD}</password>
<url>${DOCKER_REGISTRY}</url>
</publishRegistry>
</docker>
</configuration>
</plugin>
...
然后你可以在你的jenkins管道中像这样调用maven:
stage('Build Docker Image') {
steps {
withCredentials([usernamePassword(credentialsId: 'YOUR_CREDENTIALS_ID', passwordVariable: 'NEXUS_PASSWORD', usernameVariable: 'NEXUS_USER')]) {
sh "mvn -DskipTests=true spring-boot:build-image -DDOCKER_REGISTRY=SUBDOMAIN.DOMAIN.COM -DDOCKER_REGISTRY_USER=$NEXUS_USER -DDOCKER_REGISTRY_PASSWORD=$NEXUS_PASSWORD"
}
}
}
关于spring-boot - 从 Spring Boot POM 文件中删除私有(private) docker 注册表凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66680371/