jenkinsでトリガーされたシェルスクリプトにbitbucketリポジトリのユーザー名とパスワードを渡す方法

jenkinsでトリガーされたシェルスクリプトにbitbucketリポジトリのユーザー名とパスワードを渡す方法

Jenkinsの「SSH経由でファイルを送信またはコマンドを実行する」オプションを使用してシェルスクリプトをトリガーするJenkinsジョブを作成しようとしています。私のシェルスクリプトの主な部分は次のとおりです。

#!/bin/bash
BRANCH=$1
cd /vm/deployment
git clone https://[email protected]/myuser/proj.git
#updating the common property files
cd /vm/deployment/swcm-properties
git reset --hard HEAD
#git pull ${BRANCH}
git fetch && git checkout ${BRANCH}
git pull

私の問題は、レプリケーション操作を実行するためにリポジトリのパスワードとユーザー名を渡すことができないため、実行が失敗することです。

ユーザー名とパスワードをグローバル資格情報に設定するオプションを見つけて、以下を構成しました。

構成

サーバーに保存されている次のシェルスクリプトを実行しようとすると、次のエラーが発生します。

#!/bin/bash
git clone https://$uname:[email protected]/mysuer/myrepo.git

remote: Invalid username or password
fatal: Authentication failed for 'https://:@bitbucket.org/****/myrepo.git/'

Jenkinsを使用してユーザー名とパスワードを渡し、Bitbucketリポジトリでgit cloneをトリガーする最善の方法は何ですか?

ベストアンサー1

これがJenkinsを使って宣言的に実装した方法です。

ここで重要なのはを使用することですURLEncoder.encode

pipeline {
    environment {
        // bitbucketcredentials is stored at Jenkins Credentials Stored
        BITBUCKET_CREDENTIALS = credentials('bitbucketcredentials')
    }

    stages {
        stage('packaging git push para branch beta') {
            steps {  
                    script {
                        env.encodedPass=URLEncoder.encode(BITBUCKET_CREDENTIALS_PSW, "UTF-8")
                    }     
                    git push https://${BITBUCKET_CREDENTIALS_USR}:${encodedPass}@bitbucket.com/yourrepo.git branch

おすすめ記事