首页 > 解决方案 > Bash 脚本在本地成功执行,但不在 Azure Devops 管道中

问题描述

我有一个在本地环境中成功执行的 bash 脚本:

#!/bin/bash
#build changelog

version="Version: "
current_date=`date`
cat <(printf " \n") <(echo $version $env:BUILD_BUILDNUMBER $current_date) <(tail -n+6 pr-changelog.md) ./CHANGELOG.md > output 
mv output ./CHANGELOG.md

但是,在我的 Azure Devops 管道中使用 Bash 构建包时,虽然脚本成功,但不会引发任何错误并且管道完成,脚本中的更改不会发生。

该脚本将行从一个文件复制到CHANGELOG.md以及其他一些小东西。在本地复制文本,但运行管道后,CHANGELOG.md我的分支中的文件没有更改。

我可以期望这行得通吗?如果不行,我应该采取哪些进一步的步骤?

TIA

标签: bashazure-devops

解决方案


Bash 脚本在本地成功执行,但不在 Azure Devops 管道中

当我们在 Azure 管道上执行构建时,它会将源从 repo 签出到构建代理,并且下一个操作也在代理上完成。它不会直接影响repo,这样可以保护我们repo中源的安全。

这就是为什么脚本中的更改不会发生在您的分支中的原因。

为了解决这个问题,我们需要通过 git 命令将这个更改提交到 repo:git add git commitgit push

git config --global user.email "xxx@xyz.com"
git config --global user.name "Admin"

cd "$(System.DefaultWorkingDirectory)"

version="Version: "
current_date=`date`
cat <(printf " \n") <(echo $version $env:BUILD_BUILDNUMBER $current_date) <(tail -n+6 pr-changelog.md) ./CHANGELOG.md > output 
mv output ./CHANGELOG.md

git add CHANGELOG.md

git commit -m "copies lines to CHANGELOG.md"

git push https://<PAT>@dev.azure.com/YourOrganization/YourProject/_git/YourProject HEAD:master

我用 Azure 管道对其进行了测试,效果很好。


推荐阅读