【问题标题】:drone ci deploy to google compute engine无人机 ci 部署到谷歌计算引擎
【发布时间】:2019-07-06 17:50:59
【问题描述】:

我正在尝试使用 Drone ci 在 Google 云计算引擎上自动部署我的应用程序。我尝试了很多方法,但我无法部署任何东西。

我的测试和发布阶段运行良好,我可以将我的代码自动推送到我的谷歌容器注册表,无人机构建映像并推送它。

我对我的计算引擎实例进行了身份验证,我试图让机器连接到谷歌容器注册表,但我无法设法运行构建的图像。这是我缺少的最后一步,我做不到。

这是我的 .drone-ci.yml 文件,包含以下步骤:

kind: pipeline
name: my-app

steps:
  - name: test
    image: node:11-alpine
    commands:
      - npm install
      - npm run test

  - name: publish
    image: plugins/gcr
    settings:
      repo: project-id/my-app
      dockerfile: Dockerfile
      tags: latest
      json_key:
        from_secret: google_credentials

  - name: deploy
    image: google/cloud-sdk:alpine
    environment:
      google_credentials:
        from_secret: google_credentials
    commands:
      - echo $google_credentials > /tmp/$CI_PIPELINE_ID.json
      - gcloud auth activate-service-account --key-file /tmp/$CI_PIPELINE_ID.json
      - gcloud compute ssh my-instance --zone my-zone --command "cat $google_credentials | sudo docker login -u _json_key --password-stdin https://gcr.io"
      - gcloud compute ssh my-instance --zone us-east1-b --command "sudo docker run -d -p 80:3000 --restart always --env-file ./env.list gcr.io/project-id/my-app:latest"

它抛出这个错误(是最后一个错误):

cat: '{': No such file or directory
bash: line 1: type:: command not found
bash: line 2: project_id:: command not found
bash: line 3: private_key_id:: command not found
bash: line 4: private_key:: command not found
bash: line 5: client_email:: command not found
bash: line 6: client_id:: command not found
bash: line 7: auth_uri:: command not found
bash: line 8: token_uri:: command not found
bash: line 9: auth_provider_x509_cert_url:: command not found
bash: line 10: client_x509_cert_url:: command not found
bash: -c: line 11: syntax error near unexpected token `}'
bash: -c: line 11: `} | sudo docker login -u _json_key --password-stdin 
https://gcr.io'

我尝试了其他方式,但我从未成功通过 google 容器注册表进行身份验证。 如何从实例中验证 docker 守护进程?

【问题讨论】:

    标签: docker google-compute-engine gcloud drone.io


    【解决方案1】:

    这里的错误命令是:

    cat $google_credentials | sudo docker login -u _json_key --password-stdin https://gcr.io
    

    您的变量$google_credentials 包含JSON 键的内容:它不包含文件的路径,因此cat $google_credentials 没有意义。由于文件 /tmp/$CI_PIPELINE_ID.json 包含您的 JSON 密钥,您应该编写:

    cat /tmp/$CI_PIPELINE_ID.json | \
        sudo docker login -u _json_key --password-stdin https://gcr.io
    

    或者(未经测试,请谨慎使用):

    echo $google_credentials | \
        sudo docker login -u _json_key --password-stdin https://gcr.io
    

    第三种解决方案(我确信这可行,因为我已经使用它进行身份验证)是:

    docker login -u _json_key -p "$(cat /tmp/$CI_PIPELINE_ID.json)" https://gcr.io
    

    【讨论】:

    • 嗨,感谢您的回答,我正在尝试第三种解决方案。我如何在 --command 引号中使用这些命令? gcloud cli 使用 --command "your command here" 通过 ssh 执行命令。这看起来像: --command "sudo docker login -u _json_key -p "$(cat /tmp/$CI_PIPELINE_ID.json)" gcr.io"
    • 由于远程实例上不存在/tmp/$CI_PIPELINE_ID.json,我认为您应该选择第二种解决方案。你可以试试:gcloud compute ssh my-instance --zone my-zone --command "echo \"$(echo $google_credentials)\" | sudo docker login -u _json_key --password-stdin https://gcr.io"$(echo $google_credentials) 应该在主机上解释,而不是在远程机器上,所以它应该可以工作。如果可行,我会更新答案。
    猜你喜欢
    • 2015-04-23
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多