workflow: rules: - if: $CI_COMMIT_BRANCH != "main" && $CI_PIPELINE_SOURCE != "merge_request_event" when: never - when: always variables: DEPLOYMENT_SERVER_HOST: "server" APP_ENDPOINT: http://server:3000 stages: - build - deploy .build: stage: build tags: - remote - shell variables: MICRO_SERVICE: "" SERVICE_VERSION: "" before_script: - cd $MICRO_SERVICE - export IMAGE_NAME=$CI_REGISTRY_IMAGE/microservice/$MICRO_SERVICE - export IMAGE_TAG=$SERVICE_VERSION script: - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY - docker build -t $IMAGE_NAME:$IMAGE_TAG . - docker push $IMAGE_NAME:$IMAGE_TAG build_frontend: extends: .build variables: MICRO_SERVICE: frontend SERVICE_VERSION: "1.3" only: changes: - "frontend/**/*" build_products: extends: .build variables: MICRO_SERVICE: products SERVICE_VERSION: "1.8" only: changes: - "products/**/*" build_shopping_cart: extends: .build variables: MICRO_SERVICE: shopping-cart SERVICE_VERSION: "2.1" only: changes: - "shopping-cart/**/*" .deploy: stage: deploy tags: - remote - shell variables: MICRO_SERVICE: "" SERVICE_VERSION: "" APP_PORT: "" before_script: - chmod 400 $SSH_PRIVATE_KEY - export IMAGE_NAME=$CI_REGISTRY_IMAGE/microservice/$MICRO_SERVICE - export IMAGE_TAG=$SERVICE_VERSION script: - scp -o StrictHostKeyChecking=no -i $SSH_PRIVATE_KEY ./docker-compose.yaml ubuntu@$DEPLOYMENT_SERVER_HOST:~ - ssh -o StrictHostKeyChecking=no -i $SSH_PRIVATE_KEY ubuntu@$DEPLOYMENT_SERVER_HOST " docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY && export COMPOSE_PROJECT_NAME=$MICRO_SERVICE && export DC_IMAGE_NAME=$IMAGE_NAME && export DC_IMAGE_TAG=$IMAGE_TAG && export DC_APP_PORT=$APP_PORT && docker network create micro_service || true && docker-compose down && docker-compose up -d" environment: name: development url: $APP_ENDPOINT deploy_frontend: extends: .deploy variables: MICRO_SERVICE: frontend SERVICE_VERSION: "1.3" APP_PORT: 3000 only: changes: - "frontend/**/*" deploy_products: extends: .deploy variables: MICRO_SERVICE: products SERVICE_VERSION: "1.8" APP_PORT: 3001 only: changes: - "products/**/*" deploy_shopping_cart: extends: .deploy variables: MICRO_SERVICE: shopping-cart SERVICE_VERSION: "2.1" APP_PORT: 3002 only: changes: - "shopping-cart/**/*"