Class: Makit::GitLabRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/makit/gitlab_runner.rb

Overview

This class provide methods for working with Directories/

Example:

Makit::Directory.find_directory_with_pattern("/home/user", "*.rb")

Instance Method Summary collapse

Instance Method Details

#extract_script(ci_config, job_name) ⇒ Object

Extract the script for a specified job



21
22
23
24
# File 'lib/makit/gitlab_runner.rb', line 21

def extract_script(ci_config, job_name)
  job = ci_config[job_name]
  job ? job["script"] : nil
end

#parse_gitlab_ci_file(file_path) ⇒ Object

Parse the .gitlab-ci.yml file



16
17
18
# File 'lib/makit/gitlab_runner.rb', line 16

def parse_gitlab_ci_file(file_path)
  YAML.load_file(file_path)
end

#run_job(ci_file_path, job_name, docker_image) ⇒ Object

Main function to execute the process



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/makit/gitlab_runner.rb', line 39

def run_job(ci_file_path, job_name, docker_image)
  ci_config = parse_gitlab_ci_file(ci_file_path)
  script = extract_script(ci_config, job_name)

  unless script
    puts "Job '#{job_name}' not found in #{ci_file_path}"
    return
  end

  script_file = "temp_script.sh"
  write_script_to_file(script, script_file)
  FileUtils.chmod("+x", script_file)

  puts "Running script in Docker container..."
  run_script_in_docker(docker_image, script_file)

  # Clean up the temporary script file
  FileUtils.rm(script_file)
end

#run_script_in_docker(image, script_file) ⇒ Object

Run the script in a Docker container



34
35
36
# File 'lib/makit/gitlab_runner.rb', line 34

def run_script_in_docker(image, script_file)
  system("docker run --rm -v #{Dir.pwd}:/workspace -w /workspace #{image} /bin/sh #{script_file}")
end

#write_script_to_file(script, file_path) ⇒ Object

Write the script to a temporary file



27
28
29
30
31
# File 'lib/makit/gitlab_runner.rb', line 27

def write_script_to_file(script, file_path)
  File.open(file_path, "w") do |file|
    script.each { |line| file.puts(line) }
  end
end