Skip to content

Instantly share code, notes, and snippets.

@cursork
Created December 18, 2013 11:37
Show Gist options
  • Save cursork/8020949 to your computer and use it in GitHub Desktop.
Save cursork/8020949 to your computer and use it in GitHub Desktop.
Gets the artifact from Circle for the last successful build on master. Outputs {"changed":true} if a new .jar was linked into place (for SaltStack to pick up in a cmd.run: ... stateful declaration).
#!/usr/bin/env perl
use v5.012;
use warnings FATAL => 'all';
use utf8;
use autodie ':all';
use Mojo::UserAgent;
my $circle_token = $ENV{'CIRCLE_TOKEN'}
or die 'No CIRCLE_TOKEN in environment';
my $project = 'project/' . $ARGV[0]
or die 'No :user/:project given on command line.';
my ($last_success) = grep { $_->{'status'} eq 'success' }
@{ get($project, 'tree/master')->json };
my $jar = $last_success->{'vcs_revision'} . '.jar';
if (-e $jar) {
say '{"changed":false}';
exit;
}
my $artifact_url = get($project, $last_success->{'build_num'}, 'artifacts')
->json->[0]{'url'};
{
local $ENV{'MOJO_MAX_MESSAGE_SIZE'} = 104_857_600;
get($artifact_url)->content->asset
->move_to($jar);
}
if (-e 'current.jar') {
unlink 'current.jar';
}
system "ln -s $jar current.jar";
say '{"changed":true}';
sub get {
state $ua = Mojo::UserAgent->new;
my $url = join '/', @_;
my $res = $ua->get(
($url =~ /^http/ ? $url : 'https://circleci.com/api/v1/' . $url)
. "?circle-token=$circle_token" => { 'Accept' => 'application/json' }
)->res;
return $res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment