简体   繁体   English

如何使用声明式 Jenkinsfile 构建 docker 镜像

[英]How to build docker images using a Declarative Jenkinsfile

I'm new to using Jenkins....我是使用 Jenkins 的新手....

I'm trying to automate the production of an image (to be stashed in a repo) using a declarative Jenkinsfile.我正在尝试使用声明性 Jenkinsfile 自动生成图像(存储在 repo 中)。 I find the documentation to be confusing (at best).我发现文档令人困惑(充其量)。 Simply put, how can I convert the following scripted example (from the docs )简而言之,我如何转换以下脚本示例(来自文档

node {
    checkout scm
    def customImage = docker.build("my-image:${env.BUILD_ID}")
    customImage.push()
}

to a declarative Jenkinsfile....到声明性的 Jenkinsfile ....

You can use scripted pipeline blocks in a declarative pipeline as a workaround 您可以在声明性管道中使用脚本化管道块作为解决方法

pipeline {
    agent any
    stages {
        stage('Build image') {
            steps {
                echo 'Starting to build docker image'

                script {
                    def customImage = docker.build("my-image:${env.BUILD_ID}")
                    customImage.push()
                }
            }
        }
    }
}

I'm using following approach: 我正在使用以下方法:

steps {
   withDockerRegistry([ credentialsId: "<CREDENTIALS_ID>", url: "<PRIVATE_REGISTRY_URL>" ]) {
      // following commands will be executed within logged docker registry
      sh 'docker push <image>'
   }
}

Where: 哪里:

  • CREDENTIALS_ID stands for key in Jenkis under which you store credentials to your docker registry. CREDENTIALS_ID代表Jenkis中的密钥,您可以在其中将凭据存储到docker注册表。
  • PRIVATE_REGISTRY_URL stands for url of your private docker registry. PRIVATE_REGISTRY_URL代表您的私人码头注册表的网址。 If you are using docker hub then it should be empty. 如果您使用的是docker hub,那么它应该是空的。

I cannot recommend the declarative syntax for building a Docker image bcos it seems that every important step requires falling back to the old scripting syntax.我不能推荐用于构建 Docker 映像的声明性语法 bcos 似乎每个重要步骤都需要回退到旧的脚本语法。 But if you must, a hybrid approach seems to work.但是,如果必须的话,混合方法似乎可行。

First a detail about the scm step: when I defined the Jenkins "Pipeline script from SCM" project that fetches my Jenkinsfile with a declarative pipline from git, Jenkins cloned the repo as the first step in the pipeline even tho I did not define a scm step .首先是有关 scm 步骤的详细信息:当我定义 Jenkins“来自 SCM 的管道脚本”项目时,该项目使用来自 git 的声明性管道获取我的 Jenkinsfile,即使我没有定义 scm ,Jenkins 也将 repo 作为管道中的第一步进行了克隆步骤

For the build and push steps, I can only find solutions that are a hybrid of old-style scripted pipeline steps inside the new-style declarative syntax.对于构建和推送步骤,我只能找到在新式声明性语法中混合了旧式脚本化管道步骤的解决方案。 For example see gustavoapolinario's work at Medium:例如,请参阅 gustavoapolinario 在 Medium 上的工作:

https://medium.com/@gustavo.guss/jenkins-building-docker-image-and-sending-to-registry-64b84ea45ee9 https://medium.com/@gustavo.guss/jenkins-building-docker-image-and-sending-to-registry-64b84ea45ee9

which has this hybrid pipeline definition:它具有此混合管道定义:

pipeline {
  environment {
    registry = "gustavoapolinario/docker-test"
    registryCredential = 'dockerhub'
    dockerImage = ''
  }
  agent any
  stages {
    stage('Cloning Git') {
      steps {
        git 'https://github.com/gustavoapolinario/microservices-node-example-todo-frontend.git'
      }
    }
    stage('Building image') {
      steps{
        script {
          dockerImage = docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
    stage('Deploy Image') {
      steps{
        script {
          docker.withRegistry( '', registryCredential ) {
            dockerImage.push()
          }
        }
      }
    }
    stage('Remove Unused docker image') {
      steps{
        sh "docker rmi $registry:$BUILD_NUMBER"
      }
    }
  }
}

Because the first step here is a clone, I think he built this example as a standalone pipeline project in Jenkins (not a Pipeline script from SCM project).因为这里的第一步是克隆,我认为他在 Jenkins 中将这个示例构建为一个独立的管道项目(不是来自 SCM 项目的管道脚本)。

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

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