How can I push the codebase to bitbucket using Bash on Azure Devop ubuntu agent?

Viewed 46

Here is what I am trying to do:

  • I have a pipeline which runs on ubuntu image.
  • As a part of pipeline, I want to push commits to the bitbucket cloud.
  • I have written a simple bash script to do this but it seems like

Azure DevOps pipeline task

- job: Git_Merge
  steps:
    - task: Bash@3
      inputs:
        filePath: merge.sh
      env:
        SOURCE_BRANCH: ${{ parameters.source_branch }}
        TARGET_BRANCH: ${{ parameters.target_branch }}

merge.sh

#!/bin/bash

echo "SOURCE BRANCH IS $SOURCE_BRANCH"
echo "TARGET BRANCH IS $TARGET_BRANCH"

if [[ "$SOURCE_BRANCH" == "$TARGET_BRANCH" ]]; then
   echo "Source and Target branch names are the same so no merge is needed."
   exit
fi

echo "GIT CHECKOUT $TARGET_BRANCH"
git checkout "$TARGET_BRANCH"

echo "GIT STATUS"
git status

echo "GIT MERGE"
git merge "origin/$SOURCE_BRANCH" -m "Merge $SOURCE_BRANCH to $TARGET_BRANCH [skip ci]"

echo "GIT STATUS"
git status

echo "GIT PUSH"
git push origin

echo "GIT STATUS"
git status

Error

fatal: could not read Username for 'https://bitbucket.org': terminal prompts disabled

Error log

1 Answers

By default, when DevOps agent checks out your code, it doesn't store the credentials. That's why, when you do git push, git tries to ask you for username and password, but there's no interactive terminal to read it from.

To store the credentials, use checkout task explicitly, as the first step:

steps:
  - checkout: self
    persistCredentials: true

You might also need to configure user.email and user.name to be able to commit:

  - pwsh: |
      git config user.email ${{variables['user.email']}}
      git config user.name ${{variables['user.name']}}
    displayName: configure git credentials

After that, git commands should work as expected.

Related