Deploying to Heroku using git on bitbucket
Asked Answered
H

7

86

I want to host my source on bitbucket using git because I obviously get a free private repo and I want to host my app on heroku using the source code from bitbucket.

Can i do it using the github client and heroku toolbelt. Will it work? Github is great but i dont want everyone seeing my code and I dont want to pay for a private repo because its a small project.

Hygroscope answered 5/11, 2012 at 20:55 Comment(3)
how did you exactly solve your problem? :)Counterreply
Its actually very easy. Just create a repo in bit bucket and change your origin in the config file to that of the repo and you can use bitbucket to your hearts content.Hygroscope
In which config file? My concrete problem is following..#18128261Counterreply
S
67

Deploying to Heroku should work regardless of where you host your code as the Heroku CLI adds it's own git remote for the sake of deployments. In fact, you can even git pull from Heroku, so you could technically use Heroku as a private git repository instead (though not recommended). As for using the GitHub client to connect to bitbucket, simply change the repository remote to the URL provided by bitbucket in the settings tab of the client.

Stripper answered 5/11, 2012 at 23:37 Comment(3)
where should I change the remote url?Revisory
Though the Heroku docs have this to say on the matter: "Heroku provides the git service primarily for deployment, and the ability to clone from it is offered as a convenience. We strongly recommend you store your code in another git repository such as GitHub and treat that as canonical." (source)Templeton
@FabienSnauwaert Agree. I definitely do not recommend using Heroku git as primary VCS. I will see if I can update my answer to clarify this. Thanks for the above quoteStripper
E
35

Just to add to zeiv's answer who said it should work: I can confirm that it does. We use bitbucket for git hosting and deploy to heroku. What you can't seem to do is add your bitbucket repo to your heroku account to have commit history show up, this feature seems to be currently limited to github (heroku's fault ;-)

Erigeron answered 8/11, 2012 at 22:52 Comment(0)
V
19

Bitbucket supports now Pipelines, which should make it pretty easy to deploy on Heroku. Just follow this tutorial: https://confluence.atlassian.com/bitbucket/deploy-to-heroku-872013667.html

My bitbucket-pipelines.yml to just push the master branch to Heroku looks like this:

image: node:6
clone:
  depth: full
pipelines:
  branches:
    master:
      - step:
          script:
            - git push -f https://heroku:[email protected]/$HEROKU_APP_NAME.git $BITBUCKET_BRANCH
Valaree answered 22/2, 2017 at 17:1 Comment(3)
did you achive it working? I still can't push to heroku :(Seldun
nvm, just a typo that I wasn't noticed :( ... haha, thanksSeldun
This works but make sure to use the API key from dashboard.heroku.com/account, not from heroku auth:tokenEvieevil
T
15

Chiming in with Stefan - this works perfectly. Here's what I did:

  1. Got really frustrated with the way my WP blog was resetting daily, presenting anyone who navigated to http://blog.example.com with a setup screen, because there was no wp-config.php.
  2. Logged into bitbucket.org.
  3. Linked my bitbucket & github accounts.
  4. Forked my "wp-blog" repo from github, which I had previously linked to my heroku remote.
  5. Cloned into this new fork ("git clone https://[email protected]/myname/wp-blog_config.git") .
  6. Added a proper wp-config.php.
  7. Added my heroku remote from within this new fork ("git remote add heroku [email protected]:adjective-noun-1234.git")
  8. Committed & deployed to heroku ("git push heroku master:master")
Tigon answered 7/11, 2013 at 13:26 Comment(0)
C
4

If you dont want to work in the command line and push to heroku the whole time and worry about maintaining SSH keys (quite annoying if you work on different boxes), then follow this guide on how to setup continuous integration using codeship. Its a free plugin on heroku.

http://blog.codeship.io/2014/04/29/continuous-deployment-heroku-bitbucket-nodejs.html

Cloven answered 3/9, 2014 at 7:28 Comment(0)
T
1

I found this Page helpful

Install Heroku Toolbelt

If you haven't already, log in to your Heroku account and follow the prompts to create a new SSH public key.

$ heroku login

Create a new Git repository

Initialize a git repository in a new or existing directory

$ cd my-project/
$ git init
$ heroku git:remote -a PROJECTNAME

Deploy your application

Commit your code to the repository and deploy it to Heroku using Git.

$ git add .
$ git commit -am "make it better"
$ git push heroku master

Existing Git repository

For existing repositories, simply add the heroku remote

$ heroku git:remote -a PROJECTNAME
Tumefaction answered 12/8, 2016 at 11:31 Comment(1)
how is that related to bitbucket?Erdei
C
0

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

  1. 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.
  2. The Bitbucket pipeline just needs to create a 'tgz' file with the application source code and send that package to Heroku
  3. Heroku receives the TGZ file, unzips it, builds the application, then deploys the resulting package.

Heroku Application Configuration

  1. Create the application
  2. Create an api key to be used by Bitbucket to invoke Heroku's API
  3. 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

  1. You need to enable pipeline execution in the repository configuration:
    Bitbucket -> Repository root page -> Repository Settings -> Pipeline Settings -> check 'Enable Pipelines'
  2. 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.

Cloyd answered 6/5, 2023 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.