[英]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 ,以便我能够从适用于 docker-compose.yml 文件的 Jenkins DSL 传递参数?
在构建管道作业时,我收到错误消息“未设置 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}..."
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.