'Using single-quotes in Jenkins Declarative Pipeline for credentials doesn't pass the full secret text

Following the instructions here: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation

I wrote this code on the Jenkinsfile:

    (...) stage("publish"){
            steps{
                withCredentials([string(credentialsId: 'ECR_authenticate', variable: 'get_authenticate')]) {
                echo "pushing the image to AWS ECR"
                sh ('$get_authenticate') \
                 ... (rest of code)
                }

Getting me back an error : Unknown options: |,docker,login,--username,AWS,--password-stdin,77777790668.dkr.ecr.us-east-1.amazonaws.com

When using double quotes: "${get_authenticate}" the code works, however this will be insecure and against the documentation.

Anyone know how to solve this puzzle?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source