Updating $PATH while running a bitbucket pipeline
Asked by Misha _HS_:

I'm working with hashicorp/terraform:latest image (which is very minimal) and need to install go. After installing go into /usr/local. As a next step we need to update $PATH so that we could run go commands. But the export PATH does not take and the shell can't find go executable which is in /usr/local/go/bin.

We can't quite figure out how to go about this. Help is greatly appreciated!  

\`\`\`  
image: hashicorp/terraform:latest  
pipelines:  
branches:  
dev:  
- step:  
name: Build \& deploy \[dev\]  
# image: golang  
deployment: Dev  
script:  
- apk add bash  
- apk add zip  
- source bitbucket-pipelines.sh  
- cd ${IMPORT_PATH}  
# Install go  
- wget <https://golang.org/dl/go1.17.2.linux-amd64.tar.gz>  
- tar -C /usr/local -xzf go1.17.2.linux-amd64.tar.gz  
- ls -la /usr/local/go/bin  
- export PATH=$PATH:/usr/local/go/bin  
- echo $PATH  
- echo $0 # show shell exec  
- go version # this fails with file not found  

\`\`\`


Answers:
author: Theodora Boudale
body: Hi [@Misha _HS_](/t5/user/viewprofilepage/user-id/2842580),

I have tried to reproduce this locally by running a Docker container with the image you are using:

```
docker run -it --entrypoint=/bin/sh hashicorp/terraform:latest
```

and then I executed the commands you have in your yml file (apart from lines 3 and 4 of the script)

The command **go version** also fails when debugging this locally.

If I try the installation in a different Docker container, e.g. atlassian/default-image:3 which is based on Ubuntu, the installation of go seems to succeed (both locally and in Pipelines).

It is possible that the file you are downloading is not the correct one for Alpine Linux or extra/different steps may be needed for the installation in Alpine. I'm afraid I don't know what exactly is causing the problem here, but it seems to be specific to the Linux distribution of the Docker image.

Kind regards,  
Theodora

comments: author: Misha _HS_
body: Oh, great, thank you Theodora. I will play with the image. It looks like Alpine does not like the compiled for amd64 go binary. I will update this ticket with what I find.


author: Jack Nolddor [Sweet Bananas]
body: Pre-compiled go binary seems not to be compatible with alpine.  

You can see an example of how to build go from source on alpine at this dockerfile  
<https://github.com/docker-library/golang/blob/67757c0feeb9867a54801ca378dcfc55b9db7e70/1.17/alpine3.15/Dockerfile>

Better option would be as suggested already by Atlassian team to use another image based on Debian / Ubuntu  

Regards


author: Theodora Boudale
body: Thanks for posting that example Jack!

[@Misha _HS_](/t5/user/viewprofilepage/user-id/2842580), I also found the following blog post, the solution suggested there is to install go with apk:

* <https://fukubaka0825.medium.com/how-to-resolve-the-trouble-occurred-when-i-install-go-into-the-alpine-image-3c1e84f2315a>

Kind regards,  
Theodora


author: Jack Nolddor [Sweet Bananas]
body: Hi there maybe you must add execution permissions to go binary?  

chmod a+x /usr/local/go/bin/go

comments: author: Misha _HS_
body: What I'm getting is file not found (not a permission issue)  

/opt/atlassian/pipelines/agent/tmp/shellScript5553201990805023401.sh: line 37: go: not found


Tags: bitbucket-cloud

bitbucket-pipelines

cloud