This is the first post. It may be difficult to read the text, but thank you.
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.
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)
Make a note of the URL with Repository Create → Git Clone
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
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
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