6

in my GitHub action yaml file, I have two commands at the end. The first one is yarn start (which starts the server) and the second one is for running a test file.

from my local server I usually run yarn start, then wait until frontend and backend port to be run, then only I run the test from another terminal

but from GitHub action, it runs the yarn start command then immediately runs the test script, so when the test file is run, server is not listening on port. thats why my test scripts are failed. how can I ensure that the test script will run after the yarn start is finished?

here is my action.yml file

name: "Github Actions Test"
on:
  push:
    branches:
      - wip/checkout2

jobs:
  test:
    runs-on: ubuntu-latest

    env:
      PRISMA_ENDPOINT: ${{secrets.PRISMA_ENDPOINT}}
      PRISMA_SECRET: ${{secrets.PRISMA_SECRET}}

    steps:
      - uses: actions/checkout@v1
      - name: "Install Node"
        uses: actions/setup-node@v1
        with:
          node-version: "12.x"
      - name: "Install global packages"
        run: npm install -g yarn prisma-cli concurrently mocha
      - name: "Run docker Container"
        run: docker-compose -f docker-compose.yml up --build -d
      - name: "Install deps"
        run: yarn install
      - name: "prisma deploy"
        run: yarn deploy:backend
      - name: "Seed Backend"
        run: yarn seed:backend
      - name: "Build app"
        run: yarn build
      - name: "Start backend and frontend concurrently on background and run tests"
        run: |
          yarn start &
          yarn test

Ashik
  • 2,888
  • 8
  • 28
  • 53

3 Answers3

3

Another option is wait-on

./node_modules/.bin/wait-on tcp:3000
Daniel
  • 6,194
  • 7
  • 33
  • 59
2

You will need to do one of these:

Option 1: Wait for a few seconds before running tests:

run: |
  yarn start &
  sleep 10
  yarn test

Option 2: Wait for the port to be open with some utility designed for this purpose. Maybe wait-port (untested)

Option 3: Wait for the port to be open using native linux tools - example 1, example 2.

DannyB
  • 12,810
  • 5
  • 55
  • 65
2

Similar to what @DannyB is suggesting, you could test the server is running properly in the background by waiting some seconds and then using curl to test network connectivity.

For example:

- name: "Start backend and frontend concurrently on background and run tests"
  run: |
    yarn start &
    sleep 10 &&
    curl http://localhost:8000 &&
    yarn test

This way you can check the workflow job's log and confirm the server was up and running before executing tests.

If the connection is established, curl http://localhost:<PORT> will return the content of the web page by default. You can also add a -I at the end of the command to make sure to return only the request header and check if it has a HTTP/1.0 200 OK status.

tokto
  • 116
  • 5
  • The & - Will run ain background. What && mean? – Misha Apr 21 '22 at 12:06
  • @Misha, this stackoverflow question has some really great answers: https://stackoverflow.com/questions/4510640/what-is-the-purpose-of-in-a-shell-command – tokto Apr 22 '22 at 08:53