Skip to content

Instantly share code, notes, and snippets.

@giehlman
Created November 21, 2017 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giehlman/c8ef2f4b188fdb4573424bd868c728d7 to your computer and use it in GitHub Desktop.
Save giehlman/c8ef2f4b188fdb4573424bd868c728d7 to your computer and use it in GitHub Desktop.
For gradle projects! Convenience script to build, pack and upload code to an AWS ElasticBeanstalk environment, using the AWS CLI 'eb' command. For personal and experimental use only!
#!/usr/bin/env bash
#title : eb-publish-gradle.sh
#description : For gradle projects! Convenience script to build, pack and upload code to an AWS ElasticBeanstalk environment, using the AWS CLI 'eb' command. For personal and experimental use only!
#prerequisites : AWS CLI with 'eb' cmd, Procfile, gradle project, .elasticbeanstalk/config.yml
#author : Christian-André Giehl <christian@emailbrief.de>
#date : 20170410
#version : 1.1
#usage : sh eb-publish-gradle.sh
#==============================================================================
echo "### Creating Elastic Beanstalk deployment package..."
# Exits in case the supplied state is != 0. State is typically supplied via $?
exitOnError() {
state=$1
msg=$2
if [ $state -ne 0 ]; then
echo "!!! ${msg}"
echo "### Exiting."
exit $state
fi
}
# Check is AWS CLI and deploy.yaml there
command -v aws
exitOnError $? "AWS CLI not found or not accessible!"
[[ -e ".elasticbeanstalk/config.yml" ]]
exitOnError $? "ElasticBeanstalk config.yml not found in ./elasticbeanstalk/*!"
[[ -e ".Procfile" ]]
exitOnError $? "Procfile not found!"
echo "### Starting gradle..."
./gradlew clean test fatJarAPI
exitOnError $? "Unable to build!"
JAR=$(find ./ -iname API-*.jar -type f -print -quit) # Check *.jar name with your project!
echo "### Creating deployment zip..."
#ZIP=eb-deploy-${JAR##*/}.zip
ZIP=eb-deploy.zip
rm ${ZIP}
sed -i '' "s/web: java -jar.*/web: java -jar ${JAR##*/}/g" ./Procfile
cat ./Procfile
zip -j ${ZIP} Procfile ${JAR}
unzip -l ${ZIP}
echo "### Using file '${ZIP}' to deploy to elastic beanstalk..."
msg=$(echo $(git log -1 --pretty=%B))
lbl=$(echo $(git log -1 --format='%H'))
echo "--- Deploying to EB ---"
echo "Message: " $msg
echo "Label: " $lbl
read -p "-----> DO YOU WANT TO DEPLOY? [Yy]es " -n 1 -r
if ! [[ $REPLY =~ ^[Yy]$ ]]
then
echo "!!! ABORTED !!!"
exit 1
fi
eb deploy -m "$msg" -l "$lbl"
exitOnError $? "Unable to deploy to elastic beanstalk!"
echo "### Done!"
mv ${ZIP} ./build # Cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment