英文:
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全局凭据中的用户名和密码注入到环境变量USERNAME
和PASSWORDD
中,在sh
步骤的作用域内。
在你的script.sh
中,你可以使用USERNAME
和PASSWORD
。
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论