带有Docker的Jenkins Active Choices参数获得Azure标记 [英] Jenkins Active Choices Parameter with Docker to get Azure tags

查看:92
本文介绍了带有Docker的Jenkins Active Choices参数获得Azure标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于部署作业,我想设置一个Jenkins作业,该作业从特定的Azure存储库接收所有现有标签,并使其在Active Choices参数中可供选择.

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.

我尝试了几件事,但没有任何效果.在下面的代码中,您可以看到我尝试使用的最后一个代码.我想拉一个其中包含我们的Azure CLI和配置的Docker容器,此后,我要访问该容器并启动一个Azure命令(该代码尚不在代码中,因为在此步骤之前失败).我得到的错误是:

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:方法Script1.sh()的签名不适用于参数类型:(Java.lang.String)值:"docker login -u ...等等"

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

是否甚至可以在Active Choices参数中运行它?还是可以导入特定的库来使它正常工作?还是有更好的方法?

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?

推荐答案

我已经成功复制了完整的方案(即设置Jenkins作业以从特定的Azure容器注册表存储库中接收所有现有标签的要求,按照下面提到的过程,使它们可供选择,以作为我们在构建作业时的参数.)

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.

以下Jenkins管道脚本正常工作的先决条件是

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

  1. 在运行管道的节点中安装Azure CLI(例如,在此示例中为Jenkins master)
  2. 在运行管道的节点(即,在本示例中为Jenkins master)中具有"jq"命令行JSON处理器
  3. 按照此处的指示将Azure服务主体添加到Jenkins凭据-> 请在下面找到Jenkins管道脚本.

    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)
            }
        }
    }
    

    请注意,创建此Jenkins作业时,该作业没有任何参数.但是在第一次构建后,Jenkins作业将重新生成并使用其新参数进行选择.

    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.

    干杯!

    关于, 克里希纳

    这篇关于带有Docker的Jenkins Active Choices参数获得Azure标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆