Sunday, January 27, 2019

CICD with CircleCI and Heroku

Full Stack Software Development Blog


The final task to finishing off the server work is to wire up continuous integration and continuous deployment (CICD). I already have the CI piece of it working (see previous blog). Now to do the CD piece.

Step 1. Deploy Spring Boot application to Heroku

I followed the steps in the getting started on Heroku with Java tutorial.
See https://devcenter.heroku.com/articles/getting-started-with-java

The only thing of interest is the Procfile

web: java -jar application/target/texastoc-application-1.0.jar --server.port=$PORT --spring.profiles.active=test com.texastoc.Application

As you can see the Spring Boot application is run as a jar.
Set the server port as instructed by Heroku.
Set the spring.profiles.active to test so the it would use the embedded H2 database (for now).

Step 2. Integrate CircleCI to deploy to Heroku

Next I followed the CircleCI instructions to integrate with Heroku.
See https://circleci.com/integrations/heroku/

First I had to set a couple environment variables
  • HEROKU_APP_NAME
  • HEROKU_API_KEY
The big change is in the .circleci/config.yml file to deploy after building.
See https://github.com/gpratte/texastoc-v2-spring-boot/blob/master/.circleci/config.yml

Step 3. Spring Actuator

I needed to verify the deployment. What better way than to add a new endpoint (or two) to the server, push to github master and then curl the endpoint on the server deployed to Heroku.

I added Spring Actuator and curled the health endpoint.
See https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready.html to know more about Spring Actuator.


Saturday, January 26, 2019

JWT Auth for Spring Boot

Full Stack Software Development Blog

I took what I learned in my last blog about JWT Authentication and Authorization and applied it to my Spring Boot server application.

Most of the time was spent updating the integration tests. Almost every endpoint now requires authentication. The test were enhanced to login a player which returns a token. The token is then passed in the HTTP Authorization header.

Another departure from the proof of concept in my last blog is to

  • use the player table instead of the user table 
  • use email instead of username

See branch 37-jwt-auth at TexasTOC v2 Spring Boot

Monday, January 21, 2019

JWT Authentication and Authorization with Spring Security

Full Stack Software Development Blog


Worked through how to implement Authentication and Authorization (A&A) using JSON Web Token (JWT) with Spring Security.

Instead of blogging about it here I put together a proof of concept (POC) in github. The readme tells all.

See the https://github.com/gpratte/jwtauth repo.

Sunday, January 13, 2019

CircleCI for continuous integration

Full Stack Software Development Blog


Using the free plan for CircleCI to build and run tests.

Here were the steps I followed

Chose the CircleCI free plan and use my github credentials to log into CircleCI.

Chose the texastoc-v2-spring-boot github repo for CircleCI to use. CircleCI added a key to this repo.


Followed the CircleCI directions to

  • add a .circleci directory to my project.
  • add a config.yml in the .circleci directory for a Java maven project

Now when I push to any branch CircleCI it downloads dependencies, compiles and run the tests.

Of course there were problems :)

Problem 1

My Java project is split into two modules: application and integration. That way I keep my integration tests separate from the application code. The integration module has a dependency on the application module.

I did not have a problem running the tests locally either in IntelliJ or using maven on the command line. But CircleCI choked on the integration module having a dependency on the application module. CircleCI actually wanted the application module as a library for the integration module to use.

I changed the config.yml so that the application module created a library.

Problem 2

When CircleCI attempted to run the test there was an obscure error.

I followed the instructions to add an ssh key to github which allowed me to run CircleCI in ssh mode. I was able to ssh into the build and look at the dump file. I googled the error and found a workaround that I added to my maven pom.xml.

Of course I added a comment to the pom.xml to a link to the stackoverflow solution to the file.

https://stackoverflow.com/questions/50661648/spring-boot-fails-to-run-maven-surefire-plugin-classnotfoundexception-org-apache


Friday, January 11, 2019

Server APIs coded

Full Stack Software Development Blog


Finished coding the logic for the server APIs (or as finished as any API is before the client starts calling it).

In one stretch of 10 days wrote 5000 lines of code (loc). 3000 were for the tests and 2000 for the application. Add code was done via TDD. JUnit for the Java classess/methods and Cucumber to exercise the APIs.

Now need to revisit the Authentication and Authorization (A&A). Since it is a REST service I will use a JWT token.

And of course the A&A will be done via TDD :)