如何使用管道脚本和管道参数化 jenkins DSL 作业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 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我已经在jenkins脚本中添加了环境变量,其变量我需要如下参数化到yaml文件,并且对我的预期有效,
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,