我无法弄清楚为什么我的Jenkinsfile中总是出现语法错误

时间:2018-12-17 22:06:20

标签: jenkins-pipeline

node('linux') { 
    stage('Create Test Stack') {
        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'cfcfed3e-bb3b-4b78-97a1-0ca1e7b9226e', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
            sh 'aws cloudformation create-stack --stack-name final-test --template-url https://s3.amazonaws.com/rcooper-bucket-bucket/docker-single-server.json --region us-east-1 --parameters ParameterKey=KeyName,ParameterValue=MyKeyPair2 ParameterKey=YourIp,ParameterValue=xxx.xx.xxx.xx/32'
            sh 'aws cloudformation wait stack-create-complete --stack-name stack-test --region us-east-1'
            sh 'aws cloudformation describe-stacks --stack-name stack-test --region us-east-1'       
            sh """
            docker1_ip=\$(aws cloudformation describe-stacks --stack-name stock-test --region us-east-1 --query \'Stacks[].Outputs[?OutputKey==`manager1PublicIp`].OutputValue[]\' --output text)
            sshagent (credentials: ['c8a72a93-6041-4983-973e-c4f7f3d10ebb']) {
                ssh -o StrictHostKeyChecking=no ubuntu@\${docker1_ip} \'uptime\'
                   }
            """
}
}
}

我不断收到此错误:

  

/workspace/final@tmp/durable-56b16501/script.sh:第4行:语法错误:意外的单词(预期为“)”)

2 个答案:

答案 0 :(得分:0)

我解决了这个问题。出于某种原因,“”对中的ssh行无法按预期工作。

答案 1 :(得分:0)

您可以从shell执行返回中获取docker1_ip的值。然后在sshagent中使用docker1_ip

node('linux') { 
    stage('Create Test Stack') {
        withCredentials([[$class: 'AmazonWebServicesCredentialsBinding', accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'cfcfed3e-bb3b-4b78-97a1-0ca1e7b9226e', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY']]) {
            sh 'aws cloudformation create-stack --stack-name final-test --template-url https://s3.amazonaws.com/rcooper-bucket-bucket/docker-single-server.json --region us-east-1 --parameters ParameterKey=KeyName,ParameterValue=MyKeyPair2 ParameterKey=YourIp,ParameterValue=xxx.xx.xxx.xx/32'
            sh 'aws cloudformation wait stack-create-complete --stack-name stack-test --region us-east-1'
            sh 'aws cloudformation describe-stacks --stack-name stack-test --region us-east-1' 

            docker1_ip = sh(
                returnStdout: true,
                script: "aws cloudformation describe-stacks --stack-name stock-test --region us-east-1 --query \'Stacks[].Outputs[?OutputKey==`manager1PublicIp`].OutputValue[]\' --output text"
            ).trim()


            sshagent (credentials: ['c8a72a93-6041-4983-973e-c4f7f3d10ebb']) {
                sh "ssh -o StrictHostKeyChecking=no ubuntu@${docker1_ip} uptime"
            }
        }
    }
}