Search code examples
gitlabyamlgitlab-cicicd

How to run commands in parallel Gitlab CI/CD pipeline?


I have a test command in my repo that should work when my server is up because the tests interact with the server once it is running. On my local i use two commands on first terminal npm run dev - this gets the server running and on second terminal i run the command npm run test that runs test which only pass when the first command is running. How do i achieve this in my gitlab CICD test stage job? currently i am doing this

test_job:
    stage: test
    script:
        - npm run dev
        - npm run test

so the pipeline executes npm run dev which doesnt self terminate and my pipeline gets stuck cant seem to find the solution. Help and suggestions are appreciated. Stack is typescript express graphql


Solution

  • You have two options that you can use for this:

    1. If you're building your server into a container prior to this test job, you can run the container as a service, which will allow you to access it via it's service alias. That would look something like this:
    test_job:
      stage: test
      services:
        - name: my_test_container:latest
          alias: test_container
      script:
        - npm run test # should hit http://test_container:<port> to access service
    
    1. You can use the nohup linux command to run your service in the background. This will run the command in the background once it's started up, and it will die when the CI job ends (as part of shutting down the job). That would look like this:
    test_job:
      stage: test
      script:
        - nohup npm run dev &
        - npm run test