john-westcott-iv / ansible-tower-plugin

Jenkins ansible tower plugin
MIT License
4 stars 42 forks source link

Reliable method to send back values also within declarative and scripted Pipelines #6

Closed ericzolf closed 5 years ago

ericzolf commented 5 years ago

The current method using EnvInject can't work within a declarative Pipeline (statement from CloudBees support), and possibly not even within a scripted Pipeline, as we couldn't get it to run, even with support. Additionally the EnvInject Plug-In is out of maintainer and generates exceptions in Jenkins, so it should be (IMHO) avoided.

Workaround in the meantime has been adapted from https://github.com/automaticdavid/jenjen/blob/master/Jenkinsfile and looks as follows for a simple Pipeline (the link is for multibranch pipeline):

node() {
    towervars=[:]
    stage('tower-stage') {
    ansibleTower(
        extraVars: """---
        app_version: "${params.app_version}"
        app_name: "${params.app_name}"
        """,
        importTowerLogs: true,
        importWorkflowChildLogs: false,
        jobTemplate: "job-test-jenkins-return_stats",
        jobType: 'run',
        limit: '',
        removeColor: false, skipJobTags: '',
        templateType: 'job',
        towerServer: 'My Ansible Tower',
        verbose: true
       )
            def jh = env.JENKINS_HOME
            jn = env.JOB_NAME
            def ji = env.BUILD_ID
            def file = jh + '/jobs/' + jn + '/builds/' + ji + '/injectedEnvVars.txt'  

            readFile(file).split('\n').each { line ->
                l=line.split("=",2)
                k=l[0]
                v=l[1]
                towervars[k]=v
            }
    }
    stage('next-stage') {
       echo towervars["binary_artifacts"]
    }
}

The workaround worked for us when sending the vars back using the set_stats approach, but not when using the debug/msg/JENKINS_EXPORT method.

john-westcott-iv commented 5 years ago

This should be fixed in the new version of the plugin.