You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将脚本式Jenkinsfile转换为声明式以构建Docker镜像?

Converting Your Scripted Docker Pipeline to Declarative Jenkinsfile

Hey there! I totally get where you're coming from—declarative Jenkinsfiles can feel like a leap when you're starting out, and the official docs can be pretty dense. Let's turn that scripted example you have into a clean, declarative pipeline that's easier to read and maintain.

Here's the direct conversion, with explanations below:

pipeline {
    agent any  // Use this if your Jenkins agent already has Docker installed
    // Alternatively, if you need a Docker-enabled container to run the build:
    // agent { docker { image 'docker:latest' } }

    stages {
        stage('Checkout Source Code') {
            steps {
                checkout scm
            }
        }

        stage('Build & Push Docker Image') {
            steps {
                script {
                    // Wrap scripted-style Docker calls in a script block for declarative
                    def customImage = docker.build("my-image:${env.BUILD_ID}")
                    customImage.push()
                }
            }
        }
    }
}

Key Breakdown for Newbies:

  • Agent Configuration:

    • agent any tells Jenkins to run this pipeline on any available agent that has Docker installed and configured.
    • If your agents don't have Docker pre-setup, you can use the containerized agent option (agent { docker { image 'docker:latest' } })—just make sure your Jenkins server has Docker-in-Docker (DinD) access configured for this to work.
  • Stages: We split the work into named stages to make the pipeline's progress visible in the Jenkins UI. This is one of the big benefits of declarative pipelines—better readability and tracking.

    • Checkout Source Code: This uses the built-in checkout scm step to pull your code from your configured source control (Git, SVN, etc.)—exactly what your original script did, but wrapped in a clear stage.
    • Build & Push Docker Image: Since the Docker build/push methods are scripted-style Groovy calls, we wrap them in a script block. This is the standard way to use scripted code within a declarative pipeline. The image tag uses env.BUILD_ID to ensure each build gets a unique image tag, just like your original script.

Quick Bonus Tip:

If you're pushing to a private Docker registry, you'll need to add authentication. You can do this inside the script block with docker.login(username: 'your-user', password: 'your-pass'), or use the cleaner declarative wrapper:

docker.withRegistry('https://your-registry-url', 'registry-credentials-id') {
    def customImage = docker.build("my-image:${env.BUILD_ID}")
    customImage.push()
}

Just make sure you've stored your registry credentials in Jenkins with the ID registry-credentials-id.

内容的提问来源于stack exchange,提问作者MarkH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:43:55