Commit 2508aea4 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Update GitLab CI pipeline for context and device services

parent 61f09437
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -8,4 +8,4 @@ stages:
include: 
  - local: '/src/monitoring/.gitlab-ci.yml'
  - local: '/src/context/.gitlab-ci.yml'
  - local: '/src/device/.gitlab-ci.yml'
+24 −63
Original line number Diff line number Diff line
# Build, tag, and push the Docker images to the GitLab Docker registry
build context:
  variables:
    IMAGE_NAME: 'context' # name of the microservice
    IMAGE_NAME_TEST: 'context-test' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)

# build the Docker image
build context:
  stage: build
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# tags the Docker image
tag context:
  stage: build
  script:
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# push the Docker image to the gitlab Docker registry
push context:
  stage: build
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker build -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/
    - docker tag "$IMAGE_NAME_TEST:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME_TEST:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME_TEST:$IMAGE_TAG"
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml


# test if the Docker image can be pulled from the gitlab registry
test context pull:
  stage: test
  needs:
    - push context
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# test if the Docker image can be executed
test context run:
# Pull, execute, and test the Docker image from the GitLab registry
test context:
  variables:
    IMAGE_NAME: 'context' # name of the microservice
    IMAGE_NAME_TEST: 'context-test' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: test
  needs:
    - build context
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi  
  script:
    - docker run -d -p 1010:1010 --name context --network=teraflowbridge --rm "$IMAGE_NAME:$IMAGE_TAG"
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker run -d -p 1010:1010 --name $IMAGE_NAME --network=teraflowbridge --rm "$IMAGE_NAME:$IMAGE_TAG"
    - docker ps > deploy_test_report.txt
    - docker run -d --name $IMAGE_NAME_TEST --network=teraflowbridge --rm "$IMAGE_NAME_TEST:$IMAGE_TAG" > pytest_report.txt
  after_script:
    - docker stop context
    - docker stop $IMAGE_NAME_TEST
    - docker stop $IMAGE_NAME
  rules:
    - changes:
      - src/$IMAGE_NAME/**
@@ -70,22 +47,6 @@ test context run:
    when: always
    paths:
      - deploy_test_report.txt
    expire_in: 1 day

# apply unit test to the context component
test context pytest:
  stage: test
  needs:
    - build context
  script:
    - docker build -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/ > pytest_report.txt
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml
  artifacts:
      when: always
      paths:
      - pytest_report.txt
    expire_in: 1 day

@@ -94,7 +55,7 @@ deploy context:
  stage: deploy
  needs:
    - build context
    - test context run
    - test context
  script:
    - kubectl apply -f "manisfests/contextservice.yaml"
  when: manual
+24 −63
Original line number Diff line number Diff line
# Build, tag, and push the Docker images to the GitLab Docker registry
build device:
  variables:
    IMAGE_NAME: 'device' # name of the microservice
    IMAGE_NAME_TEST: 'device-test' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)

# build the Docker image
build device:
  stage: build
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# tags the Docker image
tag device:
  stage: build
  script:
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# push the Docker image to the gitlab Docker registry
push device:
  stage: build
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$IMAGE_NAME:$IMAGE_TAG" -f ./src/$IMAGE_NAME/Dockerfile ./src/
    - docker tag "$IMAGE_NAME:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker build -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/
    - docker tag "$IMAGE_NAME_TEST:$IMAGE_TAG" "$CI_REGISTRY_IMAGE/$IMAGE_NAME_TEST:$IMAGE_TAG"
    - docker push "$CI_REGISTRY_IMAGE/$IMAGE_NAME_TEST:$IMAGE_TAG"
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml


# test if the Docker image can be pulled from the gitlab registry
test device pull:
  stage: test
  needs:
    - push device
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml

# test if the Docker image can be executed
test device run:
# Pull, execute, and test the Docker image from the GitLab registry
test device:
  variables:
    IMAGE_NAME: 'device' # name of the microservice
    IMAGE_NAME_TEST: 'device-test' # name of the microservice
    IMAGE_TAG: 'latest' # tag of the container image (production, development, etc)
  stage: test
  needs:
    - build device
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
    - if docker network list | grep teraflowbridge; then echo "teraflowbridge is already created"; else docker network create -d bridge teraflowbridge; fi  
  script:
    - docker run -d -p 1010:1010 --name device --network=teraflowbridge --rm "$IMAGE_NAME:$IMAGE_TAG"
    - docker pull "$CI_REGISTRY_IMAGE/$IMAGE_NAME:$IMAGE_TAG"
    - docker run -d -p 1010:1010 --name $IMAGE_NAME --network=teraflowbridge --rm "$IMAGE_NAME:$IMAGE_TAG"
    - docker ps > deploy_test_report.txt
    - docker run -d --name $IMAGE_NAME_TEST --network=teraflowbridge --rm "$IMAGE_NAME_TEST:$IMAGE_TAG" > pytest_report.txt
  after_script:
    - docker stop device
    - docker stop $IMAGE_NAME_TEST
    - docker stop $IMAGE_NAME
  rules:
    - changes:
      - src/$IMAGE_NAME/**
@@ -70,22 +47,6 @@ test device run:
    when: always
    paths:
      - deploy_test_report.txt
    expire_in: 1 day

# apply unit test to the device component
test device pytest:
  stage: test
  needs:
    - build device
  script:
    - docker build -t "$IMAGE_NAME_TEST:$IMAGE_TAG" -f ./src/$IMAGE_NAME/tests/Dockerfile ./src/ > pytest_report.txt
  rules:
    - changes:
      - src/$IMAGE_NAME/**
      - .gitlab-ci.yml
  artifacts:
      when: always
      paths:
      - pytest_report.txt
    expire_in: 1 day

@@ -94,7 +55,7 @@ deploy device:
  stage: deploy
  needs:
    - build device
    - test device run
    - test device
  script:
    - kubectl apply -f "manisfests/deviceservice.yaml"
  when: manual