Unable to apply a tag to branch from Bitbucket pipeline
Asked by Video Guy:

I have a self hosted runner running on Windows. I have a step in my pipeline to create a label and tag the current branch with that label.

I looked at long thread in this community that it should work. But it is not working.

The step is here:

```
- step:
? name: 'Tag the repo with build label'
? runs-on:
? ? - 'self.hosted'
? ? - 'windows'
? ? - 'aio'
? script:
? ? - echo "Tagging repo with label"
? ? if ( $Env:BITBUCKET_BRANCH -match "feature/*" )
? ? {
? ? ? ${BRANCH_PART} = "integration";
? ? }
? ? elseif ( $Env:BITBUCKET_BRANCH -match "release/*" )
? ? {
? ? ? ${BRANCH_PART} = "release";
? ? };
? ? ${RELEASE_CODENAME}="chicago";
? ? ${REPO_TAG} = "${BRANCH_PART}_${RELEASE_CODENAME}_$Env:BITBUCKET_BUILD_NUMBER";
?   git tag -am "Tagging for build $Env:BITBUCKET_BUILD_NUMBER" "${REPO_TAG}";
? ? git remote set-url origin "$Env:BITBUCKET_GIT_HTTP_ORIGIN";
? ? git push --dry-run;
? ? echo git push origin "${REPO_TAG}";

```

I understand the script doesn't look like Bash script. It is because Windows runners run with Powershell as the parent shell. All those script lines are proper powershell script code.

The error I am seeing is this:

bash: /dev/tty: No such device or address

error: failed to execute prompt script (exit code 1)

fatal: could not read Username for '[https://bitbucket.org](https://bitbucket.org/)': No such file or directory

git

push

origin

integration_chicago_33


Answers:
author: Patrik S
body: Hello [@Video Guy](/t5/user/viewprofilepage/user-id/551282) ,

Thank you for reaching out to Atlassian Community!

This error is happening because the environment variable BITBUCKET_GIT_HTTP_ORIGIN you are currently using just contains the URL of the origin (such as <http://bitbucket.org/>\<workspace\>/\<repo\> ) but it does not contain any credentials for the authentication, so PowerShell is probably opening a pop up for you to provide the username and app password, and when you close this window it's failing with the error you shared.

That said, if you want to push back to your repository when using windows runners, you will need to provide the credentials for authentication (bitbucket username and app password) when setting the URL for ***origin.***This can be done by creating environment variables either in the workspace or repository level, that contain these credentials. When creating those variables you can also mark them as [Secured](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#Secured-variables), so their value will be masked from the logs.

I went ahead and did some testing in my repository, and was able to get the tag push working with the following code :

```
- step:

        name: 'Push a tag to the repository'

        runs-on: 

         - 'self.hosted'

         - 'windows'

        script:

          - echo "Tagging repo with label";

            $Env:BRANCH_PART = "master";

            $Env:RELEASE_CODENAME="chicago";

            $Env:REPO_TAG = "$($Env:BRANCH_PART)_$($Env:RELEASE_CODENAME)_$($Env:BITBUCKET_BUILD_NUMBER)";

            git remote set-url origin "https://$($Env:BB_USERNAME):$($Env:BB_APP_PASSWORD)@bitbucket.org/$Env:BITBUCKET_WORKSPACE/$Env:BITBUCKET_REPO_SLUG.git";

            git tag -a "$Env:REPO_TAG" -m "Tagging for build $Env:BITBUCKET_BUILD_NUMBER";

            git push origin $Env:REPO_TAG;
```

You can see in the example that we are using two [custom variables](https://support.atlassian.com/bitbucket-cloud/docs/variables-and-secrets/#User-defined-variables), BB_USERNAME and BB_APP_PASSWORD, when configuring the repo's origin URL, and these variables were created at the repository level.

You can adapt the code above with your if/else statement and your own credentials and check how it goes.

Hope that helps! Let me know in case you have any questions.

Thank you, [@Video Guy](/t5/user/viewprofilepage/user-id/551282) !

Kinds regards,

Patrik S

comments: author: Video Guy
body: Then how is Bitbucket runner able to clone the workspace in the first place?

When I look at runner build step logs, this is what I see.

++++++++

+ $env:GIT_LFS_SKIP_SMUDGE = '1'; git clone --branch='feature/chicago' --depth 50 "https://**x-token-auth:$Env:REPOSITORY_OAUTH_ACCESS_TOKEN** [@Bitbucket](/t5/user/viewprofilepage/user-id/1253146).org/$env:BITBUCKET_REPO_FULL_NAME.git" $env:BUILD_DIR  
Cloning into 'C:\\Users\\cicdagent\\atlassian-bitbucket-pipelines-runner\\bin\\..\\builds\\567e7a5d-0d86-52fc-a792-943bf82212a1\\1656365456588\\build'...  
+ git reset --hard 37ee556d5888c8c5013811bc989511ec6436f6f6  
HEAD is now at 37ee556 Added Launch4J config file  
+ git config user.name bitbucket-pipelines  
+ git config user.email commits-noreply@bitbucket.org  
+ git config push.default current  
+ git remote set-url origin <https://bitbucket.org/$env:BITBUCKET_REPO_FULL_NAME.git>  
+ git reflog expire --expire=all --all

++++++++++++++++

The runner has baked the creds in when it cloned the repo. As I am doing a push from the same folder, why do I need to pass them again?

How can I make use of "x-token-auth:$Env:REPOSITORY_OAUTH_ACCESS_TOKEN" if I were to include it URL?

When I dumped environment vars, I don't see this var in the dump.


author: Patrik S
body: Hello [@Video Guy](/t5/user/viewprofilepage/user-id/551282) ,

The token variable is used by the runner's own scripts to clone the repository but is currently not available to be referenced in the user's pipeline script, so this is the reason you can't see this variable when you print all the env variables in your script.

I'm afraid that currently if you want to push back to the repository when using Windows Runners, you would need to manually set the credentials for authentication as the example I shared in my previous response.

However, I went ahead and opened a feature request to implement the functionality you are looking for :

* <https://jira.atlassian.com/browse/BCLOUD-22002>

I would suggest you to add your vote there, since this helps both developers and product managers to understand the interest. Also, make sure you add yourself as a watcher in case you want to receive first-hand updates from that ticket. Please note that all features are implemented with [this policy](https://confluence.atlassian.com/support/implementation-of-new-features-policy-201294576.html) in mind.

Thank you, [@Video Guy](/t5/user/viewprofilepage/user-id/551282) .

Kind regards,

Patrik S


Tags: bitbucket-cloud

bitbucket-pipelines

cloud

git