在pinline克隆代码的两种方式
jenkins搭建流水线从拉取代码开始其实是最正规的方式,但是如何拉取有多种方式
可以用jenkins的插件以钩子的形式,也可以在pipline脚本中实现,今天介绍两种 pipline方式实现拉取代码的方式。
1.pipline语法直接实现方式
1.1例子1
pipeline {
agent any
stages {
stage('Project1'){
steps{
cleanWs()
dir('project1') {
// Doing your project 1 stuff
git(url: 'https://github.com/xxx/proj1.git', branch: 'main')
}
}
}
stage('Project 2'){
steps{
dir('project2') {
// Doing your project 2 stuff
git(url: 'https://github.com/xxx/proj2.git', branch: 'dev')
}
}
}
stage('Something Else'){
steps{
sh 'ls -al'
}
}
}
}
1.2例子2
pipeline {
agent any
stages {
stage('Project1'){
steps{
cleanWs()
echo "Let's move proj 1 stuff to a sub dir"
sh '''
mkdir project1
shopt -s extglob dotglob
mv !(project1) project1
'''
}
}
stage('Project 2'){
steps{
dir('project2') {
// Doing your project 2 stuff
git(url: 'https://github.com/xxx/proj2.git', branch: 'main')
}
}
}
stage('Something Else'){
steps{
sh 'ls -al'
}
}
}
}
2.jenkins pinline 流水线中调用 shell脚本方式
这种方式需要服务器预先安装好了git
pipline中调用shell脚本
def CustomizeRepos = ''
def Baseline = 'false'
def VersionInfo=''
def testMessage=''
def Pr_CustomizeRepos = ""
pipeline {
agent {
label 'le-node'
}
parameters {
string(name: 'BRANCHNAME', defaultValue: 'develop', description: '代码分支名称')
}
environment {
JENKINS_NODE_COOKIE = 'dontKillMe'
project_path = '/automation/code/'
pipeline_git_tool = '/automation/scripts/build_gitclone.sh'
url = 'https://gitee.com/burebaobao/tscancode-master.git'
}
stage("下载平台代码") {
steps {
script {
echo "开始克隆代码"
sh "cd ${project_path}"
// 调用脚本
sh "${env.pipeline_git_tool} '${project_path}' '${BRANCHNAME}' '${url}'"
}
}
}
}
所调用的shell脚本
#!/bin/bash
#########################################
#代码clone脚本
#参数:
# path 代码存储路径
# branch 代码分支名
# url 地址
#########################################
#参数判断
if [ $# != 3 ]; then
echo "参数输入错误,输入必须包括path、Branch、url参数!"
exit -1
fi
path=$1
branch=$2
url=$3
echo "开始"
echo "切换路径到 $path"
cd ${path}
echo "克隆的代码分支为 ${branch}"
mcd="git clone -b ${branch} --single-branch https://gitee.com/burebaobao/tscancode-master.git"
git clone -b ${branch} --single-branch ${url}