image: atlassian/default-image:3

definitions:
  services:
    docker:
      memory: 2048
  steps:
    - step: &build-push
        name: Build and push
        runs-on: 
          - self.hosted
          - linux
          - apps
        script:
          # - tag=$(git tag --points-at HEAD)
          # - if [[ $BITBUCKET_BRANCH == "qa-"* ]]; then ENVIRONMENT=qa  ; else ENVIRONMENT=$BITBUCKET_BRANCH ; fi
          - ENVIRONMENT=development
          - APP_TYPE=pci-apps
          - APP_DIVISION=pci
          - echo "$HARBOR_ROBOT_PASSWORD" | docker login $HARBOR_REPO_URL -u $HARBOR_ROBOT_USERNAME --password-stdin 
          - BITBUCKET_COMMIT_SHORT="${BITBUCKET_COMMIT::7}"
          - echo $ID_RSA | base64 -d > id_rsa
          - docker build -f Dockerfile.php-fpm -t harbor-repo.paynetics.io/$APP_TYPE/$APP_DIVISION/$BITBUCKET_REPO_SLUG/$BITBUCKET_REPO_SLUG-$ENVIRONMENT:$BITBUCKET_COMMIT_SHORT .
          - docker push harbor-repo.paynetics.io/$APP_TYPE/$APP_DIVISION/$BITBUCKET_REPO_SLUG/$BITBUCKET_REPO_SLUG-$ENVIRONMENT:$BITBUCKET_COMMIT_SHORT
        services:
          - docker  
    - step: &trigger-helm-deploy
        name: Trigger Helm Deploy
        runs-on: 
          - self.hosted
          - linux
          - apps
        script:
          - BITBUCKET_COMMIT_SHORT="${BITBUCKET_COMMIT::7}"
          # - if [[ $BITBUCKET_BRANCH == "qa-"* ]]; then ENVIRONMENT="qa"  ; else ENVIRONMENT="$BITBUCKET_BRANCH" ; fi
          - ENVIRONMENT=development
          - APP_TYPE=pci-apps
          - APP_DIVISION=pci
          - pipe: atlassian/trigger-pipeline:5.1.3
            variables:
              BITBUCKET_USERNAME: $BITBUCKET_USERNAME
              BITBUCKET_APP_PASSWORD: $BITBUCKET_APP_PASSWORD
              REPOSITORY: 'helm-charts'
              REF_TYPE: 'branch'
              REF_NAME: 'dockerize-symfony'
              CUSTOM_PIPELINE_NAME: 'push-deploy'
              PIPELINE_VARIABLES: >
                  [{
                    "key": "DOCKER_TAG",
                    "value": "$BITBUCKET_COMMIT_SHORT"
                  },
                  {
                    "key": "APP_REPO",
                    "value": "$BITBUCKET_REPO_SLUG",
                  },
                  {
                    "key": "ENVIRONMENT",
                    "value": "$ENVIRONMENT",
                  },
                  {
                    "key": "APP_TYPE",
                    "value": "$APP_TYPE",
                  },
                  {
                    "key": "APP_DIVISION",
                    "value": "$APP_DIVISION",
                  },
                  {
                    "key": "REMOTE_BRANCH",
                    "value": "$BITBUCKET_BRANCH",
                  },
                  {
                    "key": "APP_VERSION",
                    "value": "1.0.0",
                  }]
              WAIT: 'true'
        
pipelines:
  branches:
    dockerize:
      - step: *build-push
      - step: *trigger-helm-deploy
    development:
      - step: *build-push
      - step: *trigger-helm-deploy
    staging:
      - step: *build-push
      # - step: *trigger-helm-deploy
    production:
      - step: *build-push
      # - step: *trigger-helm-deploy
    # qa-**:
    #   - step: *build-push
    #   - step: *trigger-helm-deploy
