4
votes

We have project on bitbucket jb_common with address bitbucket.org/company/jb_common I'm trying to run a container that will requareq package from another private repo bitbucket.org/company/jb_utils

Dockerfile:

FROM golang
# create a working directory
WORKDIR /app
# add source code
COPY . .

### ADD ssh keys for bitbucket
ARG ssh_prv_key
ARG ssh_pub_key
RUN apt-get update && apt-get install -y ca-certificates git-core ssh
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    echo "StrictHostKeyChecking no " > /root/.ssh/config && ls /root/.ssh/config
RUN echo "$ssh_prv_key" > /root/.ssh/id_rsa && \
    echo "$ssh_pub_key" > /root/.ssh/id_rsa.pub && \
    chmod 600 /root/.ssh/id_rsa && \
      chmod 600 /root/.ssh/id_rsa.pub
RUN git config --global url."[email protected]:".insteadOf "https://bitbucket.org/" && cat /root/.gitconfig

RUN cat /root/.ssh/id_rsa
RUN export GOPRIVATE=bitbucket.org/company/

RUN echo "${ssh_prv_key}"
RUN go get bitbucket.org/company/jb_utils

RUN cp -R .env.example .env && ls -la /app
#RUN go mod download
RUN go build -o main .
RUN cp -R /app/main /main

### Delete ssh credentials
RUN rm -rf /root/.ssh/

ENTRYPOINT [ "/main" ] 

and have bitbucket-pipelines.yml

image: python:3.7.4-alpine3.10

pipelines:
  branches:
    master:
      - step:
          services:
            - docker
          caches:
            - pip
          script:
            - echo $SSH_PRV_KEY
            - pip3 install awscli
            - IMAGE="$AWS_IMAGE_PATH/jb_common"
            - TAG=1.0.${BITBUCKET_BUILD_NUMBER}
            - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_IMAGE_PATH         
            - aws ecr list-images --repository-name "jb_common" --region $AWS_DEFAULT_REGION
            - docker build -t $IMAGE:$TAG --build-arg ssh_prv_key="$(echo $SSH_PRV_KEY)" --build-arg ssh_pub_key="$(echo $SSH_PUB_KEY)" .
            - docker push $IMAGE:$TAG

in pipeline I build image and push on ECR

I have already add repository variables on bitbucket with ssh private and public keys [https://i.stack.imgur.com/URAsV.png][1]

On local machine Docker image build successfull using command docker build -t jb_common --build-arg ssh_prv_key="$(cat ~/docker_key/id_rsa)" --build-arg ssh_pub_key="$(cat ~/docker_key/id_rsa.pub)" .

[https://i.stack.imgur.com/FZuNo.png][2]

But on bibucket have error:

go: bitbucket.org/compaany/[email protected]: reading https://api.bitbucket.org/2.0/repositories/company/jb_utils?fields=scm: 403 Forbidden
    server response: Access denied. You must have write or admin access.

This user with ssh keys have admin access on both private repo.

While debug my problem I add some steps inside bitbucket-pipelines.yml to assert that the variables are forwarded inside the container on bitbucket: echo $SSH_PRV_KEY at the result: [ https://i.stack.imgur.com/FjRof.png][1]

1
I know you disable host-key checks, but maybe adding ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts explicitly?colm.anseo
Please remember that the ssh key is not removed from the previous Docker layer, just because you did a RUN rm -rf /root/.ssh/. Use a multi layer docker file instead and you'll get around this.Johan Forssell

1 Answers

3
votes

RESOLVED!!! Pipelines does not currently support line breaks in environment variables, so base-64 encode the private key by running: base64 -w 0 < private_key Output result copy to bitbucket repository variables for your variables. And I edit my bitbucket-pipelines.yml to:

image: python:3.7.4-alpine3.10

pipelines:
  branches:
    master:
      - step:
          services:
            - docker
          caches:
            - pip
          script:
            - apk add --update coreutils
            - mkdir -p ~/.ssh
            - (umask  077 ; echo $SSH_PRV_KEY | base64 --decode > ~/.ssh/id_rsa)
            - pip3 install awscli
            - IMAGE="$AWS_IMAGE_PATH/jb_common"
            - TAG=1.0.${BITBUCKET_BUILD_NUMBER}
            - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_IMAGE_PATH         
            - aws ecr list-images --repository-name "jb_common" --region $AWS_DEFAULT_REGION
            - docker build -t $IMAGE:$TAG --build-arg ssh_prv_key="$(cat ~/.ssh/id_rsa)"  .
            - docker push $IMAGE:$TAG