node {
    stage("Setting variables") {
        echo "Setting variables"

        BRANCH_PRD = 'master'
        BRANCH_STG = 'staging'
        BRANCH_DEV = 'development'

        // Gets the environment based on the current Git branch.
        if (env.BRANCH_NAME == BRANCH_PRD) {
            env.DEPLOY_ENV = 'prd'
        } else if (env.BRANCH_NAME == BRANCH_STG) {
            env.DEPLOY_ENV = 'stg'
        } else if (env.BRANCH_NAME == BRANCH_DEV) {
            env.DEPLOY_ENV = 'dev'
        }

        // Define environment variables for other files to use it.
        env.APP_NAME = "nemt-portal-api"
        env.ENV_NAME = "${env.APP_NAME}-${env.DEPLOY_ENV}"

        echo "Branch: ${env.BRANCH_NAME}"
        echo "Environment: ${env.DEPLOY_ENV}"

        echo "Setting variables complete"
    }
    stage("Cleaning") {
        echo "Cleaning"

        sh "rm -rf ${pwd()}/*"
        deleteDir()
        
        echo "Cleaning complete"
    }
    stage("Build") {
        echo "Building"

        // Sets package name based on the AWS job name, the current Git tag version and the build ID.
        GIT_TAG       = "\$(git describe --tags --always --dirty)"
        PKG_NAME      = "${GIT_TAG}-${env.JOB_BASE_NAME}-${env.BUILD_ID}"
        ZIP_FILE_NAME = "${PKG_NAME}.zip"

        // Sets the variables used for Docker login.
        AWS_DOCKER_ROLE_NAME = "rvbrazil-bsbsi-deploy"
        AWS_DOCKER_ROLE_ARN  = "arn:aws:iam::xxxxxxxxxxx:role/bsbsi-deploy"

        // Downloads repository.
        echo "Downloading repository"
        checkout scm

        // Assumes role into our AWS account (for Docker login).
        sh "aws-assume-role ${AWS_DOCKER_ROLE_ARN} ${AWS_DOCKER_ROLE_NAME}"

        // Uses the AWS CLI to get our Docker login commmand and executes it.
        sh "\$(aws --region us-east-1 --profile ${AWS_DOCKER_ROLE_NAME} ecr get-login)"

        // Builds the application.
        sh "make build-${env.DEPLOY_ENV}"

        // Zips the files that will go to S3.
        sh "cp Dockerfile.run Dockerfile"
        sh "rm -rf ./*.zip"
        sh "zip -r ${ZIP_FILE_NAME} dist Dockerfile Dockerrun.aws.json"
        
        echo "Building complete"
    }
    stage("Deploy") {
        echo "Deploying"

        echo "Environment: ${env.DEPLOY_ENV}"

        // Sets the variables used for deploying.
        S3_BUCKET     = "boilerplate-builds"
        S3_KEY        = "${env.APP_NAME}/${env.DEPLOY_ENV}/${ZIP_FILE_NAME}"
        S3_PATH       = "${S3_BUCKET}/${S3_KEY}"
        AWS_ROLE_NAME = "qrides-bsbsi-deploy"
        AWS_ROLE_ARN  = "arn:aws:iam::xxxxxxxxxx:role/bsbsi-deploy"
        
        // Assumes role into our AWS account (for S3 deploy).
        sh "aws-assume-role ${AWS_ROLE_ARN} ${AWS_ROLE_NAME}"

        // Deploys container to S3.
        sh "aws --region sa-east-1 --profile ${AWS_ROLE_NAME} s3 mv ${ZIP_FILE_NAME} s3://${S3_PATH}"

        // Deploys to EBS.
        sh "aws --region sa-east-1 --profile ${AWS_ROLE_NAME} elasticbeanstalk \
            create-application-version \
            --application-name \"${env.APP_NAME}\" \
            --version-label \"${PKG_NAME}\" \
            --description \"${ZIP_FILE_NAME}\" \
            --source-bundle S3Bucket=\"${S3_BUCKET}\",S3Key=\"${S3_KEY}\""

        // Updates EBS environment.
        sh "aws --region sa-east-1 --profile ${AWS_ROLE_NAME} elasticbeanstalk \
            update-environment \
            --application-name \"${env.APP_NAME}\" \
            --environment-name \"${env.ENV_NAME}\" \
            --version-label \"${PKG_NAME}\""

        echo "Deploying complete"
    }
}
