node {
	checkout scm
	def json = readFile file:'package.json'
	def data = readJSON text: json;
    echo "version: ${data.version}"

	environment {
		GITUSER = credentials('bitbucket_creds')
	}

    docker.image('node:8').inside() {
        stage('Build and Publish') {
			sh '''
				git --version
				git remote -v
				git config --global push.default simple
				git config --global user.email "colin.clark@elephant-room.info"
				git config --global user.name "Colin Clark"
				git checkout develop
				git pull
				npm install
				npm run build
				npm version --no-git-tag-version patch --force
				git commit -am "Bump version update storybook-config.json"
				git remote -v
				git push https://colinclark:moobaaquack@bitbucket.org/colinclark/desops-comp-lib.git
				npm publish
			'''
        }
        stage('Build Styleguide') {
            sh 'npm run build-storybook'
        }
    }
    stage('Deploy s3') {
        withAWS(region:'eu-west-1', credentials:'aws_creds') {
            s3Upload(file:'storybook-static', path: data.version, bucket:'desops')
            s3Upload(file: 'storybook-config.json', path: 'storybook-config.json', bucket: 'desops')
        }
    }
}
