herokubitbucket-pipelinesheroku-cliheroku-api

Heroku Container:Release on Bitbucket Pipeline


I'm trying to build Bitbucket pipeline (Dockerize) for Heroku CD of my .NET core web API project. My pipeline script:

image: atlassian/default-image:3

options:
  docker: true

pipelines:
  branches:
    'develop':
    - step:
        name: Docker Build And Push
        deployment: Develop
        services:
          - docker
        caches:
          -  docker
        script:
          - echo "Docker Build"
          - docker build -t $HEROKU_APP_NAME .
          - echo "Heroku Registry Login"
          - docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com
          - echo "Heroku Image Tagging"
          - docker tag $HEROKU_APP_NAME registry.heroku.com/$HEROKU_APP_NAME/web
          - echo "Heroku Docker Push"
          - docker push registry.heroku.com/$HEROKU_APP_NAME/web
          - docker inspect registry.heroku.com/$HEROKU_APP_NAME/web --format='{{.Id}}' > HEROKU_DOCKER_IMAGE_ID
          - echo "Heroku Publish Done"
          - echo "??? How to execute 'heroku container:release web -a $HEROKU_APP_NAME'"
        artifacts:
          - HEROKU_DOCKER_IMAGE_ID

The pipeline script running fine with results:

  1. I can do docker image build
  2. Docker login into Heroku registry
  3. Push docker build into Heroku registry
  4. Get the last pushed image from Heroku registry for my application

The last step that necessary for Heroku to update the running application with latest uploaded docker image. If using PowerShell, the command is:

heroku container:release web -a <<app-name>>

But since this process run within Bitbucket pipeline, there's no Heroku CLI to execute (I've tried adding the Heroku npm, but the packages are deprecated).

I've also tried using shell script (.sh) for Platform API Container Registry & Runtime (Docker Deploys):

#!/usr/bin/env bash

curl -n -X PATCH https://api.heroku.com/apps/$HEROKU_APP_NAME/formation \
-d '{
    "updates": [
        {
            "type": "web",
            "docker_image": "'"$HEROKU_DOCKER_IMAGE_ID"'"
        }
    ]
}' \
-H "Content-Type: application/json" \
-H "Accept: application/vnd.heroku+json; version=3.docker_releases" \
-H "Authorization: Bearer $HEROKU_API_KEY"

This also unable to update my Heroku app with latest docker image (CURL result is HTTP 200OK, no error)

I'm expecting solution for executing last command heroku container:release web to update my Heroku application within Bitbucket pipeline.


Solution

  • The canonical mechanism to deploy to Heroku from Bitbucket Pipelines is using Atlassian's pipe instead of any API or CLI:

    https://bitbucket.org/product/features/pipelines/integrations?p=atlassian/heroku-deploy

    pipelines:
      branches:
        develop:
          - step:
              name: Docker Build
              services:
                - docker
              caches:
                -  docker
              script:
                - docker build -t $HEROKU_APP_NAME .
                - docker login --username=_ --password=$HEROKU_API_KEY registry.heroku.com
                - docker tag $HEROKU_APP_NAME registry.heroku.com/$HEROKU_APP_NAME/web
                - docker push registry.heroku.com/$HEROKU_APP_NAME/web
          - step:
              name: Heroku Deploy
              deployment: development
              script:
                - pipe: atlassian/heroku-deploy:2.1.0
                  variables:
                    HEROKU_API_KEY: '<string>'
                    HEROKU_APP_NAME: '<string>'
                    # ... checkout the docs
    

    If you don't want to use the pipe I'd advise to research an image that packs Heroku's CLI https://hub.docker.com/search?q=heroku-cli or build it youself.

    The atlassian/default-image image just lacks Heroku tooling. It is a mashup of diverse languages and tooling you will generally want to AVOID.