1、申请gitlab读写权限账号

2、在Jenkins的主机上,运行Jenkins程序的用户的home目录下,执行:

touch .git-credentials
vim .git-credentials
http://{用户名}:{密码}@{IP地址}

git config --global credential.helper store

查看.gitconfig文件,有

 

[credential] helper = store

这样,操作git就不需要再输入账号密码

3、

a、合并分支到主干

cat git_merge_branch_to_master.sh 
#!/bin/bash
RepositoryURL=$1
Branch=$2
ProjectName_tmp=${RepositoryURL##*/}
ProjectName=${ProjectName_tmp%\.*}
echo $ProjectName
echo $Branch

nowPath=`pwd`
rm -fr $nowPath/*
ls
git clone $RepositoryURL
cd $ProjectName
sleep 1
git checkout -b $Branch origin/$Branch
sleep 1
git checkout master
sleep 1
git merge $Branch
sleep 1
git push origin master

 

b、合并主干到分支

cat git_merge_master_to_branch.sh
#!/bin/bash
RepositoryURL=$1
Branch=$2
ProjectName_tmp=${RepositoryURL##*/}
ProjectName=${ProjectName_tmp%\.*}
echo $ProjectName
echo $Branch

nowPath=`pwd`
echo $nowPath
rm -fr $nowPath/*
ls
git clone -b $Branch $RepositoryURL
cd $ProjectName
sleep 1
git checkout -b master origin/master
sleep 1
git checkout $Branch
sleep 1
git merge master
sleep 1
pwd
#git add .
#git commit -m "merge master to branch"
#git push $RepositoryURL $ProjectName
ls
git push origin $Branch
mv ./* .. && cd .. && rm -fr $ProjectName

 

4、脚本使用,在Jenkins新建自由风格项目,在“构建”中,“增加构建步骤”,“execute shell ” 填写执行脚本的命令,例如:

source ~/.bashrc && bash /apps/sh/git_merge_branch_to_master.sh http://10.0.0.200/dxmall/pms-service.git 20170705_shop_pms_1.5.1

脚本执行有两个参数,第一个参数为项目的gitlab地址,第二个为分支的名字。