Jenkins 解決Git插件不兼容問題的方案

簡介

Jenkins 解決Git插件不兼容問題的方案,不少時候因爲項目打包需求的插件版本不一樣,致使jenkins加載插件失敗或者配置常常丟失。今天咱們主要解決Git插件不兼容或者版本問題。固然方案不少種,這裏咱們使用的是不依賴Git插件的方式。linux

 

服務器環境

主機系統:CentOS 7 i5 4核心 4GB內存git

服務器:Tomcat + Jenkinsshell

項目:Android打包bash

 

解決原理

jenkins參數化構建過程是有順序的,咱們在build的項目以前選擇Execute Shell來git clone 或者更新代碼。如下整個配置都是關於【構建選項】模塊的配置。服務器

步驟1:驗證用戶名和密碼ide

在linux bash中經過git clone 項目輸入本身的用戶名和密碼,這一步能夠讓linux系統將用戶名和密碼保存在本地。fetch

步驟2:使用腳本下載或者更新代碼ui

更新代碼的腳本以下:this

#!/bin/bash

echo 'Start GitClient  for using git to update the project to '`pwd`

declare  GITURL='http://[你的項目地址]' #如https://gitee.com/cn_lyjuan/BaseUtil-Android.git
declare git_cmd='/usr/local/git/bin/git'

echo 'GitServer Address : ' ${GITURL}

echo 'git rev-parse --is-inside-work-tree'

declare needUpdate=false

$git_cmd  rev-parse --is-inside-work-tree >/dev/null 2>&1
if [ $? -eq 0 ]
then
     needUpdate=true
else
     needUpdate=false
fi

echo 'needUpdate is ' ${needUpdate}

if [ $needUpdate != true ]
then
	
	echo 'git clone the remote project from '${GITURL}
	rm -rf .git
	echo 'git init '`pwd`
	$git_cmd init `pwd`
	echo '------------start fetching--------------------'
	echo 'git --version'
	$git_cmd --version
	echo 'git -c core.askpass=true fetch --tags --progress' ${GITURL} '+refs/heads/*:refs/remotes/origin/*'
	$git_cmd -c core.askpass=true fetch --tags --progress ${GITURL} +refs/heads/*:refs/remotes/origin/*
	echo 'git config remote.origin.url' ${GITURL}
    $git_cmd config remote.origin.url $GITUR 
	echo 'git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*'
	$git_cmd config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
	echo 'git config remote.origin.url' ${GITURL}
	$git_cmd config remote.origin.url ${GITURL}

	echo '--------fetching upstream changes from '${GITURL} '----------'
	echo 'git -c core.askpass=true fetch --tags --progress' ${GITURL} '+refs/heads/*:refs/remotes/origin/*'
	$git_cmd -c core.askpass=true fetch --tags --progress ${GITURL} +refs/heads/*:refs/remotes/origin/*
	
	echo 'git rev-parse refs/remotes/origin/master^{commit}'
	
	declare commitId=$( $git_cmd rev-parse refs/remotes/origin/master^{commit}  )
	echo $commitId
	
	$git_cmd rev-parse refs/remotes/origin/origin/master^{commit} >/dev/null 2>&1
	if [ $? -eq 0 ]
	then
	    commitId=$( $git_cmd rev-parse refs/remotes/origin/origin/master^{commit} )
	fi
	echo '---------Checkout out Revision ' ${commitId}'--------'
	
	echo 'git config core.sparsecheckout'
	$git_cmd config core.sparsecheckout
	echo 'git checkout -f ' ${commitId}
	$git_cmd checkout -f ${commitId}
	
	echo 'unset varibiant'

	unset commitId
 
else

	
	echo 'git config remote.origin.url' ${GITURL}	
	$git_cmd config remote.origin.url $GITURL

	echo 'git --version'
	$git_cmd --version

	echo 'git fetch --tags --progress' ${GITURL} '+refs/heads/*:refs/remotes/origin/*'
	$git_cmd  fetch --tags --progress ${GITURL} +refs/heads/*:refs/remotes/origin/*

	echo 'git rev-parse origin/master^{commit}'
	declare commitId=$( $git_cmd rev-parse origin/master^{commit} )

	echo '-------fetching upstream of Revision is ' ${commitId} '-----'

	echo 'git config core.sparsecheckout'
	$git_cmd config core.sparsecheckout

	echo 'git checkout -f' ${commitId}
	$git_cmd checkout -f $commitId

	echo 'git rev-list '${commitId}
	$git_cmd rev-list $commitId -1

	unset commitId

fi

$git_cmd log -1

unset GITURL
unset git_cmd
        

echo 'finishly execute this script'

步驟3:配置打包參數url

 

綜上:咱們能夠解決Git插件不兼容的問題。本人對shell語法掌握不夠熟練,以上腳本還有改造的潛能,若是有須要您能夠自行改造。

相關文章
相關標籤/搜索