Hide password in Jenkinsfile: 在Jenkinsfile中隐藏密码

huangapple go评论56阅读模式
英文:

Hide password in Jenkinsfile

问题

My Jenkinsfile (Declarative Pipeline) content as follows,

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'git@github.com:org/repository.git'
            }
        }
        
        stage('Script') {
            steps {
                sh 'script.sh' 
            }
        }
    }
}

In my script.sh, I have username and password in the following format:

#!/bin/bash

USERNAME="user"
PASSWD="mypassword-here"

Through the Declarative pipeline (Jenkinsfile), I'm trying to hide the password which is in my script.sh file.

In my Jenkins Global credentials page, I've already created a kind as Username with password then updated my script.sh as below:

#!/bin/bash

USERNAME="user"
PASSWD="$MY_CREDS_PSW"

But it is not working. Can you tell me how to add my jenkins Global credentials created as Kind-ID in my script to hide the password?

英文:

My Jenkinsfile (Declarative Pipeline) content as follows,

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'git@github.com:org/repository.git'
            }
        }
        
        stage('Script') {
            steps {
                sh 'script.sh' 
            }
        }
	}	

In my script.sh i have username and password in below format

#!/bin/bash

USERNAME="user"
PASSWD="mypassword-here"

Through Declarative pipeline(Jenkinsfile), I'm trying to hide the password which is in my script.sh file.

In my jenkins Global credentials page, already created kind as Username with password then updated my script.sh as below

#!/bin/bash

USERNAME="user"
PASSWD="$MY_CREDS_PSW"

But it is not working. Can i know how do i add my jenkins Global credentials created as Kind-ID in my script to hide the password?

答案1

得分: 2

你可以利用withCredentials块。以下是你的Jenkinsfile的更新版本:

pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'git@github.com:org/repository.git'
            }
        }

        stage('Script') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'YOUR_CREDENTIALS_SpeciifcID', passwordVariable: 'PASSWORDD', usernameVariable: 'USERNAME')]) {
                    sh 'script.sh'
                }
            }
        }
    }
}

在上面的代码中,用你创建的Jenkins全局凭据的实际ID替换'YOUR_CREDENTIALS_SpecificId'withCredentials块允许你将Jenkins全局凭据中的用户名和密码注入到环境变量USERNAMEPASSWORDD中,在sh步骤的作用域内。

在你的script.sh中,你可以使用USERNAMEPASSWORD

英文:

you can make use of the withCredentials block. Here's an updated version of your Jenkinsfile:

    pipeline {
    agent any

    stages {
        stage('Checkout') {
            steps {
                git url: 'git@github.com:org/repository.git'
            }
        }

        stage('Script') {
            steps {
                withCredentials([usernamePassword(credentialsId: 'YOUR_CREDENTIALS_SpeciifcID', passwordVariable: 'PASSWORDD', usernameVariable: 'USERNAME')]) {
                    sh 'script.sh'
                }
            }
        }
    }
}

In the above code, replace 'YOUR_CREDENTIALS_SpecificId' with the actual ID of your Jenkins Global credentials that you created. The withCredentials block allows you to inject the username and password from your Jenkins Global credentials into the environment variables USERNAME and PASSWORDD within the scope of the sh step.

In your script.sh, you van add USERNAME PASSWORD

huangapple
  • 本文由 发表于 2023年6月6日 15:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412257.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定