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 MICRO_SERVICE: frontend SERVICE_VERSION: "1.3" APP_PORT: 3000 stages: - build - deploy build: stage: build tags: - group - shell before_script: - 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 deploy: stage: deploy tags: - group - shell 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