pipeline {
	agent {
        kubernetes {
            yamlFile 'jenkins/kubernetesPod.yaml'
        }
    }

    environment {
        BRANCH_BASE_NAME = "${env.GIT_BRANCH}".tokenize('/').last()
    }

	stages {
        stage("Create Release Tag") {
            steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }

                withCredentials([
                    usernamePassword(
                        credentialsId: 'global_gitlabcloud_login',
                        usernameVariable: 'GIT_USERNAME',
                        passwordVariable: 'GIT_PASSWORD'
                    )
                ]){
                    sh 'git config --global user.email "jshaikh@digite.com"'
                    sh 'git config --global user.name "Jishan Shaikh"'
                    sh 'git tag ${RELEASE_VERSION} -m "Created release tag from master branch"'
                    sh 'git push -f https://${GIT_USERNAME}:${GIT_PASSWORD}@${REPO} --tags'
                }   
            }
        }

        stage("Change Dev Version") {
            steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }

                withCredentials([
                    usernamePassword(
                        credentialsId: 'global_gitlabcloud_login',
                        usernameVariable: 'GIT_USERNAME',
                        passwordVariable: 'GIT_PASSWORD'
                    )
                ]){
                    sh 'git config --global user.name "Jishan Shaikh"'
                    sh 'git config --global user.email "jshaikh@digite.com"'
                    sh 'git checkout dev'
                    sh 'git add package.json'
                    sh 'git commit -m "Changed dev version for release"'
                    sh 'git push -f https://${GIT_USERNAME}:${GIT_PASSWORD}@${REPO}'
                    sh 'git checkout master'
                    sh 'git merge dev'
                    sh 'git push -f https://${GIT_USERNAME}:${GIT_PASSWORD}@${REPO}'
                }   
            }
        }

        stage('Dependencies Install') {
			steps {   
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }

			    container('node-js') {
			        withCredentials([
                        usernamePassword(
                            credentialsId: 'global_gitlabcloud_login',
                            usernameVariable: 'USERNAME',
                            passwordVariable: 'PASSWORD'
                        ), 
                        string(credentialsId: 'nexus-npm-repo-token', variable: 'NEXUS_TOKEN')
                    ]) {
                        sh 'sed -i "s/<auth token>/${NEXUS_TOKEN}/g" .npmrc'
                        sh 'npm install'
                        sh 'npm install --save-dev jest-junit'
			        }
				}
			}
		}

		stage('Unit Automation') {
			steps { 
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }

			    container('node-js') {
				    sh 'npm test -- --ci --testResultsProcessor="jest-junit"'
			    }
			}
		}

        stage('Build') {
			steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }
 
			    container('node-js') {
				    sh 'CI=false npm run build'
			    }
			}
		}
    }

    post {
        failure {
            googlechatnotification url: "${Swiftalk_Chat_URL}",
                message: "Release pipeline of job ${env.JOB_NAME} for build ${env.BUILD_NUMBER} is failed - ${env.BUILD_URL}console at stage `${FAILED_STAGE}`"
        }
        fixed {
            googlechatnotification url: "${Swiftalk_Chat_URL}",
                message: "Release pipeline of job ${env.JOB_NAME} in build ${env.BUILD_NUMBER} is fixed - ${env.BUILD_URL}console"
        }
        success {
            googlechatnotification url: "${Swiftalk_Chat_URL}",
                message: "New Version ${RELEASE_VERSION} Released for Blox - ${JOB_URL}${BUILD_NUMBER}/console"
        }
    }
}
