jenkins dsl job script: как получить доступ к переменной окружения (которая вводится через propertiesFile) в шаге downstreamParameterized - PullRequest
1 голос
/ 14 апреля 2019

Коротко мой сценарий задания dsl

job('test') {
  steps {
    shell('echo VERSION=$VERSION > version.txt\n' +
          'echo VERSION_SUFFIX=$VERSION_SUFFIX >> version.txt\n' + 
          'echo GROUP_ID=$GROUP_ID >> version.txt')

    // EnvInject Plugin 
    environmentVariables {
      propertiesFile('version.txt')
    }
  }

  publishers {
    postBuildScripts {
        steps {
            shell('echo ${VERSION}')
        }
        onlyIfBuildSucceeds(false)
        onlyIfBuildFails(false)
    }
    downstreamParameterized {
      trigger('next-job') {
        parameters {
          predefinedProp('relVersion', '${VERSION}')
        }
      }
    }
  }  
}

Мне нужен номер $ VERSION для передачи параметру в последующее задание.

Я пробовал $ {env.VERSION}, а также пробовал много вариантов, но я не смог поймать ВЕРСИЮ.любая помощь приветствуется, спасибо заранее.

1 Ответ

2 голосов
/ 14 апреля 2019

Вы можете использовать опцию Prepare an environment for the run, которая выполняется перед SCM.

Опция Prepare an environment for the run принадлежит не pre-build/ build /post build, а заданию properties.

Нет задания DSL API, поддерживаемого для настройки этой опции. Но мы можем использовать configure block.

job('next-job') {

  configure { project -> 
    project / 'properties' << 'EnvInjectJobProperty' {

        info {
          loadFilesFromMaster false
          propertiesContent 'Branch=${relVersion}'
        }
        keepBuildVariables true
        keepJenkinsSystemVariables true
        overrideBuildParameters false
        on true
    }
  } // end of configure block

  scm { 
    git { 
      remote { 
        url("ssh://git@bitbucket.rl.git") 
      } 
        branches('${branch}') 
    } 
  } // end of scm

  steps {}
  publishers {}
}

Над заданием DSL может генерировать следующий xml в качестве содержимого config.xml * 1014 начального задания *

<project>
    <actions></actions>
    <description></description>
    <keepDependencies>false</keepDependencies>
    <properties>
        <EnvInjectJobProperty>
            <info>
                <loadFilesFromMaster>false</loadFilesFromMaster>
                <propertiesContent>Branch=${relVersion}</propertiesContent>
            </info>
            <keepBuildVariables>true</keepBuildVariables>
            <keepJenkinsSystemVariables>true</keepJenkinsSystemVariables>
            <overrideBuildParameters>false</overrideBuildParameters>
            <on>true</on>
        </EnvInjectJobProperty>
    </properties>
    <canRoam>true</canRoam>
    <disabled>false</disabled>
    <blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
    <blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
    <triggers></triggers>
    <concurrentBuild>false</concurrentBuild>
    <builders></builders>
    <publishers></publishers>
    <buildWrappers></buildWrappers>
    <scm class='hudson.plugins.git.GitSCM'>
        <userRemoteConfigs>
            <hudson.plugins.git.UserRemoteConfig>
                <url>ssh://git@bitbucket.rl.git</url>
            </hudson.plugins.git.UserRemoteConfig>
        </userRemoteConfigs>
        <branches>
            <hudson.plugins.git.BranchSpec>
                <name>${branch}</name>
            </hudson.plugins.git.BranchSpec>
        </branches>
        <configVersion>2</configVersion>
        <doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
        <gitTool>Default</gitTool>
    </scm>
</project>

Вы можете попробовать jod DSL на http://job -dsl.herokuapp.com / , чтобы проверить сгенерированный xml из него, как и ожидалось.

...