Jenkins Active Choices Parameter with Docker to get Azure tags

不羁岁月 提交于 2021-01-07 02:47:03

问题


For deployment jobs, I want to set up a Jenkins job which receives all existing tags from a specific Azure repository and makes them available for selection in an Active Choices parameter.

I tried several things but nothing has worked. In the code below you can see the last code with which I tried. I want to pull a Docker container which has our Azure CLI and our config in it, after that I want to access the container and start an Azure command (which is not in the code yet, as it fails prior to this step). The Error I get is:

groovy.lang.MissingMethodException: No signature of method Script1.sh() is applicable for argument types: (Java.lang.String) values: "docker login -u... and so on"

def dockerImage = 'ourRegistry/deploy/azure'
def output = []
try {    
          sh 'docker login -u="our_robot_user" -p="TOKEN" ourRegistry && docker pull ${dockerImage}'
          dockerRun = docker.image(dockerImage).withRun('--env-file=azure.env')
          dockerRun.inside("-u user") {
                output.push("INSIDE")
                //res = sh(returnStdout: true, script: 'az acr repository show-tags --name xx --subscription "xx" --repository "xx"')
                //output.push(res)
    }
    } catch (error) {
          output.push(error)
    }
return output

Is it even possible to run this in an Active Choices parameter? Or can I import specific libraries to get this working? Or is there a better way?


回答1:


I have successfully reproduced the complete scenario (i.e., the requirement of 'setting up a Jenkins job which receives all the existing tags from a specific Azure Container Registry repository and make them available for selection as parameters while we build the job') by following the below mentioned process.

Just FYI this is achieved by using "choice parameter" as shown in below screenshot.

Pre-requisites for the below Jenkins pipeline script to work are

  1. to have Azure CLI installed in the node where you run the pipeline (i.e., in Jenkins master in this example)
  2. to have 'jq' command-line JSON processor available in the node where you run the pipeline (i.e., in Jenkins master in this example)
  3. to add Azure service principal to Jenkins credential as instructed here -> https://docs.microsoft.com/en-us/azure/jenkins/execute-cli-jenkins-pipeline#add-azure-service-principal-to-jenkins-credential

Please find below Jenkins pipeline script.

#!groovy
import groovy.transform.Field
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript
def props = []
@Field
def newParams = []
node {
    try {
        regenerateJob = (params.RegenerateJob == null) ? true : params.RegenerateJob
    }
    catch (MissingPropertyException e) {
        regenerateJob = true
    }
    stage('test'){
        withCredentials([azureServicePrincipal('JENKINSSERVICEPRINCIPALCREDENTIALID')]) {
            def shtagsoutput = sh (returnStdout: true, script: '''az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID > /dev/null
            az account set -s $AZURE_SUBSCRIPTION_ID > /dev/null
            shtagsoutputtemp=$(az acr repository show-tags --name "AZURECONTAINERREGISTRYNAME" --subscription $AZURE_SUBSCRIPTION_ID --repository "AZURECONTAINERREGISTRYREPOSITORYNAME" | jq '.[]')
            shtagsoutputtempfinal=$(echo $shtagsoutputtemp | sed 's/"//g')
            echo "${shtagsoutputtempfinal}"''').split(' ')
        shtagsoutputfinal = [shtagsoutput].flatten()
        newParams += [$class: 'ChoiceParameterDefinition', name: 'Phase', choices: shtagsoutputfinal]
        newParams += [$class: 'BooleanParameterDefinition', name: 'RegenerateJob', defaultValue: false]
        props += [$class: 'ParametersDefinitionProperty', parameterDefinitions: newParams]
        properties(properties: props)
        }
    }
}

Note that when this Jenkins job is created, it stands with no parameters. But after the first build, the Jenkins job is regenerated and picked with its new parameters.

Cheers!!

Regards, Krishna



来源:https://stackoverflow.com/questions/54233150/jenkins-active-choices-parameter-with-docker-to-get-azure-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!