Spring boot 如何在詹金斯步骤中设置秘密

Spring boot 如何在詹金斯步骤中设置秘密,spring-boot,maven,jenkins-pipeline,kubernetes-secrets,Spring Boot,Maven,Jenkins Pipeline,Kubernetes Secrets,我正在寻找仅在Jenkins步骤中注入机密的解决方案: application.properties: spring.datasource.username=mySecretValue spring.datasource.password=mySecretValue ... 当前状态: stage('Test') { agent { docker { image 'myregistry.com/maven:3-alpine'

我正在寻找仅在Jenkins步骤中注入机密的解决方案:

application.properties:

spring.datasource.username=mySecretValue
spring.datasource.password=mySecretValue
...
当前状态:

stage('Test') {
      agent {
          docker {
               image 'myregistry.com/maven:3-alpine'
                    reuseNode true
                }
       }
       steps {
                configFileProvider([configFile(fileId: 'maven-settings-my-services', variable: 'MAVEN_SETTINGS')]) {
                    sh 'mvn -s $MAVEN_SETTINGS verify'
                }
            }
...


谢谢

选项1)为该机密添加密码作业参数。但是作业必须手动运行,因为需要有人输入秘密

// write the secret to application.property at any stage that
// prior to test and deployment stage

sh "echo spring.datasource.password=${params.DB_PASSWORD} >> application.property"

选项2)将密码添加为Jenkins字符串文本凭据。但添加凭证需要Jenkins管理员访问权限,并且还需要考虑将来的更新

stage('test or deployment') {
   environment {
      DB_PASSWORD = credentials('<credential_id_of_the_secret>')
   }
   steps {
      sh "echo spring.datasource.password=${env.DB_PASSWORD} >> application.property" 
   }
}
阶段(“测试或部署”){
环境{
DB_PASSWORD=凭证(“”)
}
台阶{
sh“echo spring.datasource.password=${env.DB_password}>>application.property”
}
}

我做这件事的一种方法是用credentials插件变量逐个附加机密:

 echo 'Attach properties for tests to property file:'
withCredentials([string(credentialsId: 'DB_PW', variable: 'SECRET_ENV')]) {
                sh 'echo spring.mydatabase.password=${SECRET_ENV} >> ./src/main/resources/application.properties'
与“echo”不同,“sed”也是一个替换键的空值的选项,而不是将属性添加到文件末尾

第二种方法是附加完整的属性文件,而不是键/值对。属性文件包含测试所需的所有属性:

    echo 'Attach properties file for test runs:'   withCredentials([file(credentialsId: 'TEST_PROPERTIES', variable: 'APPLICATION_PROPERTIES')]) {    dir('$WORKSPACE') {
  sh 'sed s#'/src/main/resources/' application.properties > TEST_PROPERTIES'

在这两种情况下,机密都必须在运行后删除,否则可以在工作区文件夹下以明文形式查看。

非常感谢您的支持!我已经实现了选项2,但是对于日志中提示的每个参数null,我都会得到,比如“echo spring.datasource.password=null”。已安装凭据绑定插件。您可能知道原因吗?请更正,应使用
env.DB\u密码