Home > other >  How to run Spring Boot server in background GH Actions?
How to run Spring Boot server in background GH Actions?

Time:10-30

In my workflow I have defined a step:

    - name: Setup Spring Boot App
      run: |
         cd backend/MeetApp
         POSTGRES_USERNAME=postgres POSTGRES_PASSWORD=postgres nohup ./gradlew bootRun

No matter what I try I can't figure out how to start my Spring Boot process, potentially wait for specific time, and then go to the next steps.

CodePudding user response:

TL;DR

Run it in the background with &

Details

GitHub Actions lets you run a command in the background with the & at the end of the line.

For example, this dummy step:

  - name: does background work?
    run: |
      echo start && sleep 3 && echo stop &
      echo w1 && sleep 1 && echo w2 && sleep 1 && echo w3 && sleep 1 && echo w4

ends up printing, in order: w1, start, w2, w3, stop, w4.

In your case, you would need:

  - name: Setup Spring Boot App
    run: |
      cd backend/MeetApp
      POSTGRES_USERNAME=postgres POSTGRES_PASSWORD=postgres nohup ./gradlew bootRun &

And then -- I've tested this -- you can put the command that uses that background process in the same step or in subsequence steps, it'll work either way.

Waiting for it

Now, additional details from the comments, you need to wait long enough before starting to use this background process in the next CI steps. A simple sleep 5 (or choose your time) works, but if you have use a dummy query that will succeed only once the background process is booted up, doing it in a loop every second up to a maximum delay will a) wait the shortest time necessary and b) make sure you always wait long enough.

Here's an example that waits at most 20 seconds:

  - name: Setup Spring Boot App
    run: |
      cd backend/MeetApp
      POSTGRES_USERNAME=postgres POSTGRES_PASSWORD=postgres nohup ./gradlew bootRun &
      for attempt in {1..20}; do sleep 1; if <test-command>; then echo ready; break; fi; echo waiting...; done 

That <test-command> should exit 0 if gradlew has booted, non-zero otherwise. With this loop, CI will not move on to the next step until the boot is complete.

  • Related