git - 使用来自 Jenkins 的凭据在 shell 脚本中进行 git clone
问题描述
我正在尝试使用存储在 Jenkins 中的凭据克隆服务器中的存储库,并且我正在对该存储库执行某些操作。
pipeline {
options {
skipDefaultCheckout()
timestamps()
}
parameters {
string(name: 'FILENAME', defaultValue: 'tmp', description: 'Enter the file name that needs to be copied')
string(name: 'DB_NAME', defaultValue: 'tmp_db', description: 'Enter the database name that needs to be created')
string(name: 'VERSION', defaultValue: '1', description: 'Enter the DB name version')
choice(name: 'RUN', choices: 'Migrate Data', description: 'Data migration')
}
agent {
node { label 'myserver' }
}
triggers {
pollSCM('H/5 * * * *')
}
stages {
stage('Clean & Clone') {
steps {
cleanWs()
git(branch: 'jenkinsfilr-branch',
credentialsId: 'lsdeploy-github',
url: 'https://github.com/xyz')
}
}
stage('Run the shell script On-prem'){
steps {
git(branch: 'progress',
credentialsId: 'lsdeploy-github',
url: 'https://github.com/abc')
}
configFileProvider([configFile(fileId: 'env-on-prem', targetLocation: '.env')]) {
sh '''
bash -x ./ops/database-migration/migrate.sh ${FILENAME} ${DB_NAME} ${VERSION}
'''
}
}
}
}
}
migrate.sh 包含对abc
文件夹执行的操作(从https://github.com/abc克隆),而 migrate.sh 驻留在xyz
repo 中(从https://github.com/xyz克隆)。在这种情况下,由于 abc repo 是最新克隆的,所以 jenkins 会抛出找不到 migrate.sh 脚本的错误。有没有办法避免这个错误?我尝试git clone --branch progress https://github.com/abc
在 migrate.sh 脚本中执行,但它要求我提供凭据。我尝试了另一种方式,以便我可以将凭据存储在 Jenkins 中并克隆存储库。有什么帮助吗?
解决方案
感谢@shane Bishop,这是最终奏效的解决方案
pipeline {
options {
skipDefaultCheckout()
timestamps()
}
parameters {
string(name: 'FILENAME', defaultValue: 'tmp', description: 'Enter the file name that needs to be copied')
string(name: 'DB_NAME', defaultValue: 'tmp_db', description: 'Enter the database name that needs to be created')
string(name: 'VERSION', defaultValue: '1', description: 'Enter the DB name version')
choice(name: 'RUN', choices: 'Migrate Data', description: 'Data migration')
}
agent {
node { label 'myserver' }
}
triggers {
pollSCM('H/5 * * * *')
}
stages {
stage('Clean & Clone') {
steps {
cleanWs()
git(branch: 'jenkinsfilr-branch',
credentialsId: 'lsdeploy-github',
url: 'https://github.com/xyz')
}
}
stage('Run the shell script On-prem'){
steps {
sh 'cp ./ops/database-migration/on-prem/migrate.sh ./'
git(branch: 'progress',
credentialsId: 'lsdeploy-github',
url: 'https://github.com/abc')
}
configFileProvider([configFile(fileId: 'env-on-prem', targetLocation: '.env')]) {
sh '''
bash -x migrate.sh ${FILENAME} ${DB_NAME} ${VERSION}
'''
}
}
}
}
}
推荐阅读
- python - 如何通过 python http 请求发送一个大的 JSON 文件
- vue.js - Vuex /状态管理不适用于具有路由历史记录的Nuxt
- amazon-web-services - 在使用云形成创建堆栈时加密 EC2 的根卷
- azure - Kibana 无法连接 ElasticSearch (Azure)
- r - 将一个数据帧中的键值转换为另一个数据帧的列
- python - dateutil 解析器:如何解析用空格分隔的时间?
- python - 使用 Pandas 创建日期范围系列
- python - PYSPARK:-在数据框中爆炸数组而不丢失空值:'DataFrame'对象没有属性'_get_object_id'
- c# - WebBrowser 控件中的“预期功能”
- css - 无法使用“资产”功能包含 CSS