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

How to parameterize jenkins DSL job with pipeline script & docker-compose.yaml

我创建了一个 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,即 nv_dsl.groovy,这样我就可以从 Jenkins DSL 传递适用于 docker-compose.yml 文件的参数?

构建管道作业时,我收到错误消息“未设置 CONTAINER_NAME 变量。默认为空字符串”。

我在 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}..."
     }
   }
 }
}