skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. Is there a way to push this artifact directly into the root of the
    main branch of my Azure repo?

    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 the version.txt file and then commit it to push into the repo. See Run Git commands in a script

    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

    However, 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 pushes

    The final YAML for your reference:

    trigger:
    - main
    
    variables:
     system.debug : true
     major: '1'
     minor: '0'
     patch: $[counter(variables['minor'], 1)]
    
    pool:
      vmImage: ubuntu-latest
    
    steps:
      - checkout: self
        persistCredentials: true
      - task: Bash@3
        inputs:
          targetType: 'inline'
          script: |
            sudo echo "$(major).$(minor).$(patch)" > version.txt
            cat version.txt
            git config --global user.email "[email protected]"
            git config --global user.name "Your name"
            git status
            git checkout -b main
            git add --all
            git status
            git commit -m "Push version.txt to repo and [skip ci]"
            git push origin main
            git status
          workingDirectory: '$(Build.SourcesDirectory)'
    

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search