I’m trying to create a file into my repository with the version of the current build, that gets updated automatically whenever there’s a push to the main. I’ve tried using
- task: Bash@3
inputs:
targetType: 'inline'
script: |
sudo echo "$(major).$(minor).$(patch)" > version.txt
cat version.txt
But, even if the cat command shows the correct content, the file is not getting created on the repo.
One colleage of mine suggested me using an artifact, and I’ve developed the following code:
steps:
- task: Bash@3
inputs:
targetType: 'inline'
script: |
sudo echo "$(major).$(minor).$(patch)" > version.txt
cat version.txt
- task: PublishPipelineArtifact@1
inputs:
publishLocation: filepath
targetPath: version.txt # path to the folder or file to publish
artifactName: version # name of the artifact to create
The artifact is correctly made and I can download it and see the correct version number. Is there a way to push this artifact directly into the root of the main branch of my Azure repo? Thanks in advance.
2
Answers
You would need to do git add and commit after creating the file (I presume you are using Git since you are talking about repository):
https://learn.microsoft.com/en-us/azure/devops/pipelines/scripts/git-commands?view=azure-devops&tabs=yaml
The artifact you are talking about is really just a concept, and not a tangible thing you can or should be committing anywhere. In reality when you are publishing a pipeline artifact you are really just storing a file, folder structure or a compressed package somewhere where it can be downloaded by another stage or pipeline. In your example, it’s just a file, so you’ll commit that file. You would, of course, need to checkout the main-branch if the pipeline checks out some other branch at the start of the run.
Jukkak is right. To push the artifact (
version.txt
in your scenario) directly into the root of the main branch of Azure repo, you need to run git commands to add theversion.txt
file and then commit it to push into the repo. See Run Git commands in a scriptHowever, according to your above description, the CI trigger is enabled in your pipeline on main branch, so that a new pipeline will be triggered whenever there’s a push to the main. That means it will trigger a new pipeline when the
version.txt
is pushed into the main branch of the Azure repo. To avoid this, we can include[skip ci]
in the message of any of the commits that are part of a push, and Azure Pipelines will skip running CI for this push. See Skipping CI for individual pushesThe final YAML for your reference: