Last active
February 4, 2025 22:30
-
-
Save aaron-humerickhouse/314b629661607814b3dc9f3977e2573e to your computer and use it in GitHub Desktop.
run_circle_job.rb
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
require 'yaml' | |
require 'optparse' | |
require 'net/http' | |
require "uri" | |
require 'json' | |
require 'bundler/inline' | |
gemfile do | |
source 'https://rubygems.org' | |
gem 'pry-byebug' | |
end | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: ruby run_circle_job.rb -j JOB [options]" | |
opts.on('-j', '--job JOB', 'CircleCI job to execute [ REQUIRED ]') { |v| options[:job] = v } | |
opts.on('-r', '--revision REVISION', 'Git revision of code to run') { |v| options[:revision] = v } | |
opts.on('-s', '--repo REPO', 'Github bigcommerce repository to find the revision') { |v| options[:repo] = v } | |
opts.on('-b', '--branch BRANCH', 'Github branch to find the revision') { |v| options[:branch] = v } | |
end.parse! | |
if options[:job].nil? | |
puts 'Please provide the job with the -j flag' | |
exit 1 | |
end | |
if options[:revision].nil? | |
puts "Revision wasn't provided, using git to retrieve it" | |
options[:revision] = `git rev-parse HEAD`.strip | |
end | |
if options[:repo].nil? | |
puts "Repo wasn't provided, using pwd to retrieve it" | |
options[:repo] = `pwd`.split('/').last.strip | |
end | |
if options[:branch].nil? | |
puts "Branch wasn't provided, using git to retrieve it" | |
options[:branch] = `git rev-parse --abbrev-ref HEAD`.strip | |
end | |
which = `which circleci` | |
if which.include?('not found') | |
puts 'CircleCI CLI is not installed. Please install it with `brew install circleci`' | |
exit 1 | |
end | |
circleci_token = ENV['CIRCLE_API_TOKEN'] | |
unless circleci_token | |
puts 'Please set the CIRCLE_API_TOKEN environment variable' | |
exit 1 | |
end | |
# Compile the config | |
compiled_config = YAML.load(`circleci config process .circleci/config.yml`) | |
if compiled_config.include?('failed to load yaml config') | |
puts 'Failed to compile the config' | |
exit 1 | |
end | |
body = { | |
job: options[:job], | |
revision: options[:revision], | |
config: compiled_config.to_yaml | |
} | |
url = URI("https://circleci.com/api/v1.1/project/github/bigcommerce/#{options[:repo]}/tree/main") | |
https = Net::HTTP.new(url.host, url.port) | |
https.use_ssl = true | |
request = Net::HTTP::Post.new(url) | |
request["Content-Type"] = "application/json" | |
request["Circle-Token"] = circleci_token | |
request.body = body.to_json | |
puts 'Starting job with the following parameters' | |
puts " Job: #{options[:job]}" | |
puts " Revision: #{options[:revision]}" | |
puts " Repo: #{options[:repo]}" | |
puts " Branch: #{options[:branch]}\n\n" | |
response = https.request(request) | |
if Integer(response.code) >= 400 | |
puts "Error: #{response.code}" | |
puts response.body | |
exit 1 | |
end | |
puts 'Job succeeded' | |
puts JSON.parse(response.body)['build_url'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment