Jenkinsfile不会替代变量

时间:2019-09-30 14:46:26

标签: jenkins jenkins-pipeline

我有一个相当简单的脚本,但是Jenkinsfile从未替代变量(since),而且我不确定为什么。 我尝试过$since${since}语法,每次替换都是空的。参数工作正常。

since = ''

pipeline {
    agent any
    options {
        buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
    }
    parameters {
        string(defaultValue: '', description: 'The service name you wish to display status', name: 'serviceName')
        choice(choices: ['swarm-group-test', 'swarm-group-prod'], description: 'Swarm environment to remove from', name: 'swarmEnv')
        string(defaultValue: '5', description: 'Number of minutes to look back in logs', name: 'numMinutes')
    }
    tools {
        nodejs "node-js-11.12.0"
    }
    stages {
        stage('Invoke Playbook') {
            steps {
                script {
                    sh (script: "node -p -e \"var a = new Date(); a.setMinutes(a.getMinutes() - ${numMinutes}); a.toISOString();\"", returnStdout: true).trim()
                }
                echo "since: ${since}"
                ansiColor('xterm') {
                    sh '''
                        export ANSIBLE_FORCE_COLOR=true
                        export ANSIBLE_STDOUT_CALLBACK=debug
                        ansible-playbook -b -v -u jenkins playbooks/display-service-status.yml -k --extra-vars="serviceName=$serviceName swarmEnv=$swarmEnv since=$since" -i playbooks/swarm-hosts
                    '''
                }
            }
        }
    }
}

输出:

since: 2019-09-30T12:43:12.134Z
ansible-playbook -b -v -u jenkins playbooks/display-service-status.yml -k '--extra-vars=serviceName=TEST_openam swarmEnv=swarm-group-test since=' -i playbooks/swarm-hosts

1 个答案:

答案 0 :(得分:2)

我相信您不能在单引号字符串中替换Jenkins变量。

这是一个有用的文档:https://gist.github.com/Faheetah/e11bd0315c34ed32e681616e41279ef4