Sachin Singh
Sachin Singh

Reputation: 423

How do I write pre-deployment scripts to run newman postman test in .gitlab-ci.yml file

I want to set up pre-deployment postman test using newman in gitlab-ci.yml, if test pass then it will deploy if the test fails then deploy will be cancelled.

this is my .gitlab-ci.yml file -

variables:
        POSTMAN_COLLECTION : collection.json
    POSTMAN_ENVIRONMENT: postman-test.postman_environment.json

stages:
    - build
    - test

build:
    stage: build

    script:
      - docker build -t registry.tech5-sa.com/t5-platform/t5-templates-db_app:$CI_COMMIT_REF_NAME --build-arg UID=$(id -u) -f API/Dockerfile .
      - docker build -t registry.tech5-sa.com/t5-platform/t5-templates-db_postgresql:$CI_COMMIT_REF_NAME --build-arg UID=$(id -u) -f API/Dockerfile.postgresql .
      - docker push registry.tech5-sa.com/t5-platform/t5-templates-db_app:$CI_COMMIT_REF_NAME
      - docker push registry.tech5-sa.com/t5-platform/t5-templates-db_postgresql:$CI_COMMIT_REF_NAME

    tags:
        - t5 
        - t5-templates-db


stage: test
        image: 
            name: postman/newman_alpine33
            entrypoint:[""]

        before_script: 
            - docker login -u "$CI_REGISTRY_USER" -P "$CI_REGISTRY_PASSWORD" $CI_REGISTRY

        script: 
            - docker-compose up
            - newman --version
            - newman run ${POSTMAN_COLLECTION} -e ${POSTMAN_ENVIRONMENT}

Upvotes: 1

Views: 1081

Answers (1)

julste
julste

Reputation: 351

I think you are on the right way. Just add another stage e.g. deploy like this:

stages:
  - build
  - test
  - deploy

build:
  stage: build
  script: echo "Building the app"

test:
  stage: test
  script: echo "Running tests"

deploy:
  stage: deploy
  script: echo "Deploying the app"

The jobs assigned to these stages will run in this order. If any job fails, then the pipeline fails and jobs that are assigned to the next stage won’t run.

See more here

Upvotes: 1

Related Questions