Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Docker,从bitbucket私人回购中获取_Docker_Go_Bitbucket_Ssh Keys_Bitbucket Pipelines - Fatal编程技术网

Docker,从bitbucket私人回购中获取

Docker,从bitbucket私人回购中获取,docker,go,bitbucket,ssh-keys,bitbucket-pipelines,Docker,Go,Bitbucket,Ssh Keys,Bitbucket Pipelines,我们有一个关于bitbucket jb_common的项目,地址是bitbucket.org/company/jb_common 我正在尝试运行一个容器,该容器将从另一个私有repo bitbucket.org/company/jb_utils请求QUAREQ包 Dockerfile: FROM golang # create a working directory WORKDIR /app # add source code COPY . . ### ADD ssh keys for bit

我们有一个关于bitbucket jb_common的项目,地址是bitbucket.org/company/jb_common 我正在尝试运行一个容器,该容器将从另一个私有repo bitbucket.org/company/jb_utils请求QUAREQ包

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."git@bitbucket.org:".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" ] 
并具有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
在管道中,我建立形象并推动ECR

我已经使用ssh私钥和公钥在bitbucket上添加了存储库变量 [https://i.stack.imgur.com/URAsV.png][1]

在本地计算机上使用命令成功生成Docker映像
docker build-t jb\u common--build arg ssh\u prv\u key=“$(cat~/docker\u key/id\u rsa)”--build arg ssh\u pub\u key=“$(cat~/docker\u key/id\u rsa.pub)”。

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

但在bibucket上有错误:

go: bitbucket.org/compaany/jb_utils@v0.1.2: 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.
此具有ssh密钥的用户在两个私有repo上都具有管理员访问权限

在调试我的问题时,我在bitbucket-pipelines.yml中添加了一些步骤,以断言变量在bitbucket上的容器内转发:
echo$SSH\u PRV\u KEY
,结果如下:
[ https://i.stack.imgur.com/FjRof.png][1]

比较我用来访问私人回购(基于的)的
Dockerfile
与您的:

ARG SSH_PRIVATE_KEY
RUN \
    mkdir -p ~/.ssh && \
    umask 0077 && \
    echo "${SSH_PRIVATE_KEY}" > ~/.ssh/id_rsa && \
    git config --global url."git@bitbucket.org:".insteadOf https://bitbucket.org/ && \
    ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
我使用的唯一没有出现在
Dockerfile
中的东西是
ssh-keyscan
行。

已解决!!! 管道当前不支持环境变量中的换行符,因此base-64通过运行以下命令对私钥进行编码:
base64-w 0
将结果副本输出到变量的bitbucket存储库变量。 我将bitbucket-pipelines.yml编辑为:

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

我知道您禁用了主机密钥检查,但可能会显式添加
ssh-keyscan-bitbucket.org>~/.ssh/known_-hosts