Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I was following the jenkinsfile documentation and I tried to use a secret text in my pipeline but I got the error "groovy.lang.MissingPropertyException: No such property: $AWS_SECRET_ACCESS_KEY for class: groovy.lang.Binding"

This is my jenkinsfile. I also tried wrapping the variable like echo "${AWS_SECRET_ACCESS_KEY}" but neither works. I found an alternative solution is with a withCredentials line but I want to know why it doesn't work because I found clearer the jenkins file if I add that data as variables.

Implementing "withCredentils" it tells me: "groovy.lang.MissingPropertyException: No such property: set for class: groovy.lang.Binding" and I found that it seems I need to downgrade my security...

pipeline {
    agent any
    environment {
        AWS_ACCESS_KEY     = credentials('AWS_ACCESS_KEY_ID')
        AWS_SECRET_KEY = credentials('AWS_SECRET_ACCESS_KEY')
    stages {
        stage('Test') {
            steps {
                echo 'Testing..'
                echo $AWS_SECRET_KEY
                echo $AWS_ACCESS_KEY
        stage('Deploy') {
            steps {
                echo 'Deploying....'
                sh "npx serverless --no-aws-s3-accelerate --key $AWS_ACCESS_KEY --secret $AWS_SECRET_KEY"

EDIT: fix second implementation of credentials

AWS_SECRET_ACCESS_KEY is to my understanding the ID of the credential object. You need to use $AWS_ACCESS_KEY and $AWS_SECRET_KEY in the shell command. – Christopher Dec 5, 2019 at 22:28
  • echo ${AWS_SECRET_ACCESS_KEY} will not work, because only the credentials steps can return the content of credentials into your pipeline context.

  • withCredentils <-- typo

  • I would suggest to use 'withCredentials' ( https://plugins.jenkins.io/credentials-binding)

    after you have created a new credential (AWS type) (using 'the CloudBees Aws Credential Plugin' : https://wiki.jenkins.io/display/JENKINS/CloudBees+AWS+Credentials+Plugin)

    Im not very familiar with declarative pipeline syntax, so it might not be the best way to do it.

    Anyway, you need to add a script block :

    stage('Deploy') {
        steps {
            script {
                withCredentials([[
                    class: 'AmazonWebServicesCredentialsBinding',
                    credentialsId: awsCredentialId,
                    accessKeyVariable: 'AWS_ACCESS_KEY',
                    secretKeyVariable: 'AWS_SECRET_KEY'
                ]]) {
                        println 'Deploying....'
                        sh " npx serverless --no-aws-s3-accelerate --key $AWS_ACCESS_KEY --secret $AWS_SECRET_KEY"
    

    you can simply call the credentials like this.

     withCredentials([usernamePassword(credentialsId: "${jenkinsCredentialsId}", usernameVariable: 'USERNAME', passwordVariable: 'PASSWORD')]) {
                     // your command here
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.