jenkins憑證使用

環境變量

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 = [email protected]\nalways-auth = true\n$PATH" > .npmrc'
        }
        sh 'echo "獲取憑據"'
    }
  }
}

可以在 jenkinsfile 文件的構建過程中獲取到 secret 的憑據,並且可以在後續將其插入進去

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章