我写过以下Jenkins文件:
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
'''
}
}
}
我可以在没有这一行的情况下运行此构建:
[$class: 'UsernamePasswordMultiBinding', credentialsId: git_creds, usernameVariable: 'G_USER', passwordVariable: 'G_PASS']
但是在添加这一行之后,当我运行构建时,我收到以下错误:
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用户名和密码,并在''''''''''''''''''代码就像我使用了神器凭证一样。
任何人都知道我为什么会收到此错误?我指定的凭据存在于Jenkins服务器上
答案 0 :(得分:11)
使用SSH凭据,您可以非常轻松地完成此任务。
首先,您需要在Jenkins实例上配置凭据,即告诉Jenkins它可以在哪里找到将用于向Git存储库进行身份验证的私钥。 比如配置可能如下所示:
我个人选择为jenkins
用户生成SSH(私有+公共)密钥,并将其添加到经典用户目录〜/ .ssh,我添加了jenkins
用户作为我的Git成员存储库协作者,但这取决于你。
有关Git帐户的SSH配置的更多信息,您可以查看how to generate a new SSH key或how to add a new SSH key to your Github account的Github文档。
下一步是实际使用管道中的凭据。这是一个简单的例子:
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命令保留凭证,这样就可以了。