猿问

Docker,从bitbucket私有存储库中获取

我们有一个关于bitbucket jb_common的项目,地址 bitbucket.org/company/jb_common 我正在尝试运行一个容器,该容器将从另一个私有存储库 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


宝慕林4294392
浏览 146回答 1
1回答

开满天机

解决!!!管道当前不支持环境变量中的换行符,因此 base-64 通过运行以下内容对私钥进行编码:将结果输出到变量的 bitbucket 存储库变量。我编辑我的 bitbucket-pipelines.yml 到:base64 -w 0 < private_keyimage:&nbsp;python:3.7.4-alpine3.10pipelines: &nbsp;&nbsp;branches: &nbsp;&nbsp;&nbsp;&nbsp;master: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;step: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;services: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;docker &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caches: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;pip &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;script: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;apk&nbsp;add&nbsp;--update&nbsp;coreutils &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;mkdir&nbsp;-p&nbsp;~/.ssh &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;(umask&nbsp;&nbsp;077&nbsp;;&nbsp;echo&nbsp;$SSH_PRV_KEY&nbsp;|&nbsp;base64&nbsp;--decode&nbsp;>&nbsp;~/.ssh/id_rsa) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;pip3&nbsp;install&nbsp;awscli &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;IMAGE="$AWS_IMAGE_PATH/jb_common" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;TAG=1.0.${BITBUCKET_BUILD_NUMBER} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;aws&nbsp;ecr&nbsp;get-login-password&nbsp;--region&nbsp;$AWS_DEFAULT_REGION&nbsp;|&nbsp;docker&nbsp;login&nbsp;--username&nbsp;AWS&nbsp;--password-stdin&nbsp;$AWS_IMAGE_PATH&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;aws&nbsp;ecr&nbsp;list-images&nbsp;--repository-name&nbsp;"jb_common"&nbsp;--region&nbsp;$AWS_DEFAULT_REGION &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;docker&nbsp;build&nbsp;-t&nbsp;$IMAGE:$TAG&nbsp;--build-arg&nbsp;ssh_prv_key="$(cat&nbsp;~/.ssh/id_rsa)"&nbsp;&nbsp;. &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;docker&nbsp;push&nbsp;$IMAGE:$TAG
随时随地看视频慕课网APP

相关分类

Go
我要回答