Deploy docker container from external registry to

2019-05-02 03:02发布

问题:

I got project repository hosted on gitlab. I am using gitlab-ci to build docker container from my project. What I would like to achieve is deploying that container to heroku.

I was trying to follow solution from this question: How to build, test and deploy using Jhipster, Docker, Gitlab and Heroku

Here is how my .gitlab-ci.yaml looks like:

stages:
 - build
 - package
 - deploy

build_npm:
  image: node:latest
  stage: build
  script:
  - npm install
  - npm run build:prod
  artifacts:
    paths:
      - dist/

build_image:
  image: docker:latest
  services:
  - docker:dind
  stage: package
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
    - docker build -t registry.gitlab.com/maciejsobala/myApp .
    - docker push registry.gitlab.com/maciejsobala/myApp:latest


deploy_to_heroku:
  stage: deploy
  services:
  - docker:dind
  script:
    - gem install dpl
    - docker run registry.gitlab.com/maciejsobala/myApp:latest
    - dpl --provider=heroku --app= myApp --api-key=$HEROKU_API_KEY

What I am trying to achieve is, have 3 stages:

  • build: at this moment, compile only npm project (in the future, I want to add some jar here)
  • package: create and push to registry docker image.
  • deploy: install docker image on heroku.

I am running into issues with the last stage (deploy). To be honest I am not really sure, what should be done here.

I tried to use dpl, regarding to this tutorial: https://docs.gitlab.com/ce/ci/examples/test-and-deploy-ruby-application-to-heroku.html

Unfornatelly I am running into issues when trying to run docker image

$ docker run registry.gitlab.com/maciejsobala/myApp:latest
/bin/bash: line 49: docker: command not found

I am completely blind here. I would really appreciate any solutions, links to articles/tutorials etc.

回答1:

You are starting the app for some reason (using docker run) you might don't need. The dpl tool is intended to be used inside a codebase, rather than for image deployment. As you said

build_image:
  image: docker:latest
  services:
  - docker:dind
  stage: package
  script:
    - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN registry.gitlab.com
    - docker build -t registry.gitlab.com/maciejsobala/myApp .
    - docker push registry.gitlab.com/maciejsobala/myApp:latest

is working, what means your runner is able to run docker in docker and successfully pushing images. For heroku deployment, you must only push that image to the heroku docker registry, according to the official heroku documentation. In short you do a

deploy_to_heroku:
  stage: deploy
  services:
  - docker:dind
  script:
    - docker login --email=_ --username=_ --password=<YOUR-HEROKU-AUTH-TOKEN> registry.heroku.com
    - docker tag registry.gitlab.com/maciejsobala/myApp:latest registry.heroku.com/maciejsobala/myApp:latest
    - docker push registry.heroku.com/maciejsobala/myApp:latest

with your heroku auth token, which you can get by heroku auth:token

As said in the documentation, pushing to herokus registry triggers a release process of the app.



回答2:

The reason for

"No such image: registry.gitlab.com/username/image:tag"

error is that tag source should be pulled beforehand. script block should include a docker pull statement. The overall script block should be as follows:

  script:
    - docker login --email=_ --username=_ --password=<YOUR-HEROKU-AUTH-TOKEN> registry.heroku.com
    - docker pull registry.gitlab.com/maciejsobala/myApp:latest
    - docker tag registry.gitlab.com/maciejsobala/myApp:latest registry.heroku.com/maciejsobala/myApp:latest
    - docker push registry.heroku.com/maciejsobala/myApp:latest

Still this is not enough. Heroku changed its release policy so that pushing to Heroku Container Registry does not trigger a release anymore. Here is the extra command to fulfill the missing release task:

    - docker run --rm -e HEROKU_API_KEY=<YOUR-HEROKU-AUTH-TOKEN> wingrunr21/alpine-heroku-cli container:release web --app myApp