Skip to content

Commit

Permalink
Adding more release automation
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Aug 22, 2014
1 parent ddef97f commit 09f7212
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 1 deletion.
36 changes: 36 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,47 @@ docs:
view-docs:
open docs/_build/html/index.html

# Packages the phar and zip
package: burgomaster
time php build/packager.php

# Downloads a copy of Burgomaster
burgomaster:
mkdir -p build/artifacts
curl -s https://raw.githubusercontent.com/mtdowling/Burgomaster/0.0.1/src/Burgomaster.php > build/artifacts/Burgomaster.php

# Ensures that the TAG variable was passed to the make command
check_tag:
$(if $(TAG),,$(error TAG is not defined. Pass via "make tag TAG=4.2.1"))

# Creates a release but does not push it. This task updates the changelog
# with the TAG environment variable, replaces the VERSION constant, ensures
# that the source is still valid after updating, commits the changelog and
# updated VERSION constant, creates an annotated git tag using chag, and
# prints out a diff of the last commit.
tag: check_tag
@echo Tagging $(TAG)
chag update -m '$(TAG) ()'
sed -i '' -e "s/VERSION = '.*'/VERSION = '$(TAG)'/" src/Aws/Common/Aws.php
php -l src/Aws/Common/Aws.php
git commit -a -m '$(TAG) release'
chag tag
@echo "Release has been created. Push using 'make release'"
@echo "Changes made in the release commit"
git diff HEAD~1 HEAD

# Creates a release based on the master branch and latest tag. This task
# pushes the latest tag, pushes master, creates a phar and zip, and creates
# a Github release. Use "TAG=X.Y.Z make tag" to create a release, and use
# "make release" to push a release. This task requires that the
# OAUTH_TOKEN environment variable is available and the token has permission
# to push to the repository.
release: package
@echo "Pushing code to master"
git push origin master
@echo "Pushing latest tag to Github"
git push origin `git describe --abbrev=0 --tags`
echo "Creating a Github release"
php build/gh-release.php

.PHONY: docs burgomaster
126 changes: 126 additions & 0 deletions build/gh-release.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/*
* This script creates a new Github release via the releases API using
* the staging directory, artifacts built via the package.php script,
* an OAUTH_TOKEN provided as an environment variable.
*
* The contents of the release notes are parsed from the CHANGELOG.md file
* using the latest version found in the CHANGELOG.
*/
require __DIR__ . '/../vendor/autoload.php';

/**
* Github release helper class.
*/
class Release
{
private $token;
private $owner;
private $repo;
private $client;

public function __construct($token, $owner, $repo)
{
$this->token = $token;
$this->owner = $owner;
$this->repo = $repo;
$this->client = new \Guzzle\Http\Client();
$this->client->setUserAgent('aws-sdk-php');
$this->client->setDefaultOption(
'headers/Authorization',
'token ' . $token
);
}

/**
* Executes a command and returns the output as a string. If it fails it throws.
*/
public static function exec($command)
{
exec($command, $output, $returnValue);
$output = implode("\n", $output);

if ($returnValue != 0) {
die("Error executing command: {$command}\n$output");
}

return $output;
}

public function createRelease(
$tag,
$contents,
$branch = 'master',
$draft = false
) {
$request = $this->client->createRequest(
'POST',
"https://api.github.com/repos/$this->owner/$this->repo/releases",
array('Content-Type' => 'application/json'),
json_encode(array(
'tag_name' => $tag,
'target_commitish' => $branch,
'name' => $tag . ' release',
'body' => $contents,
'draft' => $draft
))
);

return $this->client->send($request)->json();
}

public function uploadAsset($id, $name, $contents, $contentType)
{
$request = $this->client->createRequest(
'POST',
sprintf(
"https://api.github.com/repos/%s/%s/releases/%s/assets?name=%s",
$this->owner, $this->repo, $id, $name
),
array('Content-Type' => $contentType),
$contents
);

return $this->client->send($request)->json();
}
}

$owner = 'aws';
$repo = 'aws-sdk-php';
$returnVal = shell_exec('which chag') or die('chag not found in path');
$token = getenv('OAUTH_TOKEN') or die('OAUTH_TOKEN environment var not found!');
$artifacts = realpath(__DIR__ . '/artifacts') or die('artifacts dir not found');
$stageDir = realpath($artifacts . '/staging') or die('stage dir not found');
$zip = realpath($artifacts . '/aws.zip') or die('zip not found');
$phar = realpath($artifacts . '/aws.phar') or die('phar not found');

// Get the latest changelog entry
chdir($stageDir);
$tag = Release::exec('chag get');
$contents = Release::exec('chag contents');
echo "Found tag: {$tag}\n\n{$contents}\n\n";

$release = new Release($token, $owner, $repo);
$res = $release->createRelease($tag, $contents, 'master', true);
echo "Created release: {$res['id']}\n";

// Upload the phar
$upload = $release->uploadAsset(
$res['id'],
'aws.phar',
file_get_contents($phar),
'application/octet-stream'
);

echo "Uploaded asset: {$upload['browser_download_url']}\n";

// Upload the zip
$upload = $release->uploadAsset(
$res['id'],
'aws.zip',
file_get_contents($zip),
'application/zip'
);

echo "Uploaded asset: {$upload['browser_download_url']}\n\n";
echo "Successfully create GitHub release\n";
3 changes: 2 additions & 1 deletion build/packager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
$projectRoot = __DIR__ . '/../';
$burgomaster = new \Burgomaster($stageDirectory, $projectRoot);

foreach (['README.md', 'LICENSE.md', 'NOTICE.md'] as $file) {
$metaFiles = ['README.md', 'LICENSE.md', 'NOTICE.md', 'CHANGELOG.md'];
foreach ($metaFiles as $file) {
$burgomaster->deepCopy($file, $file);
}

Expand Down

0 comments on commit 09f7212

Please sign in to comment.