如何将 git 凭据添加到构建中,以便能够在 shell 代码中使用? [英] How to add git credentials to the build so it would be able to be used within a shell code?

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

问题描述

我编写了以下 Jenkinsfile:

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

我正在尝试绑定 git 用户名和密码,并在 '''sh''' 代码中使用它们,就像我使用人工凭据一样.

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

有人知道我为什么会收到这个错误吗?我指定的凭据存在于 Jenkins 服务器上

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

推荐答案

使用 SSH 凭据您可以很容易地做到这一点.

Using SSH credentials you can do that pretty easily.

首先,您需要在 Jenkins 实例上配置凭据,即告诉 Jenkins 在哪里可以找到用于对您的 Git 存储库进行身份验证的私钥.比如配置可能是这样的:

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 :

我个人选择为 jenkins 用户生成 SSH (private + public) 密钥并将其添加到经典用户目录 ~/.ssh 中,我添加了 jenkins 用户作为我的 Git 存储库协作者的成员,但这取决于你.有关您的 Git 帐户的 SSH 配置的更多信息,您可以查看 Github 文档以获取 如何生成新的 SSH 密钥如何将新的 SSH 密钥添加到您的 Github 帐户.

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.

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

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

node("master") {
  stage 'Checkout'
  git url: "ssh://jenkins@your.git.server: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'
}

在结帐时声明时,应为您将在管道中进一步执行的所有 Git 命令保留凭据,这样就可以解决问题.

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 代码中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆