import groovy.transform.Field

@Field ECR_REGISTRY = '086679231553'
@Field ECR_REGION = 'us-east-1'
@Field DOCKER_IMAGE = "${ECR_REGISTRY}.dkr.ecr.${ECR_REGION}.amazonaws.com/chef-workstation:latest"

def call(Map args = [:]) {
  // Wipe the workspace so we are building completely clean
  deleteDir()
  checkout scm

  withEcr(registries: [[accountId: ECR_REGISTRY, region: ECR_REGION]]) {
      withSecrets(awsAccountId: '086679231553', awsRole: 'shared-chef-workstation-deploy-role', secrets: [
          [id: 'shared/chef-workstation/CLIENT_KEY', environmentVariable: 'CLIENT_KEY'],
      ]) {
          sh(
              script: """
              # Checking json syntax
              for file in \$(find . -type f -name "*.json");do
                  jq empty \${file};
              done
              # Checking ruby syntax and styles
              docker run --rm \
              -v \"\$(pwd):/repository\" \
              -w /repository \
              -e CLIENT_KEY \
              ${DOCKER_IMAGE} \
              cookstyle .
              """
          )
      }
  }
}
