You can integrate a Bitbucket with Heroku and automate the deployment.
I created this project in github to show the minimum setup needed to deploy a bitbucket repository to Heroku.
How the deployment works
- Assuming you have checked out a branch from Bitbucket repository, you just need to push a change and that'll trigger the pipeline on Bitbucket.
- The Bitbucket pipeline just needs to create a 'tgz' file with the application source code and send that package to Heroku
- Heroku receives the TGZ file, unzips it, builds the application, then deploys the resulting package.
Heroku Application Configuration
- Create the application
- Create an api key to be used by Bitbucket to invoke Heroku's API
- Set the buildpack that Heroku must use to build your application:
Go to your application root page in Heroku -> Settings -> Buildpacks -> Add Buildpack -> 'buildback_for_your_application'
Bitbucket Repository Configuration
- You need to enable pipeline execution in the repository configuration:
Bitbucket -> Repository root page -> Repository Settings -> Pipeline Settings -> check 'Enable Pipelines'
- Set the environment variables that are used by the ./bitbucket-pipelines.yml file:
Bitbucket -> Repository root page -> Repository Settings -> Pipeline Settings -> check 'Repository Variables'
, then set:
- HEROKU_APP_NAME: the name your gave to your application in Heroku
- HEROKU_API_KEY: the api key you created in Heroku to allow api requests to your application.
Files
Set the following files in the root folder of your application
File |
Used by |
Description |
./bitbucket-pipelines.yml |
Bitbucket |
Defines the pipeline rules for Bitbucket. |
./Procfile |
Heroku |
Tells Heroku this application receives requests from the internet, and what is the command line to execute it. |
./system.properties |
Heroku |
Sets what java sdk must be used to run this app. |
./bitbucket-pipelines.yml example
image: gradle:jdk17 # change the image to suit your application.
# The minimum steps required to deploy from Bitbucket to Heroku are:
# 1. create a TGZ file with the application source code
# 2. send that TGZ to Heroku
pipelines:
default:
- step:
name: Create TGZ from source code
script:
- tar --exclude='.git' -cvzf /tmp/app.tar.gz .
- mv /tmp/app.tar.gz .
artifacts:
- app.tar.gz
- step:
name: Deploy source code TGZ to heroku
deployment: production
script:
- pipe: atlassian/heroku-deploy:0.1.1
variables:
HEROKU_API_KEY: $HEROKU_API_KEY
HEROKU_APP_NAME: $HEROKU_APP_NAME
ZIP_FILE: app.tar.gz
./Procfile example
web: java -jar build/libs/demo-application-0.0.1.jar
That example is for a java application that receives requests from the internet. Change is to suit your application.
./system.properties example
java.runtime.version=17
Needed for java application. Change it to suit your application.
Your application must listen on the '$PORT' port
Heroku sets a random port number in the $PORT
environment variable and it expects your application will be listening on that port. Make sure your application uses that environment variable to set the listening port.