maven - 如何设置 Jenkins 管道以使用未提交到 GitHub 的 secrets.properties 文件
问题描述
我有一个基于 Springboot/Maven 的应用程序,它使用secrets.properties
文件来存储令牌。该文件包含一个键/值对作为IEX_CLOUD_TOKEN=MY_TOKEN
.
运行我的 Jenkins 管道后,我收到如下所示的错误。它失败是有道理的,因为secrets.properties
它不在 GitHub 中。如何设置管道以在应用程序需要时使用我的令牌?
我在 Jenkins 中设置了一个凭证并将其设置Kind
为Secret file
. 然后withCredentials
我在我的Jenkinsfile
. 但是,我仍然收到以下错误消息。
错误信息
context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [edu.bu.cs673.stockportfolio.StockportfolioApplication]; nested exception is java.io.FileNotFoundException: class path resource [secrets.properties] cannot be opened because it does not exist
詹金斯文件
pipeline {
agent any
triggers {
pollSCM '* * * * *' // 5 stars means poll the scm every minute
}
tools {
maven 'Maven 3.6.3'
}
options {
skipStagesAfterUnstable()
}
environment {
IexCloudApiKey=credentials('IEXCloud')
}
stages {
stage('Test') {
steps {
withCredentials([file(credentialsId: 'IEXCloud', variable: 'FILE')]) {
sh '''
cat $FILE > secrets.properties
mvn test
rm secrets.properties
'''
}
}
}
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
post {
success {
junit 'target/surefire-reports/**/*.xml'
}
}
}
}
}
解决方案
首先是关于您的管道的几条评论。
管道重复了很多步骤,因为您知道Maven中的生命周期?如果你打电话mvn compile
,然后mvn test
你会重复几个步骤,包括compile
更糟糕的使用mvn package
也重复几个步骤......包括test
所以compile
首先将其简化为mvn package
.
此外,您应该使用在工作区之外完成的凭据设置,如下所示:
withCredentials([file(credentialsId: 'secret', variable: 'FILE')]) {
dir('subdir') {
sh 'use $FILE'
}
}
推荐阅读
- tensorflow - 在项目中使用 go 模块时如何导入 tensorflow go 包?
- c++ - 如何将范围设置为随机库
- highcharts - 两条样条曲线比较
- sql - 如何使用 SQL 基于共享属性跨单独的实体 ID 创建单个 ID?
- c++ - 为什么找到 2 个不同大小的排序数组的中位数需要 O(log(min(n,m)))
- java - 允许从 1 台远程计算机(不是全部)访问 Tomcat 8.5.32
- boost-asio - 将 boost-asio 与来自 KDB+ 的基于文件描述符的套接字 api 集成
- php - 如何防止 Jquery 脚本在 ajax 调用中加载两次或将 JQuery 函数提交到 Ajax 调用页面
- python - 如何将 Django 数据库中的模板标签解释/呈现为 HTML
- python - 在 Jupyter Notebook 中运行时,Python 脚本中的 Matplotlib 绘图未显示在输出中