def getRepo(){
            String name = "${env.JOB_NAME}";
            String[] value = name.split('/');
            return value[0];
}

def gitCredentials = "JenkinsGithub"


pipeline {
    parameters {
          booleanParam(defaultValue: true, description: 'Execute pipeline?', name: 'shouldBuild')
       }

  agent any
  stages {
        stage("Check if should build"){
        steps{
        script {
            result = sh (script: "git log -1 | grep '.*Jenkins version bump.*'", returnStatus: true)
            if (result == 0) {
                echo ("'Version bump' spotted in git commit. Aborting.")
                env.shouldBuild = "false"
            }
        }
}
        }
        stage('Build, bump version, and Publish NPM Package') {
            when {
                branch 'cleanup-and-styling'
                expression {
                    return env.shouldBuild != "false"
                }


            }
          steps {

              withCredentials([string(credentialsId: 'npm-token', variable: 'NPM_TOKEN')]) {
                              sshagent(credentials: ["${gitCredentials}"]){

                                sh '''
                                      npm version patch -m "Jenkins version bump"
                                      npm install
                                      npm run-script build
                                      git add .
                                      git commit -m "Jenkins version bump" | true
                                      git push origin cleanup-and-styling
                                      git push origin --tags
                                      echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" > .npmrc
                                      npm publish
                                '''


                                }

              }

          }
        }
        stage('Update Bnbservice repo'){
          when {
              branch 'cleanup-and-styling'
              expression {
                  return env.shouldBuild != "false"
              }
          }
          steps{
            dir('bnbservice'){
                  git(url: "git@bitbucket.org:bot-blockchain/bnbservice.git", branch: 'tiers', credentialsId: "${gitCredentials}")

                sshagent(credentials: ["${gitCredentials}"]){
                 sh '''
                    wait-for-package-replication -p ''' + getRepo() + '''
                    npm install ''' + getRepo() + '''@latest
                    git add .
                    git commit -m "Jenkins updating version of" ``` + getRepo() + ```
                    git push origin tiers
                    '''
              }
            }
        }
    }


        stage('Upload To S3') {
          when {
                branch 'cleanup-and-styling'
                expression {
                    return env.shouldBuild != "false"
                }

          }
          steps {
            withAWS(credentials: 'aws', region: 'us-east-1') {
              s3Upload(bucket: 'bnbservice.io', acl: 'PublicRead', workingDir: 'public/build/', path:'js/', includePathPattern: '**/*')
              cfInvalidate(distribution: 'E9S44VPPPCDMC', paths: ['/*'])
            }

          }

    }
  }
}
