Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
A short Ruby script to run tests from `.gitlab-ci.yml` using Docker
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'yaml'
GITLAB_CI_YML = File.expand_path('.gitlab-ci.yml')
puts "Using #{GITLAB_CI_YML}"
def sys(cmd)
puts cmd
system(cmd)
end
YAML.load_file(GITLAB_CI_YML).values.each do |h|
next unless h.is_a?(Hash) && h.key?('stage') && h['stage'] == 'test'
services = h.fetch('services', []).map { |s| s.split ':' }
services.each do |name, tag|
image = "#{name}:#{tag}"
sys %(docker images --format '{{.Repository}}:{{.Tag}}'|grep ^#{image}$ || docker pull #{image})
sys %(docker run -d --name #{name} #{image})
end
variables = h.fetch('variables', []).map do |var, val|
"-e #{var}='#{val}'"
end
links = services.map { |name, _tag| "--link #{name}" }
script = (['cd /src'] + h['script']).join(' && ')
cmd = %w[docker run --rm -i -v $(pwd):/src] + links + variables + [h['image']] + %w[bash -c] + [%('#{script}')]
ok = sys(cmd.join(' '))
services.each do |name, _tag|
sys %(docker stop #{name} && docker rm #{name})
end
exit 1 unless ok
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment