カスタム コード チェックアウト コマンドを実装するための Jenkins パイプライン checkout() メソッドの代替

バックグラウンド

以前、ジェンキンスを使用して git コード ウェアハウスをチェックアウトするためのカスタム コマンドをサポートし、ユーザー パスワードを自動的に追加し、ユーザー パスワードを非表示にすることも要求するというリクエストを受け取りました。 jenkins によって生成されたメソッドの適用は流水线语法生成器あまりcheckout()良くありません

方法1。gitUsernamePassword凭据变量方式

gitUsernamePassword方法1では、git コマンドの実行時に自動的に ID を識別できますが、この方法では jenkins の git プラグインのバージョンが4.8.1それ以上である必要があり、最新バージョンの jenkinsgit:4.12.2プラグインには jenkins のバージョンが必要です2.332.4。プラグインは jenkins 上で直接アップグレードされます, 互換性がない可能性があります, そしてそれはパイプラインを使用できなくなる原因になります. jenkins プラグインをアップグレードする必要がある場合, それは不可能ではありません. jenkins で回復できない問題を避けるために、最初にディレクトリを$JENKINS_HOMEフォローしてください。plugins

例は次のとおりです。

withCredentials([gitUsernamePassword(credentialsId: 'my-credentials-id', gitToolName: 'git-tool')]) {
    
    
  sh 'git fetch --all'
}

方法 2、withCredentials は環境変数を使用してアカウント シークレットを手動で追加します

試行を繰り返した結果、最終的に、git checkout コマンドでアカウント シークレットを自動的に追加および非表示にする方法を実現しました。その中には、${CERT_ID}カスタム チェックcheckoutCmdアウト コマンド パラメーターであるカスタム資格情報も含まれます。

 withCredentials([usernamePassword(credentialsId: "${CERT_ID}", usernameVariable: 'username', passwordVariable: 'password')]) {
    
    
     //获取http或https开头的域名, 例如http://gitlab.com/project.git 截取为gitlab.com
    def domain=sh(script: ''' echo '${checkoutCmd}' | grep -Eo '(https?://)[^/]+' |head -n 1 | awk -F '://' '{print \$2}' ''', returnStdout: true).trim()
    //在域名中加上用户密码,组成user:[email protected]"这种格式
    passWithDomain="${username}:${password}@${domain}"
    //替换后检出命令
    checkoutCmd=checkoutCmd.replaceAll(domain,passWithDomain)
    //set +x关闭日志输出避免输出用户密码, 参考 https://www.jenkins.io/doc/pipeline/steps/credentials-binding 
    sh '''
      set +x
      ${checkoutCmd}
      '''
}

もともと、jenkins git プラグインのコード ロジックを分析して、より良い実装方法があるかどうかを確認したかったのですが、jenkins プラグイン 2 のバインディング アカウント シークレットのコード ロジックは次のgit:4.12.2とおりですauth.shワークスペースの下に生成されますが、ファイルがどのように使用されるかはまだわかりません

        @Override
        protected FilePath write(StandardUsernamePasswordCredentials credentials, FilePath workspace)
                throws IOException, InterruptedException {
    
    
            FilePath gitEcho;
              //Hard Coded platform dependent newLine
            if (this.unixNodeType) {
    
    
                gitEcho = workspace.createTempFile("auth", ".sh");
                // [#!/usr/bin/env sh] to be used if required, could have some corner cases
                gitEcho.write("case $1 in\n"
                        + "        Username*) echo " + this.userVariable
                        + "                ;;\n"
                        + "        Password*) echo " + this.passVariable
                        + "                ;;\n"
                        + "        esac\n", null);
                gitEcho.chmod(0500);
            } else {
    
    
                gitEcho = workspace.createTempFile("auth", ".bat");
                gitEcho.write("@ECHO OFF\r\n"
                        + "SET ARG=%~1\r\n"
                        + "IF %ARG:~0,8%==Username (ECHO " + this.userVariable + ")\r\n"
                        + "IF %ARG:~0,8%==Password (ECHO " + this.passVariable + ")", null);
            }
            return gitEcho;
        }

参考


  1. https://www.jenkins.io/blog/2021/07/27/git-credentials-binding-phase-1 ↩︎

  2. https://github.com/jenkinsci/git-plugin/tree/master/src/main/java ↩︎

おすすめ

転載: blog.csdn.net/qq_26545503/article/details/127568685