The story of pushing Java to Heroku using the BitBucket pipeline

This is the first post. It may be difficult to read the text, but thank you.

motivation

I want to automatically deploy to Heroku when I push it to Git. But GitHub was acquired by Microsoft ~~ If you don't charge, you can see things that you don't want to see because it's public. BitBucket is private, so it seems to work.

** Premise **

** Try it now **

** First, prepare for Heroku **

Create any project from Heroku CLI or from the web application management screen Make a note of Heroku's API key (you can check it from the Account Settings screen)

** Next, create a BitBucket repository **

Make a note of the URL with Repository Create → Git Clone

** Java project creation **

Right-click → New → Create with New Spring Starter Project The project name created after that is SampleProject, and the variable name in each file is also SampleProject. Please read it as your project name.

SampleProject


├─src/main/java
├─src/main/resources
│ ├─application.properties
├─mvnw
├─mvnw.cmd
└─pom.xml

OK if it has the above package structure Create and modify BitBucket and Heroku-specific files from here

system.properties (new)


java.runtime.version=1.8

Procfile (new)


web: java -jar target/SampleProject-0.0.1jar --server.port=${PORT}

pom.xml(Fix)


…
	<artifactId>SampleProject</artifactId>
	<version>0.0.1</version>
	<packaging>jar</packaging>
…

Fixed Packaging in pom.xml to jar Confirm that the artifactId & version name of pom.xml is the same as Procfile

application.properties(Fix)


Added the following description

server.port=${PORT:5000}

bitbucket-pipelines.yml (new)


# This is a sample build configuration for Java (Maven).
# Check our guides at https://confluence.atlassian.com/x/zd-5Mw for more examples.
# Only use spaces to indent your .yml configuration.
# -----
# You can specify a custom docker image from Docker Hub as your build environment.
image: maven:3.3.9

pipelines:
  default:
    - step:
        caches:
          - maven
        script: # Modify the commands below to build your repository.
          #- mvn -B verify # -B batch mode makes Maven less verbose
          - git push https://heroku:"Heroku API key"@git.heroku.com/"Heroku project name".git HEAD

Description of each file

system.properties Configuration file required to run maven project after build on Heroku side Procfile Files needed to run the project on Heroku. Describe the execution command By the way, in the maven project, when mvn build is executed, the build result file (jar in this case) is stored in the target folder directly under the project. You have specified to do this pom.xml Changed mvn build goal to jar (I think the default was war) application.properties The server port on heroku side seems to be 5000, so it is described like this (as officially written) bitbucket-pipelines.yml Use the pipeline function of bitbucket. When pushed to the master branch of bitbucket, it will automatically execute the process described in yaml Here, since java is managed by maven this time, ① Declare the maven project at the beginning ② Push to heroku's project branch are doing. When pushed to heroku, it will automatically determine the project language, configuration file, etc. and build it without permission.

Final folder structure (If it is not this configuration, bitbucket and heroku will not work, so check it)

SampleProject


├─src/main/java
├─src/main/resources
│ ├─application.properties
├─bitbucket-pipelines.yml
├─mvnw
├─mvnw.cmd
├─pom.xml
├─Procfile
├─pom.xml
└─system.properties

** Finally push to bitbucket **

When you push to bitbucket, ① The bitbukcet pipeline starts ② Push from bitbukcet to git repository on heroku side ③ heroku automatically determines that it is a maven project and executes maven build ④ Start the jar directly under the target folder with the java command It will be deployed on heroku according to the flow of. After pushing, check the log of the pipeline screen of bitbucket and the log of Heroku to make sure that various commands are working. If all goes well, you're done.

Once you've created your pipeline, it will be automatically deployed to heroku every time you push it to BitBucekt. It's the end.

Recommended Posts

The story of pushing Java to Heroku using the BitBucket pipeline
[Java version] The story of serialization
20190803_Java & k8s on Azure The story of going to the festival
[Apache Tomcat] The story of using Apache OpenWebBeans to enable CDI
Output of the book "Introduction to Java"
The story of writing Java in Emacs
The story of forgetting to close a file in Java and failing
[Java] How to get to the front of a specific string using the String class
The story of low-level string comparison in Java
The story of learning Java in the first programming
The story of introducing Ajax communication to ruby
The story of raising Spring Boot 1.5 series to 2.1 series
The story of adding the latest Node.js to DockerFile
[Java] How to get the authority of the folder
Story of test automation using Appium [Android / java]
Java Welcome to the Swamp of 2D Arrays
[Java] How to easily get the longest character string of ArrayList using stream
[Java] How to get the URL of the transition source
How to write Scala from the perspective of Java
The story of migrating from Paperclip to Active Storage
[Java] How to get the maximum value of HashMap
Java: Use Stream to sort the contents of the collection
The first step to using Xib instead of StoryBoard
The story of making Dr. Oakid using LINE BOT
The story of making dto, dao-like with java, sqlite
The story of raising Spring Boot from 1.5 series to 2.1 series part2
The story of pushing a Docker container to GitHub Package Registry and Docker Hub with GitHub Actions
I tried to summarize the basics of kotlin and java
Get to the abbreviations from 5 examples of iterating Java lists
Deploy Spring Boot applications to Heroku without using the Heroku CLI
Command to check the number and status of Java threads
[Java] Try editing the elements of the Json string using the library
[Java] To know the type information of type parameters at runtime
How to derive the last day of the month in Java
Be sure to compare the result of Java compareTo with 0
Reintroduction to Java for Humanities 0: Understanding the Act of Programming
How to play MIDI files using the Java Sound API
How to check the database of apps deployed on Heroku
Input to the Java console
[Java] Various summaries attached to the heads of classes and members
[Java] Try to solve the Fizz Buzz problem using recursive processing
The story of not knowing the behavior of String by passing Java by reference
The story of Collectors.groupingBy that I want to keep for posterity
Try Hello World with the minimum configuration of Heroku Java spring-boot
[Rails / Heroku / MySQL] How to reset the DB of Rails application on Heroku
How to get the contents of Map using for statement Memorandum
The story of toString () starting with passing an array to System.out.println
A story about hitting the League Of Legends API with JAVA
The milliseconds to set in /lib/calendars.properties of Java jre is UTC
From fledgling Java (3 years) to Node.js (4 years). And the impression of returning to Java
The story of making a communication type Othello game using Scala.
How to solve the unknown error when using slf4j in Java
I tried to display the calendar on the Eclipse console using Java.
How to check for the contents of a java fixed-length string
Is the version of Elasticsearch you are using compatible with Java 11?
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
[Java] Delete the elements of List
[java8] To understand the Stream API
Java comparison using the compareTo () method
[Java] Input to stdin of Process