原文鏈接:https://blog.spiritling.cn/posts/6b626a8a/
環境變量
jenkinsfile 使用環境變量
代碼:
pipeline {
agent {
docker {
image 'spiritling/node:10.15.3'
}
}
stages {
stage('get') {
environment {
VERSION = sh(script: 'node script/auto-versioning.js', , returnStdout: true)
}
steps {
sh 'echo "VERSION: "$VERSION'
}
}
}
將 auto-versioning.js 執行后返回的文本或數字存入到 VERSION
環境變量中去
在 steps
中使用 $VERSION
來獲取環境變量
憑據
賬號密碼憑據管理
創建憑據,以下為例子:
類型:Username with password
范圍:全局
用戶名:root
密碼:rootxxxx
ID:BIRRARY_ID
描述:隨意填寫
在 jenkinsfile 中使用
pipeline {
agent {
docker {
image 'spiritling/node:10.15.3'
}
}
stages {
stage('get') {
steps {
withCredentials([usernamePassword(credentialsId: 'BIRRARY_ID', passwordVariable: 'password', usernameVariable: 'username')]) {
sh 'git remote set-url origin https://${username}:${password}@github.com/spiritling/blog.git'
}
sh 'echo "獲取憑據"'
}
}
}
可以在 jenkinsfile 文件的構建過程中獲取到 username 和 password 的憑據,並且可以在后續將其插入進去
加密文本憑據管理
創建憑據,以下為例子:
類型:Secret text
范圍:全局
Secret:rootxxxx
ID:BIRRARY_ID
描述:隨意填寫
在 jenkinsfile 中使用
pipeline {
agent {
docker {
image 'spiritling/node:10.15.3'
}
}
stages {
stage('get') {
steps {
withCredentials([string(credentialsId: 'ID:BIRRARY_ID', variable: 'secret')]) { //set SECRET with the credential content
sh 'echo -e "registry=https://npmjs.org/spiritling/\n_auth = ${secret}\nemail = spirit_ling_cn@163.com\nalways-auth = true\n$PATH" > .npmrc'
}
sh 'echo "獲取憑據"'
}
}
}
可以在 jenkinsfile 文件的構建過程中獲取到 secret 的憑據,並且可以在后續將其插入進去