Vous êtes autorisé à écrire des objets dans S3.
Confirmé pour travailler avec les versions suivantes
bash:commander:
aws --version
Résultat (exemple):
aws-cli/1.11.70 Python/2.7.12 Linux/4.4.11-23.53.amzn1.x86_64 botocore/1.5.33
Si la version est ancienne, effectuez la mise à jour vers la dernière version.
bash:commander:
sudo -H pip install -U awscli
Commencez par vérifier les variables.
bash:Vérifier les 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
Résultat (exemple):
AWS_DEFAULT_PROFILE: (0.1) <Profil avec autorisation d'écrire des objets dans S3> 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
Si la variable n'est pas incluse ou n'est pas appropriée, travaillez sur chaque numéro d'étape Je le ferai.
Consultez la liste des profils.
bash:commander:
cat ~/.aws/credentials \
| grep '\[' \
| sed 's/\[//g' | sed 's/\]//g'
Résultat (exemple):
iamFull-prjz-mbpr13
<Profil avec autorisation d'écrire des objets sur S3>
bash:Paramètres variables:
export AWS_DEFAULT_PROFILE='<Profil avec l'autorisation d'écrire des objets dans S3>'
bash:Paramètres variables:
export AWS_DEFAULT_REGION='ap-northeast-1'
bash:Paramètres variables:
S3_BUCKET_NAME=$( \
aws s3 ls \
| grep 'source-ap-northeast-1-' \
| cut -d ' ' -f3 \
) \
&& echo ${S3_BUCKET_NAME}
bash:Paramètres variables:
FILE_ZIP='MessageUtil.zip'
bash:Paramètres variables:
SNS_TOPIC_PREFIX='handson-'
bash:Paramètres variables:
SNS_TOPIC_ARN=$( \
aws sns list-topics \
--query "Topics[?contains(TopicArn, \`${SNS_TOPIC_PREFIX}\`)].TopicArn" \
--output text \
) \
&& echo ${SNS_TOPIC_ARN}
Résultat (exemple):
arn:aws:sns:ap-northeast-1:XXXXXXXXXXXX:handson-20170417
bash:Paramètres variables:
DIR_WORK='${HOME}/src-codebuild-demo-java'
bash:commander:
cd ${DIR_WORK}/
bash:Vérifier les 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
Résultat (exemple):
AWS_DEFAULT_PROFILE: (0.1) <Profil avec autorisation d'écrire des objets dans S3> 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
bash:Paramètres variables:
SNS_MSG_SUBJECT='AWS CodeBuild - Build Completed'
bash:Paramètres variables:
SNS_MSG_BODY="${CODEB_PROJECT_NAME} build has completed." \
&& echo ${SNS_MSG_BODY}
bash:Paramètres variables:
FILE_INPUT="${DIR_WORK}/buildspec.yml"
bash:Vérifier les 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:commander:
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}
bash:commander:
PATH_ZIP="${HOME}/${FILE_ZIP}" \
&& echo ${PATH_ZIP}
bash:commander:
zip -r ${PATH_ZIP} * -x '.*'
Résultat (exemple):
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%)
Vérifiez le contenu du fichier ZIP créé.
bash:commander:
unzip -Z ${PATH_ZIP}
Résultat (exemple):
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%
Transférez le fichier ZIP créé dans le compartiment S3 pour le fichier source.
bash:commander:
aws s3 cp ${PATH_ZIP} s3://${S3_BUCKET_NAME}/
Résultat (exemple):
upload: ../MessageUtil.zip to s3://source-ap-northeast-1-XXXXXXXXXXXX/MessageUtil.zip
bash:commander:
aws s3 ls s3://${S3_BUCKET_NAME}/${FILE_ZIP}
Résultat (exemple):
2017-04-1701:23:45 3243 MessageUtil.zip
bash:commander:
rm ${PATH_ZIP}