Created
April 22, 2015 19:01
-
-
Save double16/b58fca44e009cce82076 to your computer and use it in GitHub Desktop.
double16/pet-store heroku deploy examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
heroku.post(path: 'builds', requestContentType: 'application/json', | |
body: ['source_blob': ['url':get_url, 'version': "git rev-parse HEAD".execute().text.trim() ]] | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sources = heroku.post(path: 'sources') | |
def put_url = sources.data.source_blob.put_url | |
def get_url = sources.data.source_blob.get_url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
web: java -Dgrails.env=prod -Dserver.port=${PORT:-8080} -Dserver.host=0.0.0.0 ${JAVA_OPTS} -jar pet-store*.jar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.runtime.version=1.8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment