Docker,从 bitbucket 私有(private)仓库中获取

标签 docker go bitbucket ssh-keys bitbucket-pipelines

我们在 bitbucket jb_common 上有项目,地址为 bitbucket.org/company/jb_common
我正在尝试运行一个容器,该容器将从另一个私有(private)仓库 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."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 上添加了存储库变量
[/image/URAsV.png][1]
在本地机器上使用命令成功构建 Docker 镜像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)" .[/image/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 key 的用户对两个私有(private)仓库都有管理员访问权限。
在调试我的问题时,我在 bitbucket-pipelines.yml 中添加了一些步骤以断言变量在 bitbucket 上的容器内转发:echo $SSH_PRV_KEY结果:
[ /image/FjRof.png][1]

最佳答案

解决!!!
管道当前不支持环境变量中的换行符,因此通过运行 base-64 对私钥进行编码:base64 -w 0 < private_key将结果副本输出到您的变量的 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

关于Docker,从 bitbucket 私有(private)仓库中获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67337522/

相关文章:

git - bitbucket 上的意外 merge

jenkins 中的 Git LFS 设置

python - docker python 图像中的 Git 存储库

validation - Kubernetes client-go 库是否包含用于验证 json/yaml 文件的功能?

go - 如何在 golang 中记录 http 服务器错误?

go - 运行 "go get github.com/libp2p/go-libp2p"导致错误消息

git - Phabricator 钩子(Hook)和 Bitbucket

docker - Nginx 到 Docker 容器的动态代理

spring - Keycloak:从内部 docker 容器运行时 token 颁发者无效

docker - libcontainer,runc和nsenter Bootstrap