步骤及解释
设置git用户名
git config --global user.email "useremail"
git config --global user.name "username"
获取branch
$branch = "$(Build.SourceBranch)" -replace "refs/heads/"
"$(Build.SourceBranch)"
:这是一个环境变量,它包含了触发当前构建的分支的完整引用路径。例如,如果构建是由 main
分支触发的,那么这个变量的值可能是 refs/heads/main
。
-replace "refs/heads/"
:这是PowerShell中的一个操作符,用于替换字符串中的指定模式。在这里,它用于从 $(Build.SourceBranch)
的值中移除 refs/heads/
部分,只留下分支的名称。
示例
假设 $(Build.SourceBranch)
的值是 refs/heads/feature/new-feature
,那么执行这行命令后:
- 原始值:
refs/heads/feature/new-feature
- 处理后的值:
feature/new-feature
修改并push
git checkout -b $branch
Write-Host "Adding changes to staging area..."
git -C "$(Build.SourcesDirectory)" add **/package.json
Write-Host "Committing changes..."
git -C "$(Build.SourcesDirectory)" commit -m "your change"
Write-Host "Pushing changes to remote repository..."
git -C "$(Build.SourcesDirectory)" -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $branch
Write-Host "Done."
整体
- powershell: |
git config --global user.email "email"
git config --global user.name "name"
$branch = "$(Build.SourceBranch)" -replace "refs/heads/"
Write-Host "this is the $branch"
git checkout -b $branch
Write-Host "Adding changes to staging area..."
git -C "$(Build.SourcesDirectory)" add **/package.json
Write-Host "Committing changes..."
git -C "$(Build.SourcesDirectory)" commit -m "your changes"
Write-Host "Pushing changes to remote repository..."
git -C "$(Build.SourcesDirectory)" -c http.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" push --set-upstream origin $branch
Write-Host "Done."
workingDirectory: "$(Build.SourcesDirectory)"
failOnStderr: false
displayName: "Update Version number to Repo"
git -C 在某路径下执行
git -c 设置变量