Deploying spring boot application (jar) to octopus target

Hi, I need to deploy a spring boot application packaged as jar to my dev environment (octopus target). Spring boot already has tomcat embedded so there’s no need to use any other web server. I was thinking of using “Deploy a Java Archive” process step but I don’t know how to pass arguments like active profile (to inject proper application.properties file) while executing jar file on the target. Could you please help me with this?

Hi,

Welcome to the community!

You can achieve this by using variable substitution in files. Specifically, for a jar file we have this documentation: https://octopus.com/docs/deployment-examples/java-applications#variable-substitution-in-java-packages

Please get in touch if you have any more questions!

Thanks,
Lianne

Hi Lianne!

The doc you shared suggest using one application.properties main file in various live environments but I would like to have different properties on development environment, different on test environment and different on prod environment which means I would like to have application.properties used in my local setup and some properties overwritten in:
application-dev.properties (for dev env)
application-test.properties (for test env)
application-prod.properties (for prod env)

So I would like to be able to run jar file with --spring.profiles.active= flag.
What I did was deploy a package step to push a jar file to environment, then run a power shell script with package reference. However my spring boot application failed to start even though it is started properly when I run java -jar .jar --spring.profiles.active= directly in VM.

I wonder what’s the best way to deploy spring boot applications using octopus and targets.

Hi @etrochimiuk,

Thanks for coming back to us.

You’d be able to use Variable Substitution for this approach as Lianne mentioned for your application.properties file. You should be able to have individual files as mentioned in https://octopus.com/docs/deployment-examples/java-applications#variable-substitution-in-java-packages and it’s shown on https://octopus.com/docs/deployment-process/configuration-features/substitute-variables-in-files#spring-boot-application-properties. We also have a blog which discusses deploying Springboot files via Octopus on https://octopus.com/blog/spring-boot-as-a-service.

You can specify Octostash variables that are substituted into your application.properties file during deployment. In the example, if you specify the environment: #{Environment} or environment: #{Octopus.Environment.ID} then it would substitute during deployment and you can use this.

Let me know how you get on and if I can help further.

Thanks,

Derek

Hi Derek,

What’s the best way to deploy and run spring-boot application on target?
I have virtual machine defined (windows) and I would like to deploy a package (.zip containing .jar) to it and have it run there so I can access the application via browser.

Can I simply use “deploy a package” and “run a script” to invoke java -jar … ?

Hi @etrochimiuk,

Yes, that would work fine. You could do it in a single step also.

  1. Use Deploy a Package Step
  2. Add Tags and the Package ID of your zip file to the step
  3. Use custom Deployment Scripts from from the Configure Features button.

  1. You should now see a Customer Deployment Script option appear in your deployment step. Input your command using powershell.This would likely be a post-deployment script.

Another approach would also be to set the java application to run as a Windows Service. You can follow the same as above, but there are a few extra items to consider with the “Deploy a Windows Service” Step. You could also run the arguments as part of the Windows Service executable. We h ave a blog which discusses this and it’s a little old, but it should still be relevant and you can see it on https://octopus.com/blog/spring-boot-windows-services.

Let me know how you get on.

Thanks,

Derek

Thank you very much for your help! I was finally able to deploy and run application.
Now I need to handle environment properties to choose proper application.properties file. What I’m deploying to environment is jar file, is I use Substitute Variables in Files feature in Deploy package step, will it be able to substitute a variable in .properties file?

Hi @etrochimiuk,

That’s great news about the deployment working.

Yes, you will be able to use Octopus Variables and subsitute these with an Octopus Variable. One thing to consider is that Spring expression language also uses the #{} syntax so you need to double encode any non-Octopus variables. i.e. ##{MyVariable} in your .properties file. You can see an example on https://octopus.com/docs/deployment-process/configuration-features/substitute-variables-in-files#spring-boot-application-properties.

Let me know how you get on,

Derek

Hi Derek,

I’m still facing problems with my deployment. I have created a process with “Deploy a package” step as I mentioned before. I added post-deployment power shell script to run my jar file as a process (Start-Process) and I have background process launched in my VM. The problem is that this process is running all the time, so apparently, in order to deploy newer version of the package and run it, I should kill the previous process but I don’t have a possibility to retrieve process’ PID easily on Windows (my Virtual Machine uses Windows) and use it to kill the process on deployment of a new package. How this should be handled? Could you please help me with that?

Or if I decide to go with Windows Service, I will need to stop already running service before I deploy a newer version of the package. What if I configure octopus step to stop the service, but the service is not yet defined since I deploy my application for the first time? It would work only if I did a redeployment.

Hey there @etrochimiuk! The built-in Deploy a Windows Service template will stop an existing service update it, then start it automatically. If the service doesn’t already exist, the template will create it, then start it.

Hope this helps!

Should I include following files:

  • SpringBoot.exe - the winsw executable renamed.
  • SpringBoot.exe.config - the winsw EXE configuration file
  • SpringBoot.xml - the winsw XML configuration file
  • SpringBootStopper.jar - the Spring Boot Stopper JAR file
  • SpringBootWindowsService.jar - the Spring Boot JAR file

in my .zip package pushed to Octopus which will be later used in Deploy a Windows Service step?
So instead having only jar file in a package, I should have the rest of files as well?

According to https://www.baeldung.com/spring-boot-app-as-a-service, I believe you are correct.

And if it’s possible to substitute #{} variables in application.properties file which is included in executable jar - how to specify proper path so octopus step knew how to access a file for variables substitution inside jar file?

Hi @etrochimiuk,

Thanks for coming back to us.

I’ve tested this and got this working.

  1. I created a package that just contains an application.properties file that contained the below.
    image
  2. I created a Deploy a package step and enabled Substitute Variables in Files. I configured the step to use the package I created.
  3. I then added application.properties file to the Substitute Variables in Files part of the deployment.
  4. I created the Variable.
  5. Opened the application.properties file on the deployment and I saw the desired change.
    image
  6. You can specify any file in the package that you wish to run substitution on.

image

Please let me know how you get on and if I can help further.

Thanks

Derek

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.