如何将git凭证添加到构建中

如何将git凭证添加到构建中

本文介绍了如何将git凭证添加到构建中,以便能够在shell代码中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了下面的Jenkinsfile:

  node(master){
def artifactory_creds =' XXXXXXX'
def git_creds ='XXXXXXX'
java = docker.image('openjdk:8-jdk')
java.pull()
java.inside( - u root用户的密码= 99999999){
withCredentials([//使用Jenkins凭证ID artifactory
[$ class:'UsernamePasswordMultiBinding',credentialsId:artifactory_creds,usernameVariable:'A_USER',passwordVariable:' A_PASS'],
[$ class:'UsernamePasswordMultiBinding',credentialsId:git_creds,usernameVariable:'G_USER',passwordVariable:'G_PASS'] //使用Jenkins凭证ID为git
]){
sh'''
cd $ PWD&& git clone $ {G_USER}:$ {G_PASS}@github.com/ganoti/Product-Android。
NDK_VER =r12b
SDK_VER =r24.4.1
export GRADLE_USER_HOME = $ PWD / Product-Android / .gradle
export PATH = $ PATH:$ GRADLE_USER_HOME:$ GRADLE_USER_HOME / android-ndk- $ NDK_VER
#apt-get update&& apt-get install gcc-multilib lib32z1 make file -y $ b $ if if [! -d$ GRADLE_USER_HOME / android-sdk-linux];那么
if [! -f$ GRADLE_USER_HOME / android-sdk_ $ SDK_VER-linux.tgz];然后
curl -o$ GRADLE_USER_HOME / android-sdk_ $ SDK_VER-linux.tgzhttps://dl.google.com/android/android-sdk_$SDK_VER-linux.tgz
cd $ GRADLE_USER_HOME &安培;&安培; tar -xvzf android-sdk_ $ SDK_VER-linux.tgz
fi
fi
if [! -d$ GRADLE_USER_HOME / android-ndk- $ NDK_VER];那么
if [! -f$ GRADLE_USER_HOME / android-ndk_ $ SDK_VER-linux.tgz];然后#检查sdk tarball是否存在于系统
curl -o$ GRADLE_USER_HOME / android-ndk- $ NDK_VER-linux-x86_64.ziphttps://dl.google.com/android/repository/android- ndk- $ NDK_VER-linux-x86_64.zip
cd $ GRADLE_USER_HOME&& unzip -o android-ndk- $ NDK_VER-linux-x86_64.zip
fi
fi


#下载所需的SDK工具
#echo y| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 2#Android SDK Tools,revision 25.2.2 rc1
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 3#Android SDK Platform-tools,revision 24.0.2
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 4#Android SDK Build-tools,revision 24.0.2
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 6#Android SDK Build-tools,revision 24
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 7#Android SDK Build-tools,revision 23.0.3
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 8#Android SDK Build-tools,revision 23.0.2
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 30#SDK平台Android 7.0,API 24,修订版2
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 53#Android TV Intel x86 Atom系统映像,Android API 24版本6
#echoy| $ Android SDK sdk -u -a -t 54#Android Wear ARM EABI v7a系统映像,Android API 24修订版1
#echoy| $ Android系统映像,Android API 24,修订版1
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 55# $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 57#ARM EABI v7a系统映像,Android API 24版本6
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 58#Intel x86 Atom_64系统映像,Android API 24版本6
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 59#Intel x86 Atom系统映像,Android API 24版本6
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 153#Android Support Repository,revision 36
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 160#Google Play services,revision 32
#echoy| $ GRADLE_USER_HOME / android-sdk-linux / tools / android update sdk -u -a -t 161#Google Repository,revision 32
#从Artifactory下载约束布局文件
curl -u $ {A_USER }:$ {A_PASS} -o$ GRADLE_USER_HOME/m2repository.tar.gz https://artifactory.company.net/android-tmp/m2repository.tar.gz
tar -xzf $ GRADLE_USER_HOME / m2repository.tar .gz -C $ GRADLE_USER_HOME / android-sdk-linux / extras /
#cd $ PWD&& ./gradlew -DBUILD_FLAVOR = staging -DUSE_OLD_BUILD_PROCESS = false -DCORE_BRANCH = NONE -DVERSION_NAME = 4.1.10 -DAutomation_Scenario_Tag_To_Run = short_sanity -DUSE_BUNDLE_NDK = true -DIS_X86_COMPATIBLE = false -D Automation_Device = Nexus_7 -DBUILD_TYPE =调试-DGIT_BRANCH =原产地/开发-DAutomation_Run_Config = autocad_appium -DANDROID_VIEWS_BRANCH = clean assemblestagingDebug
ls -l $ PWD
ls -l $ PWD / .gradle
'''
}

}
}

我可以在没有这行的情况下运行这个构建:

  [$ class:'UsernamePasswordMultiBinding',credentialsId:git_creds,usernameVariable:'G_USER',passwordVariable:'G_PASS'] 

但是在添加这行后,当我运行构建时,出现以下错误:

  org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException:凭证'a378e16a-3d20-4465-aef1-b2bd233f15b6'是o f键入'用户名和私钥',其中'com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials'是预期的

我试图绑定git用户名和密码,并在'''sh'''代码中使用它们,就像我使用artifactory凭证一样。



任何人知道为什么我得到这个错误?我指定的凭证存在于Jenkins服务器上

解决方案

使用SSH凭证您可以轻松完成。



为Jenkins配置SSH凭证



首先,您需要在Jenkins实例上配置凭证,即告诉Jenkins它可以在哪里找到私有密钥将用于向您的Git存储库进行身份验证。
如配置可能如下所示:





I personnaly选择为 jenkins 用户生成SSH(私人+公共)密钥,并将其添加到经典用户目录〜/ .ssh中,然后添加 jenkins 用户作为我的Git存储库协作者的成员,但这取决于您。
有关您的Git帐户的SSH配置的更多信息,您可以检查Github文档以了解。



在您的管道中使用SSH凭证



下一步是在您的管道中实际使用凭据。这是一个简单的例子:

$ p $ node(master){
stage'Checkout'
git url:ssh://[email protected]:port / git-project.git,
credentialsId:'jenkins_ssh_key',
分支:master

/ /您的Groovy的其余部分在这里...

stage'使用Git'
//使用Git仓库做任何你喜欢的事情
sh'git add -A&& amp ; git commit -m更新代码&& git push origin master'
}

在结账时声明时,凭证应保存为所有Git命令,你将在你的管道中进一步执行,所以应该这样做。


I've wrote the following Jenkinsfile:

node("master") {
 def artifactory_creds = 'XXXXXXX'
 def git_creds = 'XXXXXXX'
  java = docker.image('openjdk:8-jdk')
  java.pull()
  java.inside("-u root --ulimit core=99999999") {
      withCredentials([ // Use Jenkins credentials ID of artifactory
        [$class: 'UsernamePasswordMultiBinding', credentialsId: artifactory_creds, usernameVariable: 'A_USER', passwordVariable: 'A_PASS'],
        [$class: 'UsernamePasswordMultiBinding', credentialsId: git_creds, usernameVariable: 'G_USER', passwordVariable: 'G_PASS'] // Use Jenkins credentials ID of git
        ]) {
        sh '''
        cd $PWD && git clone ${G_USER}:${G_PASS}@github.com/ganoti/Product-Android .
            NDK_VER="r12b"
            SDK_VER="r24.4.1"
            export GRADLE_USER_HOME=$PWD/Product-Android/.gradle
            export PATH=$PATH:$GRADLE_USER_HOME:$GRADLE_USER_HOME/android-ndk-$NDK_VER
        #    apt-get update && apt-get install gcc-multilib lib32z1 make file -y
            if [ ! -d "$GRADLE_USER_HOME/android-sdk-linux" ]; then
                if [ ! -f "$GRADLE_USER_HOME/android-sdk_$SDK_VER-linux.tgz" ]; then
                    curl -o "$GRADLE_USER_HOME/android-sdk_$SDK_VER-linux.tgz" https://dl.google.com/android/android-sdk_$SDK_VER-linux.tgz
                    cd $GRADLE_USER_HOME && tar -xvzf android-sdk_$SDK_VER-linux.tgz
                fi
            fi
            if [ ! -d "$GRADLE_USER_HOME/android-ndk-$NDK_VER" ]; then
                if [ ! -f "$GRADLE_USER_HOME/android-ndk_$SDK_VER-linux.tgz" ]; then # Checks if the sdk tarball exists on system
                    curl -o "$GRADLE_USER_HOME/android-ndk-$NDK_VER-linux-x86_64.zip" https://dl.google.com/android/repository/android-ndk-$NDK_VER-linux-x86_64.zip
                    cd $GRADLE_USER_HOME && unzip -o android-ndk-$NDK_VER-linux-x86_64.zip
                fi
            fi


        # Downloads the required SDK tools
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 2 # Android SDK Tools, revision 25.2.2 rc1
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 3 # Android SDK Platform-tools, revision 24.0.2
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 4 # Android SDK Build-tools, revision 24.0.2
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 6 # Android SDK Build-tools, revision 24
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 7 # Android SDK Build-tools, revision 23.0.3
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 8 # Android SDK Build-tools, revision 23.0.2
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 30 # SDK Platform Android 7.0, API 24, revision 2
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 53 # Android TV Intel x86 Atom System Image, Android API 24, revision 6
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 54 # Android Wear ARM EABI v7a System Image, Android API 24, revision 1
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 55 # Android Wear Intel x86 Atom System Image, Android API 24, revision 1
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 57 # ARM EABI v7a System Image, Android API 24, revision 6
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 58 # Intel x86 Atom_64 System Image, Android API 24, revision 6
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 59 # Intel x86 Atom System Image, Android API 24, revision 6
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 153 # Android Support Repository, revision 36
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 160 # Google Play services, revision 32
        #    echo "y" | $GRADLE_USER_HOME/android-sdk-linux/tools/android update sdk -u -a -t 161 # Google Repository, revision 32
        # Downloads the constraint-layouts files from Artifactory
        curl -u ${A_USER}:${A_PASS} -o "$GRADLE_USER_HOME"/m2repository.tar.gz https://artifactory.company.net/android-tmp/m2repository.tar.gz
        tar -xzf $GRADLE_USER_HOME/m2repository.tar.gz -C $GRADLE_USER_HOME/android-sdk-linux/extras/
        #cd $PWD && ./gradlew -DBUILD_FLAVOR=staging -DUSE_OLD_BUILD_PROCESS=false -DCORE_BRANCH=NONE -DVERSION_NAME=4.1.10 -DAutomation_Scenario_Tag_To_Run=short_sanity -DUSE_BUNDLE_NDK=true -DIS_X86_COMPATIBLE=false -D    Automation_Device=Nexus_7 -DBUILD_TYPE=Debug -DGIT_BRANCH=origin/develop -DAutomation_Run_Config=autocad_appium -DANDROID_VIEWS_BRANCH= clean assemblestagingDebug
        ls -l $PWD
        ls -l $PWD/.gradle
      '''
        }

    }
}

I'm able to run this build without this line:

[$class: 'UsernamePasswordMultiBinding', credentialsId: git_creds, usernameVariable: 'G_USER', passwordVariable: 'G_PASS']

But after adding this line, when I run the build, I get the following error:

org.jenkinsci.plugins.credentialsbinding.impl.CredentialNotFoundException: Credentials 'a378e16a-3d20-4465-aef1-b2bd233f15b6' is of type 'SSH Username with private key' where 'com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials' was expected

I'm trying to bind git username and password and use them within the '''sh''' code just like I used artifactory credentials.

Anyone knows why I'm getting this error? The credentials I specified exist on the Jenkins server

解决方案

Using SSH credentials you can do that pretty easily.

Configure SSH credentials for Jenkins

First, you need to configure credentials on your Jenkins instance, i.e. tell Jenkins where it can find the private key that will be used to authenticate to your Git repository.Such as configuration could look like this :

I personnaly chose to generate SSH (private + public) keys for the jenkins user and add it to the classic user directory ~/.ssh and I added jenkins user as a member of my Git repository collaborators, but that's up to you.For more information about SSH configuration for your Git account, you can check Github doc for how to generate a new SSH key or how to add a new SSH key to your Github account.

Use SSH credentials in your pipeline

Next step is to actually use the credentials in your pipeline. Here is a simple example :

node("master") {
  stage 'Checkout'
  git url: "ssh://[email protected]:port/git-project.git",
     credentialsId: 'jenkins_ssh_key',
     branch: master

  // The rest of your Groovy here...

  stage 'Use Git'
  // Do anything you like with your Git repo
  sh 'git add -A && git commit -m "Update code" && git push origin master'
}

When declared at checkout time, credentials should be kept for all Git commands you will further execute in your pipeline, so that should do the trick.

这篇关于如何将git凭证添加到构建中,以便能够在shell代码中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 06:46