jenkins 在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}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个双鱼座的测开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值