Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

azure devops - How to set a release pipeline variable to null

I have a .json config file in my application. Using the variables tab when managing the release in Azure DevOps, I can substitute values in that .json file. However, I am unable to explicitly set a value to null instead of string "null" using this method. Is there a way to do this? I need the resulting json to look like

"setting": null

instead of

"setting": "null"
question from:https://stackoverflow.com/questions/65942181/how-to-set-a-release-pipeline-variable-to-null

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you set the variable in Variables tab but don’t specify its value, its value is null. enter image description here

However, when referring to this variable in scripts, it will be parsed to null string and is not null value.

For example, we have a test.json with below content.

    "setting": "debug",
    "environment": "dev"
}

If we use the command line task with below content.

echo Hello world

cat $(System.DefaultWorkingDirectory)/_tt/test.json

echo ""
echo "now replace: "

sed "s/"debug"/$testvar/g" $(System.DefaultWorkingDirectory)/_tt/test.json

The output result is below. enter image description here

If you want to set it to null, just directly use null value with below content.

sed 's/"debug"/null/g' $(System.DefaultWorkingDirectory)/_tt/test.json

The output result is below. enter image description here

And usesed 's/"debug"/null/g' $(System.DefaultWorkingDirectory)/_tt/test.json >$(System.DefaultWorkingDirectory)/_tt/new_test.json will write output to new_test.json file.

See: How to Use sed to Find and Replace String in Files for details.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...