Jenkins中使用pipeline进行git拉取和推送

步骤1:生成用户字符串

点击Pipeline Syntax

在这里插入图片描述

选择git:Gitcheckout:xxxxxx,然后选择下方-none-处已经添加的用户名跟密码,若未添加,则使用下方Add进行添加

在这里插入图片描述

在下方使用Generate Pipeline Script进行语法生成,如下:
在这里插入图片描述
生成的格式为:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx即为我们需要的用户字符串

步骤2:编写pipeline脚本

pipeline {
    agent any
    
    stages {
        stage('Hello') {
            steps {
                # 拉取xxxxxxxxxx仓库代码, 并拉取子仓库代码
                checkout scmGit(branches: [[name: '*/main']], extensions: [submodule(parentCredentials: true, recursiveSubmodules: true, reference: '')], userRemoteConfigs: [[credentialsId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', url: 'http://192.168.0.1:8080/xxx/xxxxxxxxxx']])

				# 使用credentialsId操作git, 与终端操作相同, 注意语句要在sh ''中执行
                withCredentials([
                    gitUsernamePassword(credentialsId: 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', gitToolName: 'Default')
                    ]){
                    sh """
                    git branch
                    git checkout main
                    touch test.txt
                    echo "test" >> test.txt
                    git add test.txt
                    git commit -m "test"
                    git push origin main
                    """
                }
                
            }
        }
    }
}

pipeline的使用案例可参考https://www.cnblogs.com/FRESHMANS/p/8184874.html


参考链接:

  • https://www.saoniuhuo.com/question/detail-2324217.html
### 如何在Jenkins Pipeline中集成Git Bash 为了实现自动化构建流程,许多团队采用版本控制系统(VCS),如Git来管理源码工作流[^1]。当涉及到具体操作,比如在Jenkins Pipeline中集成Git Bash时,则需遵循特定配置。 #### 配置环境变量 确保安装了必要的工具包,在Linux或macOS上通常预装了`git`命令行工具;对于Windows系统则可能需要单独下载并安装Git for Windows。这一步骤同样适用于准备运行OctoDNS这类Python应用所需的依赖项安装环境设置[^2],不过这里关注的是Git本身而非其他软件的具体部署方式。 #### 安装插件 进入Jenkins仪表板后,通过“Manage Jenkins -> Manage Plugins”,找到并安装支持Shell执行的插件,例如“Git plugin”。此过程类似于利用Go语言生态中的`go get`指令获第三方库的方式[^3],即借助平台提供的扩展机制引入外部功能模块。 #### 编写Pipeline脚本 下面给出一段简单的Groovy语法编写的声明式管道定义样例: ```groovy pipeline { agent any stages { stage('Checkout') { steps { git 'https://github.com/your-repo.git' } } stage('Build') { steps { script { bat ''' cd ${WORKSPACE} echo "Running custom bash commands..." git --version # Add more Git operations here as needed. ''' } } } // More stages... } post { always { cleanWs() } } } ``` 上述代码片段展示了如何在一个名为“Checkout”的阶段仓库最新变更,并于后续“Build”环节调用Git客户端执行基本查询命令。注意使用`bat`关键字包裹多行批处理语句以适应Windows下的Bash shell交互需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值