Skip to content

Instantly share code, notes, and snippets.

@double16
Created April 22, 2015 19:01
Show Gist options
  • Save double16/b58fca44e009cce82076 to your computer and use it in GitHub Desktop.
Save double16/b58fca44e009cce82076 to your computer and use it in GitHub Desktop.
double16/pet-store heroku deploy examples
task emptyPom {
outputs.file('build/pom.xml')
doLast {
new File('build/pom.xml') << """
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>pet.store</groupId>
<artifactId>placeholder</artifactId>
<packaging>jar</packaging>
<version>1.0</version>
<name>Pet Store</name>
<build>
<finalName>placeholder</finalName>
</build>
</project>
"""
}
}
task herokuSources(type: Tar, dependsOn: [emptyPom, bootRepackage]) {
description = "Package built application into Heroku sources for faster deployment"
group = "distribution"
appendix = "heroku"
compression = Compression.GZIP
from jar.archivePath
from 'Procfile'
from 'system.properties'
from 'build/pom.xml'
}
// Documentation at https://devcenter.heroku.com/articles/build-and-release-using-the-api
task deployToHeroku(dependsOn: herokuSources) {
description = 'Deploy the application to Heroku via the Platform API REST services'
group = "distribution"
doLast {
RESTClient heroku = new RESTClient("https://api.heroku.com/apps/${System.getenv("HEROKU_APP_NAME")}/")
heroku.headers['Authorization'] = "Bearer ${System.getenv('HEROKU_AUTH_TOKEN')}"
heroku.headers['Accept'] = 'application/vnd.heroku+json; version=3'
// upload sources
def sources = heroku.post(path: 'sources')
def put_url = sources.data.source_blob.put_url
def get_url = sources.data.source_blob.get_url
// upload sources, Heroku/AWS is very picky about the headers sent, so we need to use this way. For example, Content-Type must not be present
logger.info("Uploading ${herokuSources.archivePath} to ${put_url}")
def res = new DefaultHttpClient().execute(new HttpPut(URI: new URI(put_url), entity: new FileEntity(herokuSources.archivePath)))
if (res.statusLine.statusCode > 399) {
throw new IOException(res.statusLine.reasonPhrase)
}
// start the build
logger.info("Building ${get_url}")
heroku.post(path: 'builds', requestContentType: 'application/json',
body: ['source_blob': ['url':get_url, 'version': "git rev-parse HEAD".execute().text.trim() ]]
)
}
onlyIf { System.getenv('HEROKU_AUTH_TOKEN') && System.getenv('HEROKU_APP_NAME') }
}
heroku.post(path: 'builds', requestContentType: 'application/json',
body: ['source_blob': ['url':get_url, 'version': "git rev-parse HEAD".execute().text.trim() ]]
)
def sources = heroku.post(path: 'sources')
def put_url = sources.data.source_blob.put_url
def get_url = sources.data.source_blob.get_url
def res = new DefaultHttpClient().execute(new HttpPut(URI: new URI(put_url), entity: new FileEntity(herokuSources.archivePath)))
if (res.statusLine.statusCode > 399) {
throw new IOException(res.statusLine.reasonPhrase)
}
web: java -Dgrails.env=prod -Dserver.port=${PORT:-8080} -Dserver.host=0.0.0.0 ${JAVA_OPTS} -jar pet-store*.jar
RESTClient heroku = new RESTClient("https://api.heroku.com/apps/${System.getenv("HEROKU_APP_NAME")}/")
heroku.headers['Authorization'] = "Bearer ${System.getenv('HEROKU_AUTH_TOKEN')}"
heroku.headers['Accept'] = 'application/vnd.heroku+json; version=3'
java.runtime.version=1.8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment