[英]How do i deploy a Docker image to a certain Docker container with a Jenkinsfile?
I have a Jenkinsfile which build and deploys an Angular Image to Docker and stores it there - I use Portainer as an UI here.我有一个 Jenkinsfile,它构建并将 Angular 映像部署到 Docker 并将其存储在那里 - 我在这里使用 Portainer 作为 UI。 Now i want to add a deployment stage to my Jenkinsfile which deploys the Image to a certain container.
现在我想向我的 Jenkinsfile 添加一个部署阶段,它将图像部署到某个容器。
I tried serveral solutions from the internet but no of them worked for me.我尝试了互联网上的几种解决方案,但没有一个对我有用。
Here is my Jenkinsfile:这是我的詹金斯文件:
pipeline {
agent any
tools {
nodejs 'NodeJS 16.9.1'
dockerTool 'docker-19.03.9'
}
environment {
IMAGE = 'name-angular'
VERSION = 'test'
}
stages {
stage('Build image') {
steps {
script {
sh 'npm install'
sh 'npm run buildProd'
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
echo "Building image ${IMAGE}:${VERSION}..."
dockerImage = docker.build("name:latest")
}
}
}
stage('Deploy image') {
steps {
echo 'Deploying image...'
}
}
stage('Clean up') {
steps {
echo 'Cleaning up...'
}
}
}
} }
As you see my deployment stage is completely empty because i even dont know how to start with this.如您所见,我的部署阶段完全是空的,因为我什至不知道如何开始。
I think the dockerImage
variable will not be transfer to the other stage, so you can call docker
command directly.我认为
dockerImage
变量不会转移到其他阶段,所以你可以直接调用docker
命令。 The below pipeline is an example, so you should fit this to your purpose.以下管道是一个示例,因此您应该根据自己的目的进行调整。
pipeline {
agent any
tools {
nodejs 'NodeJS 16.9.1'
dockerTool 'docker-19.03.9'
}
environment {
IMAGE = readMavenPom().getArtifactId()
VERSION = readMavenPom().getVersion()
}
stages {
stage('Build image') {
steps {
sh 'npm install'
sh 'npm run buildProd'
echo "Building image ${IMAGE}:${VERSION}..."
sh 'docker build -t ${IMAGE}:${VERSION} .'
}
}
stage('Deploy image') {
steps {
echo 'Deploying image...'
sh 'docker push ${IMAGE}:${VERSION}'
}
}
post {
always {
sh 'docker rmi ${IMAGE}:${VERSION}'
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.