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语法掌握不够熟练,以上脚本还有改造的潜能,若是有须要您能够自行改造。

相关文章
相关标签/搜索