簡體   English   中英

如何使用管道腳本和 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 ,以便我能夠從適用於 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM