如何使用管道脚本和管道参数化 jenkins DSL 作业docker-compose.yaml

发布于 2025-01-18 18:29:58 字数 1451 浏览 4 评论 0原文

我已经创建了DSL作业 nv_dsl.groovy 如下,

import groovy.text.SimpleTemplateEngine

def fileContents = readFileFromWorkspace "nv_pipeline.groovy" 
def fileContents1 = readFileFromWorkspace "docker-compose.yaml"
def engine = new SimpleTemplateEngine()

template = engine.createTemplate(fileContents).make(binding.getVariables()).toString()

template1 = engine.createTemplate(fileContents1).make(binding.getVariables()).toString()


pipelineJob("${DSL_JOB}") {

  definition {
    cps { 
        script(template)
    }
  }
}

nv_pipeline.groovy 如下所示,

pipeline {
  agent any

  environment {
    REPO = repository
  }

  parameters {
    choice name: "ENVIRONMENT", choices: environments
  }

  stages {
    stage('Deploy') {
      steps {
          echo "Deploying ${env.REPO} to ${params.ENVIRONMENT}..."
      }
    }
  }
}

是我的 docker-compose.yml 文件

version: "3.3"
services:
${SERVICES}:
    restart: always
    container_name: ${CONTAINER_NAME}
    build:
      context: ${CONTEXT}
      args:
        NODE: ${NODE_ENV}
    ports:
        - ${PORTS}
    environment:
      NODE_ENV: ${NODE_ENV}
    volumes:
      - ${VOLUMES}

下面 ,如何将我的docker-compose.yml文件添加到jenkins dsl ie nv_dsl.groovy ,以便我能够通过适用于docker-compose.yml文件的jenkins dsl传递参数?

在构建管道作业时,我会遇到错误“ container_name变量未设置。默认为空字符串”。

I've created a DSL job nv_dsl.groovy as below,

import groovy.text.SimpleTemplateEngine

def fileContents = readFileFromWorkspace "nv_pipeline.groovy" 
def fileContents1 = readFileFromWorkspace "docker-compose.yaml"
def engine = new SimpleTemplateEngine()

template = engine.createTemplate(fileContents).make(binding.getVariables()).toString()

template1 = engine.createTemplate(fileContents1).make(binding.getVariables()).toString()


pipelineJob("${DSL_JOB}") {

  definition {
    cps { 
        script(template)
    }
  }
}

nv_pipeline.groovy as follows,

pipeline {
  agent any

  environment {
    REPO = repository
  }

  parameters {
    choice name: "ENVIRONMENT", choices: environments
  }

  stages {
    stage('Deploy') {
      steps {
          echo "Deploying ${env.REPO} to ${params.ENVIRONMENT}..."
      }
    }
  }
}

Below is my docker-compose.yml file

version: "3.3"
services:
${SERVICES}:
    restart: always
    container_name: ${CONTAINER_NAME}
    build:
      context: ${CONTEXT}
      args:
        NODE: ${NODE_ENV}
    ports:
        - ${PORTS}
    environment:
      NODE_ENV: ${NODE_ENV}
    volumes:
      - ${VOLUMES}

Here, how can I add my docker-compose.yml file to Jenkins DSL i.e nv_dsl.groovy, so that I can able to pass parameters from Jenkins DSL that apply to the docker-compose.yml file?

While building a pipeline job, I'm getting the error "The CONTAINER_NAME variable is not set. Defaulting to a blank string".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

思念满溢 2025-01-25 18:29:58

我已经在jenkins脚本中添加了环境变量,其变量我需要如下参数化到yaml文件,并且对我的预期有效,

pipeline {
  agent any

 environment {
   CONTAINER_NAME = "${CONTAINER_NAME}"
   PORTS = "${PORTS}"
   VOLUMES = "${VOLUMES}"
   NODE_ENV = "${NODE_ENV}"
   IMAGE = "${IMAGE}"
 }

 parameters {
   choice name: "ENVIRONMENT", choices: environments
 }

 stages {
   stage('Deploy') {
     steps {
       echo "Deploying ${env.REPO} to ${params.ENVIRONMENT}..."
     }
   }
 }
}

I've added environment variables in Jenkins script whose variables I need to parameterize to YAML file as below and it worked for me as expected,

pipeline {
  agent any

 environment {
   CONTAINER_NAME = "${CONTAINER_NAME}"
   PORTS = "${PORTS}"
   VOLUMES = "${VOLUMES}"
   NODE_ENV = "${NODE_ENV}"
   IMAGE = "${IMAGE}"
 }

 parameters {
   choice name: "ENVIRONMENT", choices: environments
 }

 stages {
   stage('Deploy') {
     steps {
       echo "Deploying ${env.REPO} to ${params.ENVIRONMENT}..."
     }
   }
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文