buildspec.yml file for Spring Boot project - java

I have a Spring Boot project named "BikeService". I am generating a war file for this project by using the following command in the pom.xml file
<packaging>war</packaging>
Now I want to deploy this on Elastic Beanstalk which I have creating using
Tomcat 8.5 with Java 8 running on 64bit Amazon Linux/3.3.6
After uploading the code the deployment is successful and the Health of the environment is OK but on running the environment URL it is showing HTTP 404 - Not Found.
I am using the following buildspec.yml file:
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
pre_build:
commands:
- echo In the pre_build phase...
build:
commands:
- echo Build started on `date`
- mvn install
post_build:
commands:
- echo Build completed on `date`
artifacts:
files:
- target/bike-service-0.0.1-SNAPSHOT.war
- scripts/*

So the problem was solved after a lot of researching.
The issue was with the buildspec.yml file.
I am posting the correct buildspec.yml file in case anyone searches for it in the future.
The following file if for a Spring Boot project with war packaging.
version: 0.2
phases:
install:
runtime-versions:
java: corretto8
pre_build:
commands:
- echo In the pre_build phase...
build:
commands:
- echo Build started on `date`
post_build:
commands:
- echo Build completed on `date`
- mvn package
- mv target/bike-service-0.0.1-SNAPSHOT.war bike-service-0.0.1-SNAPSHOT.war
artifacts:
files:
- bike-service-0.0.1-SNAPSHOT.war
- .ebextensions/**/*

Related

Providing full path to java in gitlab config

In my gitlab-ci.yml configuration I have the following snippet:
before_script:
- java -jar /home/gitlab-runner/tools/ciVersioner-1.0.jar $MAJOR $MINOR $CI_COMMIT_SHORT_SHA
script:
- mvn --projects employees --also-make clean package -DskipTests
after_script:
- echo $HOSTNAME $WHOAMI
artifacts:
paths:
- employees/target/*.jar
expire_in: 30 days
It does not work but for a different reason (since java versions are incompatible).
Here's the output:
The solution was to install a newer java 11.
I did so and changed the configuration correspondingly:
before_script:
- /opt/java11/jdk-11/bin/java -jar /home/gitlab-runner/tools/ciVersioner-1.0.jar $MAJOR $MINOR $CI_COMMIT_SHORT_SHA
script:
- mvn --projects employees --also-make clean package -DskipTests
after_script:
- echo $HOSTNAME $WHOAMI
artifacts:
paths:
- employees/target/*.jar
expire_in: 30 days
However, with this configuration nothing works at all:
But if I execute this command in VM environment directly, it works as expected.
Is there a limitation on using full qualified path in the yml file or maybe it has to do with something else?
Before you updated java the job fail at the java command, so it didn't try to execute mvn.
However, when the first problem is solved, you get a new issue and now with mvn command.
I guess the runner cannot recognize mvn because maven is not installed.
You could try adding the following to the before_script section:
- apt-get update && apt-get install maven
This is will depend on the linux distribution, use yum instead if the runner is on redhat or centos

Elastic Beanstalk | CodePipeline will not deploy .war file correctly

I'm having a problem with my .war file deployment through CodePipeline (using CodeBuild) to my Elastic Beanstalk environment. After a successful pipeline deployment, it throws a 404 error (see picture).
However, when I upload the .war file from my project code directly into my Elastic Beanstalk environment, it works just fine.
Ideally, I want to just be able to run the CodePipeline to update the versioning.
My CodeBuild (buildspec) -
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
build:
commands:
- ls -la
- cd prod/
- mvn clean
- mvn install
# post_build:
# commands:
artifacts:
files:
- backend/target/backend-0.0.1-SNAPSHOT.war
# name: artifact
base-directory: prod/
404 Error -
I've tried this as well and it still gives me a 404 error
version: 0.2
phases:
install:
runtime-versions:
java: corretto11
build:
commands:
- ls -la
- cd prod/
- mvn clean
- mvn install
post_build:
commands:
- mv backend/target/backend-0.0.1-SNAPSHOT.war ROOT.war
artifacts:
files:
- ROOT.war
AWS Elastic Beanstalk expects to find and run a ROOT.war at root directory.
ROOT.war runs at myapp.elasticbeanstalk.com. In a single WAR source
bundle, the application always runs at the root path.
(Source)
You have two options to resolve this
Make sure that your artifact is named ROOT.war and is found at root level (in contrary to backend/target/backend-0.0.1-SNAPSHOT.war)
Use the post_build phase in your CodePipeline to rename and move the file adequately (the easier option).
Option 2 approach:
post_build:
commands:
- mv backend/target/backend-0.0.1-SNAPSHOT.war ROOT.war

How to deploy a maven repository on gitlab to heroku

I created a .gitlab-ci.yml file and tried to deploy my maven repository on gitlab to heroku. Unfortunately I don't understand how to do this without the heroku-cli (which I could not install on gitlab).
The build process and test process is not failing but the deployment process is due to my lack of information about how it exactly works.
The yml file I created looks like this:
before_script:
- apk update && apk add git
- apk update && apk add openjdk8
- apk update && apk add maven
- git --version
build:
stage: build
script:
- mvn compile
test:
stage: test
script:
- mvn test
deploy:
stage: deploy
script:
- heroku git:remote -a NAME_OF_MY_HEROKUAPP
- git push heroku master
only:
- master

How to deploy to Herouku through Travis CI without building the project twice?

I have a JHipster project I want to build through Travis CI and then deploy to Heroku. However, even if Travis CI passes the build, it commits the whole code to Heroku and then Heroku tries to build the whole project again, defeating the purpose of having Travis CI in the middle.
Is there a way to build my project with Travis CI and then just deploy the build WAR to Heroku?
Given the fact that JHipster uses Spring Boot, I would only need the WAR to run the application.
Here is my .travis.yml files:
language:
- java
jdk:
- oraclejdk8
sudo: false
env:
- MAVEN_CUSTOM_OPTS="-Pprod,heroku -DskipTests"
before_install:
- npm install -g npm
- rm -fr /home/travis/.npm/
- npm cache clean -g
- npm install grunt-cli -g
- npm install
install:
- mvn package -Pprod -DskipTests
script: true
notifications:
webhooks:
on_success: change
on_failure: always
on_start: false
deploy:
provider: heroku
api_key:
secure: [key]
skip_cleanup: true
Also, if there is another way to deploy a Spring Boot application through this process, it would be great.
Thank you.
You can install the heroku-deploy plugin on Travis CI and deploy the same way you deploy to Heroku locally.
In fact, this is what the heroku-deploy plugin does to test itself, so you can use it as an example. The setup script downloads the Ubuntu installer for the toolbelt from: https://toolbelt.heroku.com/install-ubuntu.sh
Then you'll only need to install the plugin with this command in your setup script:
heroku plugins:install https://github.com/heroku/heroku-deploy
And set your API key as a secure variable in the .travis.yml

Can I specify which version of Maven is used when deploying my Java app to the Heroku Cedar stack?

I'm deploying an application that requires Maven 3.0.4 to the Heroku Cedar stack, but the build fails with this warning:
Rule 1: org.apache.maven.plugins.enforcer.RequireMavenVersion failed with message:
Build with Maven 3.0.4 or later. Maven 3.0 through 3.0.3 inclusive do not pass
correct settings.xml to Maven Release Plugin.
I believe that this failure is caused by this Maven bug:
REGRESSION: Injected Settings in a Mojo are missing the profiles from settings.xml.
Is there any way I can configure Heroku Cedar to use a different version of Maven?
Fork the Heroku Java Buildpack git repository, then clone it.
Open bin/compile in your editor. Look for the following lines:
# install maven with base repository
MAVEN_URL="http://s3.amazonaws.com/heroku-jvm-langpack-java/maven.tar.gz"
if [ ! -d .maven ]; then
echo -n "-----> Installing Maven 3.0.3..."
curl --silent --max-time 60 --location $MAVEN_URL | tar xz
chmod +x .maven/bin/mvn
echo " done"
fi
Change the MAVEN_URL to download Maven 3.0.4 from Apache:
MAVEN_URL="http://www.us.apache.org/dist/maven/maven-3/3.0.4/binaries/apache-maven-3.0.4-bin.tar.gz"
Commit your changes, then configure your Heroku Cedar instance to use your custom buildpack:
heroku config:add BUILDPACK_URL=[YOUR GITHUB REPO] --app [YOUR HEROKU CEDAR APP]
For details, see Heroku's instructions Using a Custom Buildpack.
It is possible to do it as long as you create your own build pack. Have a look at what I did to use maven 3.0.4 at https://github.com/jtzikas/heroku-buildpack-java.
In short what I had to do, was to change the uri of the location of maven but also change the directory structure since the heroku one contains the .m2 and the .maven directory. Those directories are needed in order for the rest of the script to run.
# install maven without base repository
MAVEN_URL="http://www.us.apache.org/dist/maven/maven-3/3.0.4/binaries/apache-maven-3.0.4-bin.tar.gz"
if [ ! -d .maven ]; then
echo -n "-----> Installing Maven 3.0.4..."
curl --silent --max-time 60 --location $MAVEN_URL | tar xz
mv apache-maven-3.0.4 .maven
mkdir .m2
chmod +x .maven/bin/mvn
echo " done"
fi
The .m2 directory is the repository location and the .maven directory is the location of the maven installation. You will need to extract the maven 3.0.4 into the .maven directory and not the apache-maven-3.0.4 that is the root of directory of maven 3.0.4. Dont forget that you need to create an empty .m2 directory to host the maven repository
Check the heroku documentation for instruction on how to use custom buildpacks.

Categories

Resources