You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bitbucket Pipeline中PowerShell脚本内Git命令无效问题求助

Git命令在Bitbucket Pipeline的PowerShell脚本中失效问题

本地运行PowerShell脚本时一切正常,但在Bitbucket Pipeline中执行该脚本时,其中的Git命令无法生效。该脚本通过Git命令获取提交中更新的文件列表,进而同步更新另一台服务器上的对应文件以实现自动化。

PowerShell脚本

$jamsBaseUrl = "https://jamsapi.energycap.com/jams"
$hdrs = @{}
$JamsUserName = "username here"
$JamsPassword = "password here"
$updatedJobs = git diff --name-only HEAD HEAD~1


#Log into Bitbucket
$headers=@{}
$headers.Add("Authorization", "Basic token here")

# Log into JAMS
$loginReq = @{
    "username"=$JamsUserName;
    "password"=$JamsPassword;
    "grant_type"=$null;
    "refresh_Token"=$null
}
try {
    Write-Host "Logging into JAMS..."
    Write-Host "This is the commit..." $BITBUCKET_COMMIT
    $loginBody = $loginReq|ConvertTo-Json
    $loginResult = Invoke-RestMethod -Method Post -Uri "$jamsBaseUrl/api/authentication/login" -ContentType "application/json" -Headers $hdrs -Body $loginBody 
    Write-Host "Logged in as $JamsUserName."
}
catch {
    throw "Unable to log into JAMS as user $JamsUserName : $_"
}

$token = $loginResult.access_token
$hdrs.Add("Authorization", "Bearer $token")

Foreach ($job in $updatedJobs){
    Write-Host $job
    $clientJobs = $job -split '/'
    $clientDir = $clientJobs[0]
    $jobName = $clientJobs[1]
    $source = Invoke-RestMethod -Uri "https://api.bitbucket.org/2.0/repositories/mycompany/testing/src/master/$clientDir/$jobName" -Method GET -Headers $headers
    
    $jobjson =@{
        "JobName" = $jobName;
        "source" = $source;
        "ParentFolderName" = "\Testing\$clientDir";
        "MethodName" = "PowerShell"
    }
    $body = $jobjson |ConvertTo-Json
    $updateResult = Invoke-RestMethod -Method PUT -Uri "$jamsBaseUrl/api/job" -Headers $hdrs -Body $body -ContentType "application/json"

    Write-Host $jobName
    Write-Host $updateResult
}

Bitbucket Pipeline YAML配置

image: mcr.microsoft.com/powershell:latest

pipelines:
  branches:
    master:
      - step:
          script:
            - pwsh ./JamsConnector.ps1
            - echo "This script runs only on commit to the master branch"

执行错误截图

Git命令未找到错误截图

问题原因及解决办法

原因

使用的mcr.microsoft.com/powershell:latest镜像默认未安装Git,执行git命令时会提示“git : 术语 'git' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称”。此外,Bitbucket Pipeline默认的浅克隆可能导致HEAD~1无法访问历史提交,进而引发git diff命令失败。

解决步骤

  1. 在Pipeline中安装Git
    修改YAML配置,在执行脚本前添加Git安装命令:
image: mcr.microsoft.com/powershell:latest

pipelines:
  branches:
    master:
      - step:
          script:
            - apt-get update && apt-get install -y git
            - pwsh ./JamsConnector.ps1
            - echo "This script runs only on commit to the master branch"
  1. 设置完整克隆仓库
    添加clone配置确保获取完整提交历史,避免HEAD~1无法访问:
image: mcr.microsoft.com/powershell:latest

pipelines:
  branches:
    master:
      - step:
          clone:
            depth: full
          script:
            - apt-get update && apt-get install -y git
            - pwsh ./JamsConnector.ps1
            - echo "This script runs only on commit to the master branch"
  1. 可选:验证Git安装
    可在安装后添加git --version命令确认Git正常可用,提前排查环境问题。

内容的提问来源于stack exchange,提问作者taraloca

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:38:30