miniexec 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/miniexec +3 -2
  3. data/lib/miniexec.rb +42 -10
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e03f7a3801afeabed8952f4813e0dc107053f6697052e0613be969a4853f0c0
4
- data.tar.gz: 2d82ddc61ea8be64219e733d3704e56e9c30f703fe74b121ef24558a3f0f324a
3
+ metadata.gz: 7167acf5043ec6bf1dd58585100d89cc68704bcbcabd1d9dbb516aa264263bc5
4
+ data.tar.gz: 61077414a22ad766389a6b869fa776858955733cc32c667bf94d1d89bb0d5a49
5
5
  SHA512:
6
- metadata.gz: 3b0b78c30b6cf9cb186f2a9531fd86ff8229d2a28793ded8149753c8bc64162fee95e28a9305bb1a8995a7d60e27dc06faeb002f4ee0f65213765c52b2b805b5
7
- data.tar.gz: a0657ec2d0cbada87e8fa9d6bedc8c580b529c362ed1fd84a1fcec04e7d5248f72d615bf5523649f313bc304a1e6c34272754348900fb395a9a14391ea01773a
6
+ metadata.gz: 5cab8edace6a15d9c8e7bd7afb9efc46778fcf99821ff58ae34fd9c3c9fd7fb8387b1e98719c68c160fef5aab95a9f1c599d6be99f6215a253e9ef90a52be2f5
7
+ data.tar.gz: 4967266794a76314873f51aaef7d245327dbfcda0bccee453f3fa5c45bc4c903b45a9d3b37ca95c9cbc6d246dc32d1bf4c28b88274749a881e827c2b1fdba46c
data/bin/miniexec CHANGED
@@ -6,7 +6,7 @@ require 'optparse'
6
6
 
7
7
  options = {
8
8
  binds: [],
9
- env: []
9
+ env: {}
10
10
  }
11
11
 
12
12
  OptionParser.new do |opts|
@@ -29,7 +29,8 @@ OptionParser.new do |opts|
29
29
  opts.on('-e', '--environment VARIABLE',
30
30
  'Specify an environment variable to be passed to the container',
31
31
  'Example: SOMEVAR=thing') do |env|
32
- options[:env].push env
32
+ k, v = env.split('=')
33
+ options[:env][k] = v
33
34
  end
34
35
  opts.on('-d', '--docker-url URL',
35
36
  'Location of the docker socket') do |sock|
data/lib/miniexec.rb CHANGED
@@ -7,6 +7,8 @@ class MiniExec
7
7
  require 'json'
8
8
  require 'tempfile'
9
9
  require 'yaml'
10
+ require 'git'
11
+ require 'pry'
10
12
  # Class instance variables
11
13
  @project_path = '.'
12
14
  @workflow_file = '.gitlab-ci.yml'
@@ -17,7 +19,7 @@ class MiniExec
17
19
 
18
20
  def self.config(project_path: @project_path, workflow_file: @workflow_file)
19
21
  @project_path = project_path
20
- @class = workflow_file
22
+ @workflow_file = workflow_file
21
23
  self
22
24
  end
23
25
 
@@ -25,23 +27,20 @@ class MiniExec
25
27
 
26
28
  def initialize(job,
27
29
  project_path: self.class.project_path,
28
- workflow_file: self.class.workflow_file,
29
30
  docker_url: nil,
30
31
  binds: [],
31
- env: [])
32
+ env: {})
32
33
  @job_name = job
33
34
  @project_path = project_path
34
- workflow = YAML.load(File.read("#{@project_path}/#{workflow_file}"))
35
- @job = workflow[job]
35
+ @workflow = YAML.load(File.read("#{@project_path}/#{MiniExec.workflow_file}"))
36
+ @job = @workflow[job]
36
37
  @job['name'] = job
37
- @default_image = workflow['image'] || 'debian:buster-slim'
38
+ @default_image = @workflow['image'] || 'debian:buster-slim'
38
39
  @image = set_job_image
39
40
  @script = compile_script
40
41
  @binds = binds
41
- @env = env
42
-
42
+ @env = env.merge gitlab_env, variables
43
43
  configure_logger
44
-
45
44
  Docker.options[:read_timeout] = 6000
46
45
  Docker.url = docker_url if docker_url
47
46
  end
@@ -57,7 +56,7 @@ class MiniExec
57
56
  Cmd: ['/bin/bash', script_path],
58
57
  Image: @image,
59
58
  Volumes: @binds.map { |b| { b => { path_parent: 'rw' } } }.inject(:merge),
60
- Env: @env
59
+ Env: @env.map { |k, v| "#{k}=#{v}" }
61
60
  )
62
61
  container.store_file(script_path, @script)
63
62
  container.start({ Binds: [@binds] })
@@ -73,6 +72,39 @@ class MiniExec
73
72
  @default_image
74
73
  end
75
74
 
75
+ # Set gitlab's predefined env vars as per
76
+ # https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
77
+ def gitlab_env
78
+ g = Git.open(@project_path)
79
+ commit = g.gcommit 'HEAD'
80
+ tag = g.tags.find { |t| t.objectish == commit.sha }
81
+ commit_branch = g.branch.name
82
+ if tag.nil?
83
+ ref_name = g.branch.name
84
+ commit_tag = nil
85
+ else
86
+ ref_name = tag.name
87
+ commit_tag = ref_name
88
+ end
89
+ {
90
+ 'CI': true,
91
+ 'CI_COMMIT_REF_SHA': commit.sha,
92
+ 'CI_COMMIT_SHORT_SHA': commit.sha[0, 8],
93
+ 'CI_COMMIT_REF_NAME': ref_name,
94
+ 'CI_COMMIT_BRANCH': commit_branch,
95
+ 'CI_COMMIT_TAG': commit_tag,
96
+ 'CI_COMMIT_MESSAGE': commit.message,
97
+ 'CI_COMMIT_REF_PROTECTED': false,
98
+ 'CI_COMMIT_TIMESTAMP': commit.date.strftime('%FT%T')
99
+ }.transform_keys(&:to_s)
100
+ end
101
+
102
+ def variables
103
+ globals = @workflow['variables']
104
+ job_locals = @job['variables']
105
+ globals.merge job_locals
106
+ end
107
+
76
108
  def configure_logger
77
109
  @logger = Logger.new($stdout)
78
110
  @logger.formatter = proc do |severity, _, _, msg|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miniexec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Pugh
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-03 00:00:00.000000000 Z
11
+ date: 2021-03-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A minimal interpretor/executor for .gitlab-ci.yml
14
14
  email: pugh@s3kr.it