@Library('utils')

def BRANCH_NAME = env.BRANCH_NAME

pipeline {
    agent {
	    kubernetes {
            defaultContainer 'pulumi'
	    	yamlFile 'jenkins/kubepods.yaml'
        }
    }

    environment {
        NODE_AUTH_TOKEN = credentials('NPM_ACCESS_TOKEN_READ_ONLY')
        S3_BUCKET = ".gpstrackit.com"
    }

    stages {

        stage('Install Dependencies') {
            steps {
                sh 'npm config set -- //registry.npmjs.org/:_authToken ${NODE_AUTH_TOKEN}'
                sh 'npm install --legacy-peer-deps'
            }
        }

        stage('Build And Deploy') {
            parallel {
                stage("DEV") {
                    when {
                        expression {
                            BRANCH_NAME == "dev"
                        }
                    }
                    steps {
                        sh "node -v"
                        sh "npm run build-storybook -- verbose"

                        container('aws-cli') {
                            withAwsAccount('sta') {
                                sh "aws s3 sync storybook-static s3://react-controls${S3_BUCKET} --delete"
                                sh """
                                    distribution_id=\$( \
                                    aws ssm get-parameter \
                                        --name "/cloudfront/distribution/react-controls${S3_BUCKET}/id" \
                                        --query Parameter.Value \
                                        --output text \
                                    )

                                    invalidation_id=\$( \
                                    AWS_MAX_ATTEMPTS=10 \
                                        aws cloudfront create-invalidation \
                                        --distribution-id \$distribution_id \
                                        --paths "/*" \
                                        --query "Invalidation.Id" \
                                        --output text \
                                    )

                                    aws cloudfront wait invalidation-completed \
                                    --distribution-id \$distribution_id \
                                    --id \$invalidation_id
                                """
                            }
                        }
                    }
                }
            }
        }
    }

    post {
        failure {
            slackNotif()
        }
        unstable {
            slackNotif()
        }
        fixed {
            slackNotif()
        }
    }
}

def slackNotif() {
  script {
    try {
        COLOR_MAP = ['SUCCESS': 'good', 'FAILURE': 'danger', 'UNSTABLE': 'danger', 'ABORTED': 'danger']

        slackSend(
            channel: '#jenkins-deployments',
            color: COLOR_MAP[currentBuild.currentResult],
            message: "*${currentBuild.currentResult}:* Job ${env.JOB_NAME} build ${env.BUILD_NUMBER} - ${env.BUILD_URL}"
        )
    } catch (Exception e) {
      e.printStackTrace()
      echo "skip failure slack notification: " + e.getMessage()
    }
  }
}
