Skip to content

Instantly share code, notes, and snippets.

@paganotoni
Created October 13, 2012 14:57
Show Gist options
  • Save paganotoni/3884902 to your computer and use it in GitHub Desktop.
Save paganotoni/3884902 to your computer and use it in GitHub Desktop.
Grails Amazon EBS deploy Script
includeTargets << grailsScript("_GrailsInit")
import com.amazonaws.auth.*
import com.amazonaws.services.s3.*
import com.amazonaws.services.elasticbeanstalk.*
import com.amazonaws.services.elasticbeanstalk.model.*
def projectWarFilename
def optionMap = [scripts: []]
target(parseArguments: "parse the arguments passed on the command line") {
args?.tokenize().each { token ->
def nameValue = token =~ "--?(.*)=(.*)"
if (nameValue.matches()) { // this token is a name/value pair
optionMap[nameValue[0][1]] = nameValue[0][2]
} else { // single item token, only expecting scriptName
optionMap["scripts"] << token
}
}
if (!optionMap["EBSEnvName"] || !optionMap["EBSAppName"]){
println("Argumments --EBSEnvName and --EBSAppName are required, please provide these.")
System.exit(1)
}
}
target(default: 'Deploying application to EBS') {
parseArguments();
deployBeanstalk()
}
target(deployBeanstalk: 'Deploy to AWS Beanstalk') {
def warTarget = "target/application.war";
def environmentName = optionMap["EBSEnvName"]
def applicationName = optionMap["EBSAppName"]
def versionDate = new Date().format("[dd-MM-yyyy hh:mm:ss]");
def versionLabel = "automated $versionDate"
def description = "automated build"
def credentials = new BasicAWSCredentials( "[accessKey]", "[secretKey]" )
// Log on to AWS with your credentials
AmazonS3 s3 = new AmazonS3Client(credentials)
AWSElasticBeanstalk elasticBeanstalk = new AWSElasticBeanstalkClient(credentials)
// Upload a WAR file to Amazon S3
println "| Uploading war file to Amazon S3 ( Please be patient, this could take several minutes )"
def warFile = new File(warTarget) //projectWarFilename
String bucketName = elasticBeanstalk.createStorageLocation().getS3Bucket()
String key = URLEncoder.encode(warFile.name, 'UTF-8')
def s3Result = s3.putObject(bucketName, key, warFile)
println "| [Completed] Uploaded application $s3Result.versionId"
// Register a new application version
println "| Creating application version with uploaded application"
def createApplicationRequest = new CreateApplicationVersionRequest(
applicationName: applicationName, versionLabel: versionLabel,
description: description,
autoCreateApplication: true, sourceBundle: new S3Location(bucketName, key)
)
def createApplicationVersionResult = elasticBeanstalk.createApplicationVersion(createApplicationRequest)
println "| [Completed] Registered application version $createApplicationVersionResult"
println "| Updating environment with uploaded application version"
def updateEnviromentRequest = new UpdateEnvironmentRequest(environmentName: environmentName, versionLabel: versionLabel)
def updateEnviromentResult = elasticBeanstalk.updateEnvironment(updateEnviromentRequest)
println "| [Completed] Updated environment $updateEnviromentResult"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment