วิธีใช้ข้อมูลรับรองหลายรายการใน withCredentials ใน Jenkins Pipeline

ฉันมีขั้นตอนต่อไปนี้ในไปป์ไลน์เจนกินส์ที่ประกาศของฉัน: ฉันสร้างสคริปต์ที่มาจากโฟลเดอร์ resources/ ของฉันโดยใช้ LibraryResource สคริปต์นี้มีข้อมูลรับรองสำหรับผู้ใช้ autobuild ของฉันและสำหรับผู้ใช้ admintest บางคน

stage('Build1') {
                steps {
                    node{
                        def script = libraryResource 'tests/test.sh'
                        writeFile file: 'script.sh', text: script
                        sh 'chmod +x script.sh'
                        withCredentials([usernamePassword(credentialsId: xxx, usernameVariable: 'AUTOBUILD_USER', passwordVariable: 'AUTOBUILD_PASSWD')]){
                            sh './script.sh "
                        }

                    }

                }   

มันใช้งานได้ดี ฉันสามารถใช้ผู้ใช้ autobuild ของฉันได้ ตอนนี้ฉันกำลังค้นหาวิธีที่ดีที่สุดในการรวมข้อมูลประจำตัวของผู้ใช้ admintest ของฉันด้วย ฉันต้อง 'ซ้อน' กับส่วน withCredentials ที่สองหรือไม่ หรือฉันจะเพิ่ม usernamePassword 'อาร์เรย์' อีกครั้งได้หรือไม่


person lvthillo    schedule 24.11.2017    source แหล่งที่มา


คำตอบ (2)


แน่นอนว่าคุณสามารถใช้บล็อก withCredentials หนึ่งบล็อกเพื่อกำหนดข้อมูลรับรองหลายรายการให้กับตัวแปรที่ต่างกันได้

withCredentials([
    usernamePassword(credentialsId: credsId1, usernameVariable: 'USER1', passwordVariable: 'PASS1'),
    usernamePassword(credentialsId: credsId2, usernameVariable: 'USER2', passwordVariable: 'PASS2')
]){
    //...
}
person Vitalii Vitrenko    schedule 24.11.2017
comment
จะทำอย่างไรกับ Groovy? - person Steve K; 04.12.2018
comment
@meshfields นี่คือ Groovy - person Vitalii Vitrenko; 04.03.2020
comment
@SteveK stage('stage 2') { ขั้นตอน{ withCredentials([ชื่อผู้ใช้รหัสผ่าน (ชื่อผู้ใช้: 'user_1', ตัวแปรรหัสผ่าน: 'password_1', credentialsId: 'id_1'), ชื่อผู้ใช้รหัสผ่าน (ชื่อผู้ใช้ตัวแปร: 'username_2', ตัวแปรรหัสผ่าน: 'password_2', รหัสประจำตัว: 'id_2')]){ sshagent (ข้อมูลประจำตัว: ['jenkins_ssh_user_key']) { sh } } } } - person Mohsen Abasi; 11.07.2021

นอกจากนี้คุณยังสามารถใช้สิ่งนี้กับ $class ได้

                    withCredentials([[
                      $class: 'AmazonWebServicesCredentialsBinding',
                      credentialsId: 'awsID',
                      accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                      secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'],

                    [$class: 'UsernamePasswordMultiBinding',
                      credentialsId: 'myID',
                      usernameVariable: 'USR',
                      passwordVariable: 'PWD']])
person Max    schedule 14.02.2019
comment
คลาสใดก็ได้สำหรับข้อความสตริง - person WhoAmI; 11.06.2020