Pushing back to a repository from within a pipe returns error Failed to connect to localhost port 29418
Platform Notice: Cloud Only - This article only applies to Atlassian products on the cloud platform.
Summary
When executing a git push command from within a pipe/custom pipe, and to the same repository where the Pipeline is running, an error similar to the following is returned :
1
fatal: unable to access 'http://bitbucket.org/<workspace>/<repository>/': Failed to connect to localhost port 29418: Connection refused
Environment
Bitbucket Pipelines on Bitbucket Cloud
Cause
Bitbucket Pipelines set up an internal proxy for git at the start of the build process so you don't have to manually configure authentication in case you want to push back to the repository where build is running. This can be confirmed in the Build Setup phase, which contains the following entry :
1
git config http.${BITBUCKET_GIT_HTTP_ORIGIN}.proxy http://localhost:29418/
And also if you run the command :
1
git config --list
Which, among other settings, should show a line similar to this :
1
2
3
[...]
http.http://bitbucket.org/WORKSPACE/REPOSITORY_SLUG.proxy=http://localhost:29418/
[...]
This is essentially saying to git that any request going to that URL will be redirected to the internal proxy.
The internal proxy is accessible from the Build container, where your script runs. Pipes run in a separate docker container, and the pipe container is not able to access the address http://localhost:29418, causing the connection failure returned in the logs.
Solution
Since the internal proxy is not accessible from within the pipe, you can unset the proxy as part of your pipes script, with the following command :
1
git config --unset http.${BITBUCKET_GIT_HTTP_ORIGIN}.proxy
This should remove the proxy config of your git environment in the build.
Please note that, as we don't have the proxy to handle the authentication now, you will need to set it manually as part of your code. The command depends on whether you want to use HTTPS or SSH, and needs to be executed as part of your pipe's script:
For HTTPS
1
git remote set-url origin https://$BITBUCKET_USERNAME:$BITBUCKET_APP_PASSWORD@bitbucket.org/$BITBUCKET_REPO_FULL_NAME
This considers you have variables provided either in the pipe config in the YML file or in your workspace/repository variables with $BITBUCKET_USERNAME and $BITBUCKET_APP_PASSWORD. The $BITBUCKET_REPO_FULL_NAME is already a default variable provided by bitbucket that contains the workspace/repo_slug.
For SSH
1
git remote set-url origin git@bitbucket.org/$BITBUCKET_REPO_FULL_NAME
This will by default use the SSH key configured in Repository Settings > SSH Keys (under Pipelines section), unless you have a different ssh key configured as part of your pipe's container.
Was this helpful?