git-resource icon indicating copy to clipboard operation
git-resource copied to clipboard

Bug: tag_only: true should only push the specified tag, not all tags/branches

Open willhughes-au opened this issue 5 years ago • 2 comments

In a pipeline with:

      - put: git-repo
        params:
          repository: git-repo
          tag: git-tag/tag.txt 
          tag_only: true 

You use the following command:

push_tags() {
  git push --tags push-target $forceflag
}

Which can result in trying to push all tags, and also push branches.

Instead you should push just the specific tag. For example:

push_tags() {
  git push push-target $tag_name $forceflag
}

willhughes-au avatar Jan 20 '20 23:01 willhughes-au

The current code results in the following kinds of output in the log for your PUT task when you're operating off a tag that's not at the head of any branch:

To example.org:exampleco/repo.git
* [new tag]             my-tag-v1.2.3 -> my-tag-v1.2.3
! [rejected]            HEAD -> develop (non-fast-forward)

error: failed to push some refs to 'example.org:exampleco/repo.git'
hint: Updates were rejected because a pushed branch tip is behind its remote
hint: counterpart. Check out this branch and integrate the remote changes
hint: (e.g. 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

The work-around I'm now using is to do it manually:

- task: tag-repo 
  config: 
    platform: linux
    image_resource:
      type: docker-image
      source:
        repository: alpine/git
      
    inputs: 
      - name: git-repo 

    run:
      path: sh
      args: 
        - -c
        - | 
          set -eu

          echo 'Setting SSH configuration'
          mkdir -p ~/.ssh
          echo "((git-rsa-private-key))" > ~/.ssh/id_rsa 
          echo "Host *" > ~/.ssh/config
          echo "  AddKeysToAgent yes" >> ~/.ssh/config
          echo "  StrictHostKeyChecking no" >> ~/.ssh/config
          chmod -R 700 ~/.ssh

          echo 'Configuring Git'
          cd git-repo 
          git config --global user.email '[email protected]'
          git config --global user.name 'Concourse CI'

          tagname="my-tag-v1.2.3" # insert your tag here. 


          echo "Tagging git repo with tag: $tagname" 
          git tag -m "Tagging repo" $tagname 

          echo "Pushing tag" 
          git push origin $tagname 

willhughes-au avatar Jan 21 '20 00:01 willhughes-au

In the meantime, as a workaround for tagging and pushing just a single tag, clean_tags: true param could be used in the preceding get task.

stanislav-zaprudskiy avatar Aug 08 '22 16:08 stanislav-zaprudskiy