Sha256: c86c237df68978a7997084a5e01105e5816ab2c7630addf17d30b8fa0a8440c6

Contents?: true

Size: 1.59 KB

Versions: 25

Compression:

Stored size: 1.59 KB

Contents

# 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

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
makit-0.0.57 lib/makit/gitlab_runner.rb
makit-0.0.53 lib/makit/gitlab_runner.rb
makit-0.0.52 lib/makit/gitlab_runner.rb
makit-0.0.51 lib/makit/gitlab_runner.rb
makit-0.0.50 lib/makit/gitlab_runner.rb
makit-0.0.49 lib/makit/gitlab_runner.rb
makit-0.0.47 lib/makit/gitlab_runner.rb
makit-0.0.46 lib/makit/gitlab_runner.rb
makit-0.0.45 lib/makit/gitlab_runner.rb
makit-0.0.44 lib/makit/gitlab_runner.rb
makit-0.0.42 lib/makit/gitlab_runner.rb
makit-0.0.41 lib/makit/gitlab_runner.rb
makit-0.0.35 lib/makit/gitlab_runner.rb
makit-0.0.34 lib/makit/gitlab_runner.rb
makit-0.0.33 lib/makit/gitlab_runner.rb
makit-0.0.32 lib/makit/gitlab_runner.rb
makit-0.0.31 lib/makit/gitlab_runner.rb
makit-0.0.30 lib/makit/gitlab_runner.rb
makit-0.0.29 lib/makit/gitlab_runner.rb
makit-0.0.28 lib/makit/gitlab_runner.rb