image: atlassian/default-image:3
definitions:
  services:
    docker:
      memory: 2048

pipelines:
  default:
    - parallel:
        - step:
            name: 'Build and Test'
            script:
              - echo "Your build and test goes here..."
        - step:
            name: 'Lint'
            script:
              - echo "Your linting goes here..."
        - step:
            name: 'Security scan'
            script:
              - echo "Your security scan goes here..."
  branches:
    main:
      - step:
          name: Build production image
          services:
            - docker
          size: 2x
          script:
            - export PROD_IMAGE_TAG=$AZURE_SERVER/$PROJECT_NAME-backend:latest
            - docker login $AZURE_SERVER -u $AZURE_USER -p $AZURE_PASSWORD
            - docker build -t $PROD_IMAGE_TAG -f ./docker/prod/Dockerfile .
            - docker save --output salvatti_prod.docker $PROD_IMAGE_TAG
          artifacts:
            - salvatti_prod.docker
      - step:
          name: Deploy production image to registry
          services:
            - docker
          size: 1x
          script:
            - export PROD_IMAGE_TAG=$AZURE_SERVER/$PROJECT_NAME-backend:latest
            - docker load --input ./salvatti_prod.docker
            - docker login $AZURE_SERVER -u $AZURE_USER -p $AZURE_PASSWORD
            - docker push $PROD_IMAGE_TAG
      - step:
          name: Run application on server
          deployment: production
          trigger: 'automatic'
          script:
            - export PROD_IMAGE_TAG=$AZURE_SERVER/$PROJECT_NAME-backend:latest
            - export SSH_DIRECTORY=$SSH_DIRECTORY
            - export DOCKER_NETWORK=$DOCKER_NETWORK
            - apt update
            - apt install rsync -y
            - rsync -vzh docker-compose.prod.yaml $SSH_USER@$SSH_SERVER:~/$SSH_DIRECTORY/
            - ssh $SSH_USER@$SSH_SERVER "cd ~/$SSH_DIRECTORY/ && docker login $AZURE_SERVER -u $AZURE_USER -p $AZURE_PASSWORD && docker-compose -f docker-compose.prod.yaml -p $DOCKER_NETWORK down && docker pull $PROD_IMAGE_TAG && docker-compose -f docker-compose.prod.yaml -p $DOCKER_NETWORK up -d"
    base:
      - step:
          name: Build base image
          services:
            - docker
          size: 2x
          script:
            - export BASE_IMAGE_TAG=$AZURE_SERVER/$PROJECT_NAME-base-backend:latest
            - docker build -t $BASE_IMAGE_TAG -f ./docker/base/Dockerfile .
            - docker save --output salvatti_base.docker $BASE_IMAGE_TAG
          artifacts:
            - salvatti_base.docker
      - step:
          name: Deploy base image to registry
          services:
            - docker
          trigger: 'manual'
          size: 1x
          script:
            - export BASE_IMAGE_TAG=$AZURE_SERVER/$PROJECT_NAME-base-backend:latest
            - docker load --input ./salvatti_base.docker
            - docker login $AZURE_SERVER -u $AZURE_USER -p $AZURE_PASSWORD
            - docker push $BASE_IMAGE_TAG
    master:
      - step:
          name: Run application on server
          deployment: staging
          trigger: 'automatic'
          script:
            - export SSH_DIRECTORY=$SSH_DIRECTORY
            - apt update
            - apt install rsync -y
            - rsync -avzh . $SSH_USER@$SSH_SERVER:~/$SSH_DIRECTORY/
            - ssh $SSH_USER@$SSH_SERVER "docker login $AZURE_SERVER -u $AZURE_USER -p $AZURE_PASSWORD && cd ~/$SSH_DIRECTORY/ && docker-compose -f docker-compose.stage.yaml build && docker-compose -f docker-compose.stage.yaml up -d"