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

    docker.image('node:8').inside('-u root') {
        stage('Build and Publish') {
            sh 'git config --global user.email "colin.clark@elephant-room.info"'
            sh 'git config --global user.name "Colin Clark"'
            sh 'npm install'
            sh 'npm run build'
            sh 'npm version --no-git-tag-version patch --force'
            sh '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: '', bucket: 'desops')
        }
    }
}
