[JAWS-UG CLI] CodeBuild: # 3 Creating buildspec (Java)

Prerequisites

Privilege to S3

You have permission to write objects to S3.

AWS CLI version

Operation confirmed with the following versions

bash:command:


aws --version

Result (example):

  aws-cli/1.11.70 Python/2.7.12 Linux/4.4.11-23.53.amzn1.x86_64 botocore/1.5.33

If the version is old, update to the latest version.

bash:command:


sudo -H pip install -U awscli
  1. Preparation =======

First, check the variables.

bash:Check variables:


cat << ETX

        AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:  (0.2) ${AWS_DEFAULT_REGION}
        S3_BUCKET_NAME       (0.3) ${S3_BUCKET_NAME}
        FILE_ZIP             (0.4) ${FILE_ZIP}
        SNS_TOPIC_NAME       (0.5) ${SNS_TOPIC_NAME}
        DIR_WORK             (0.6) ${DIR_WORK}
        PWD                  (0.7) $(pwd)

ETX

Result (example):

AWS_DEFAULT_PROFILE: (0.1) AWS_DEFAULT_REGION: (0.2) ap-northeast-1 S3_BUCKET_NAME (0.3) source-ap-northeast-1-XXXXXXXXXXXX FILE_ZIP (0.4) MessageUtil.zip SNS_TOPIC_NAME (0.5) handson-20170417 DIR_WORK (0.6) ${HOME}/src-codebuild-demo-java PWD (0.7) ${HOME}/src-codebuild-demo-java

If the variable is not included or is not appropriate, work on each step number I will do it.

0.1. Profile specification

Check the list of profiles.

bash:command:


cat ~/.aws/credentials \
       | grep '\[' \
       | sed 's/\[//g' | sed 's/\]//g'

Result (example):

  iamFull-prjz-mbpr13

bash:Variable setting:


export AWS_DEFAULT_PROFILE='<Profile with permission to write objects to S3>'

0.2. Specifying the region

bash:Variable setting:


export AWS_DEFAULT_REGION='ap-northeast-1'

0.3. Specifying S3 bucket for source code

bash:Variable setting:


S3_BUCKET_NAME=$( \
        aws s3 ls \
          | grep 'source-ap-northeast-1-' \
          | cut -d ' ' -f3 \
) \
        && echo ${S3_BUCKET_NAME}

0.4. Specifying the ZIP file name

bash:Variable setting:


FILE_ZIP='MessageUtil.zip'

0.5. Specifying SNS topics for notifications

bash:Variable setting:


SNS_TOPIC_PREFIX='handson-'

bash:Variable setting:


SNS_TOPIC_ARN=$( \
        aws sns list-topics \
          --query "Topics[?contains(TopicArn, \`${SNS_TOPIC_PREFIX}\`)].TopicArn" \
          --output text \
) \
        && echo ${SNS_TOPIC_ARN}

Result (example):

  arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:handson-20170417

0.6. Specifying a working directory

bash:Variable setting:


DIR_WORK='${HOME}/src-codebuild-demo-java'

0.7. Move to working directory

bash:command:


cd ${DIR_WORK}/

final confirmation

bash:Check variables:


cat << ETX

        AWS_DEFAULT_PROFILE: (0.1) ${AWS_DEFAULT_PROFILE}
        AWS_DEFAULT_REGION:  (0.2) ${AWS_DEFAULT_REGION}
        S3_BUCKET_NAME       (0.3) ${S3_BUCKET_NAME}
        FILE_ZIP             (0.4) ${FILE_ZIP}
        SNS_TOPIC_NAME       (0.5) ${SNS_TOPIC_NAME}
        DIR_WORK             (0.6) ${DIR_WORK}
        PWD                  (0.7) $(pwd)

ETX

Result (example):

AWS_DEFAULT_PROFILE: (0.1) AWS_DEFAULT_REGION: (0.2) ap-northeast-1 S3_BUCKET_NAME (0.3) source-ap-northeast-1-XXXXXXXXXXXX FILE_ZIP (0.4) MessageUtil.zip SNS_TOPIC_NAME (0.5) handson-20170417 DIR_WORK (0.6) ${HOME}/src-codebuild-demo-java PWD (0.7) ${HOME}/src-codebuild-demo-java

  1. Pre-work ===========

1.1. Specifying the message title

bash:Variable setting:


SNS_MSG_SUBJECT='AWS CodeBuild - Build Completed'

1.2. Specifying the message body

bash:Variable setting:


SNS_MSG_BODY="${CODEB_PROJECT_NAME} build has completed." \
        && echo ${SNS_MSG_BODY}
  1. Create the Build Spec ========================

bash:Variable setting:


FILE_INPUT="${DIR_WORK}/buildspec.yml"

bash:Check variables:


cat << ETX

        FILE_INPUT:         ${FILE_INPUT}
        AWS_DEFAULT_REGION: ${AWS_DEFAULT_REGION}
        SNS_TOPIC_ARN:      ${SNS_TOPIC_ARN}
        SNS_MSG_SUBJECT:    ${SNS_MSG_SUBJECT}
        SNS_MSG_BODY:       ${SNS_MSG_BODY}

ETX

bash:command:


cat << EOF > ${FILE_INPUT}
version: 0.1

environment_variables:
  plaintext:
    AWS_DEFAULT_REGION: "${AWS_DEFAULT_REGION}"
    SNS_TOPIC_ARN: "${SNS_TOPIC_ARN}"
    SNS_MSG_SUBJECT: "${SNS_MSG_SUBJECT}"
    SNS_MSG_BODY: "${SNS_MSG_BODY}"

phases:
  install:
    commands:
      - echo Nothing to do in the install phase...
  pre_build:
    commands:
      - echo Nothing to do in the pre_build phase...
  build:
    commands:
      - echo Build started on \$(date +%Y-%m-%dZ%H:%M:%S)
      - mvn install
  post_build:
    commands:
      - echo Build completed on \$(date +%Y-%m-%dZ%H:%M:%S)
      - aws sns publish --topic-arn \${SNS_TOPIC_ARN} --subject "\${SNS_MSG_SUBJECT}" --message "\${SNS_MSG_BODY}"
artifacts:
  files:
    - target/messageUtil-1.0.jar
EOF

cat ${FILE_INPUT}

2.2. Creating a ZIP file

bash:command:


PATH_ZIP="${HOME}/${FILE_ZIP}" \
        && echo ${PATH_ZIP}

bash:command:


zip -r ${PATH_ZIP} * -x '.*'

Result (example):

  adding: buildspec.yml (deflated 56%)
  adding: pom.xml (deflated 52%)
  adding: src/ (stored 0%)
  adding: src/test/ (stored 0%)
  adding: src/test/java/ (stored 0%)
  adding: src/test/java/TestMessageUtil.java (deflated 60%)
  adding: src/main/ (stored 0%)
  adding: src/main/java/ (stored 0%)
  adding: src/main/java/MessageUtil.java (deflated 58%)

Check the contents of the created ZIP file.

bash:command:


unzip -Z ${PATH_ZIP}

Result (example):

  Archive:  /home/taro/MessageUtil.zip
  Zip file size: 2305 bytes, number of entries: 9
  -rw-rw-r--  3.0 unx      418 tx defN 17-Apr-16 23:45 buildspec.yml
  -rw-rw-r--  3.0 unx      625 tx defN 17-Apr-16 12:37 pom.xml
  drwxrwxr-x  3.0 unx        0 bx stor 17-Apr-16 12:36 src/
  drwxrwxr-x  3.0 unx        0 bx stor 17-Apr-16 12:36 src/test/
  drwxrwxr-x  3.0 unx        0 bx stor 17-Apr-16 12:37 src/test/java/
  -rw-rw-r--  3.0 unx      568 tx defN 17-Apr-16 12:37 src/test/java/TestMessageUtil.java
  drwxrwxr-x  3.0 unx        0 bx stor 17-Apr-16 12:36 src/main/
  drwxrwxr-x  3.0 unx        0 bx stor 17-Apr-16 12:37 src/main/java/
  -rw-rw-r--  3.0 unx      345 tx defN 17-Apr-16 12:37 src/main/java/MessageUtil.java
  9 files, 1956 bytes uncompressed, 863 bytes compressed:  55.9%

2.3. Transfer to S3 bucket

Transfer the created ZIP file to the S3 bucket for the source file.

bash:command:


aws s3 cp ${PATH_ZIP} s3://${S3_BUCKET_NAME}/

Result (example):

  upload: ../MessageUtil.zip to s3://source-ap-northeast-1-XXXXXXXXXXXX/MessageUtil.zip
  1. Post-work ===========

3.1. Confirmation of transfer completion

bash:command:


aws s3 ls s3://${S3_BUCKET_NAME}/${FILE_ZIP}

Result (example):

  2017-04-1701:23:45       3243 MessageUtil.zip

3.2. Delete ZIP file

bash:command:


rm ${PATH_ZIP}

Done

Recommended Posts

[JAWS-UG CLI] CodeBuild: # 3 Creating buildspec (Java)
[JAWS-UG CLI] CodeBuild: # 1 Creating Source Code (Java)
Creating ElasticSearch index from Java