lib/makit/gitlab_runner.rb in makit-0.0.57 vs lib/makit/gitlab_runner.rb in makit-0.0.58

- old
+ new

@@ -1,60 +1,60 @@ -# frozen_string_literal: true - -require "yaml" -require "fileutils" - -# This module provides classes for the Makit gem. -module Makit - # This class provide methods for working with Directories/ - # - # Example: - # - # Makit::Directory.find_directory_with_pattern("/home/user", "*.rb") - # - class GitLabRunner - - # Parse the .gitlab-ci.yml file - def parse_gitlab_ci_file(file_path) - YAML.load_file(file_path) - end - - # Extract the script for a specified job - def extract_script(ci_config, job_name) - job = ci_config[job_name] - job ? job["script"] : nil - end - - # Write the script to a temporary file - def write_script_to_file(script, file_path) - File.open(file_path, "w") do |file| - script.each { |line| file.puts(line) } - end - end - - # Run the script in a Docker container - def run_script_in_docker(image, script_file) - system("docker run --rm -v #{Dir.pwd}:/workspace -w /workspace #{image} /bin/sh #{script_file}") - end - - # Main function to execute the process - 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 - end -end +# frozen_string_literal: true + +require "yaml" +require "fileutils" + +# This module provides classes for the Makit gem. +module Makit + # This class provide methods for working with Directories/ + # + # Example: + # + # Makit::Directory.find_directory_with_pattern("/home/user", "*.rb") + # + class GitLabRunner + + # Parse the .gitlab-ci.yml file + def parse_gitlab_ci_file(file_path) + YAML.load_file(file_path) + end + + # Extract the script for a specified job + def extract_script(ci_config, job_name) + job = ci_config[job_name] + job ? job["script"] : nil + end + + # Write the script to a temporary file + def write_script_to_file(script, file_path) + File.open(file_path, "w") do |file| + script.each { |line| file.puts(line) } + end + end + + # Run the script in a Docker container + def run_script_in_docker(image, script_file) + system("docker run --rm -v #{Dir.pwd}:/workspace -w /workspace #{image} /bin/sh #{script_file}") + end + + # Main function to execute the process + 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 + end +end