pipedawg 0.1.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9167728fbd620f0098099ff53a90fb1d85336a8a771b24cc3059b8dc03144285
4
- data.tar.gz: 6f1d59f9fb1ca2de1a49d04520f6a9cd7f171f569c72a372899f9f28749ead34
3
+ metadata.gz: 114b51a7224d92e384383b3485e96547a250ae90cc64a89250d7a45ff3ae35c0
4
+ data.tar.gz: 98eac24c50bdeb4282077b32e45afc87f9e8f3db01f47c5b837d36a9a3866ca0
5
5
  SHA512:
6
- metadata.gz: f53e89a60305aaa04c14443c404e3bcfa0fa24274503751c927ff6ace5f9cabbeb87f3bc2e87ca81d04a9c14aa280ca2942b381eeecbbae23db4bfd4c4c2165e
7
- data.tar.gz: a3f7acd6b467e21fee88fa2e2b22ef55e60030311355fb3074ae0f4eac0a52674c0027c0b4ebb0b21dfb1317ee5d1e3a753ece8f96c098183699c53d04a703ae
6
+ metadata.gz: 1504e970a5b8b629f5b95a92a8fd9edc4685d3d98adbf3f94631a0c0d4b12beaadb83a30b5337ba9ad03310956ce1c9e7924f6c04f968bca8287e0c45a69eb5e
7
+ data.tar.gz: 80e24f54810711abc38902ab44aae67499372f9d73c265aedc82d6d6bc2c3dd7d26f230195a435580674502bb40563e67810be18ebbb51bd2e35c4b735888fb0
data/README.md CHANGED
@@ -42,25 +42,13 @@ gem_job = Pipedawg::Job.new(
42
42
  script: ['bundle install', 'gem build *.gemspec']
43
43
  )
44
44
 
45
- docker_job = Pipedawg::Job.new(
46
- 'build:docker',
47
- image: 'docker',
48
- needs: ['build:gem'],
49
- retry: 2,
50
- script: [
51
- 'docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY',
52
- 'docker pull $CI_REGISTRY_IMAGE || true',
53
- 'docker build --pull --cache-from $CI_REGISTRY_IMAGE -t $CI_REGISTRY_IMAGE .',
54
- 'docker push $CI_REGISTRY_IMAGE'
55
- ],
56
- services: ['docker:dind']
45
+ kaniko_job = Pipedawg::KanikoJob.new(
46
+ 'build:kaniko',
47
+ {needs: ['build:gem'], retry: 2},
48
+ {context:'${CI_PROJECT_DIR}/docker',external_files: {'*.gem':'gems'}}
57
49
  )
58
50
 
59
- pipeline = Pipedawg::Pipeline.new 'build:image', jobs: [gem_job, docker_job]
60
-
61
- # Automatically calculates stages of jobs based on 'needs'
62
- pipeline.update_stages
63
-
51
+ pipeline = Pipedawg::Pipeline.new 'build:image', jobs: [gem_job, kaniko_job]
64
52
  puts pipeline.to_yaml
65
53
  ```
66
54
 
@@ -84,23 +72,21 @@ build:gem:
84
72
  - gem build *.gemspec
85
73
  stage: '1'
86
74
  tags: []
87
- build:docker:
75
+ build:kaniko:
88
76
  artifacts: {}
89
77
  cache: {}
90
- image: docker
91
78
  needs:
92
79
  - build:gem
93
80
  retry: 2
94
81
  rules: []
95
82
  script:
96
- - docker login -u gitlab-ci-token -p $CI_BUILD_TOKEN $CI_REGISTRY
97
- - docker pull $CI_REGISTRY_IMAGE || true
98
- - docker build --pull --cache-from $CI_REGISTRY_IMAGE -t $CI_REGISTRY_IMAGE .
99
- - docker push $CI_REGISTRY_IMAGE
83
+ - echo "{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}"
84
+ > "/kaniko/.docker/config.json"
85
+ - cp "*.gem" "${CI_PROJECT_DIR}/docker/gems"
86
+ - '"/kaniko/executor" --context "${CI_PROJECT_DIR}/docker" --dockerfile "Dockerfile"
87
+ --no-push'
100
88
  stage: '2'
101
89
  tags: []
102
- services:
103
- - docker:dind
104
90
  ```
105
91
 
106
92
  ## Development
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pipedawg
4
+ # helm_copy_job class
5
+ class HelmCopyJob < Job
6
+ attr_accessor :helm_opts
7
+
8
+ def initialize(name = 'build', opts = {}, helm_opts = {})
9
+ @helm_opts = {
10
+ chart: name,
11
+ destinations: [{ user: nil, password: nil, url: nil }],
12
+ helm: 'helm',
13
+ image: { entrypoint: [''], name: 'alpine/helm' },
14
+ password: nil, url: nil, user: nil, version: nil
15
+ }.merge(helm_opts)
16
+ super name, opts
17
+ update
18
+ end
19
+
20
+ def update
21
+ opts[:image] = helm_opts[:image] if helm_opts[:image]
22
+ opts[:script] = [] + pull + (helm_opts[:destinations].map { |d| push(d) }).flatten(1)
23
+ end
24
+
25
+ private
26
+
27
+ def pull
28
+ case helm_opts[:url]
29
+ when nil
30
+ []
31
+ when %r{^oci://}
32
+ pull_oci
33
+ else
34
+ pull_classic
35
+ end
36
+ end
37
+
38
+ def push(destination)
39
+ case destination[:url]
40
+ when nil
41
+ []
42
+ when %r{^oci://}
43
+ push_oci(destination)
44
+ else
45
+ push_classic(destination)
46
+ end
47
+ end
48
+
49
+ def pull_oci # rubocop:disable Metrics/AbcSize
50
+ script = []
51
+ if helm_opts[:url] && helm_opts[:chart] && helm_opts[:version]
52
+ script = ['export HELM_EXPERIMENTAL_OCI=1']
53
+ script << login_oci(helm_opts) if helm_opts[:user] && helm_opts[:password]
54
+ script << "\"#{helm_opts[:helm]}\" pull \"#{helm_opts[:url]}/#{helm_opts[:chart]}\" --version \"#{helm_opts[:version]}\"" # rubocop:disable Layout/LineLength
55
+ end
56
+ script
57
+ end
58
+
59
+ def push_oci(destination) # rubocop:disable Metrics/AbcSize
60
+ script = []
61
+ if destination[:url] && helm_opts[:chart] && helm_opts[:version]
62
+ script = ['export HELM_EXPERIMENTAL_OCI=1']
63
+ script << login_oci(destination) if destination[:user] && destination[:password]
64
+ script << "\"#{helm_opts[:helm]}\" push \"#{helm_opts[:chart]}-#{helm_opts[:version]}.tgz\" \"#{destination[:url]}\"" # rubocop:disable Layout/LineLength
65
+ end
66
+ script
67
+ end
68
+
69
+ def login_oci(login_opts)
70
+ require 'uri'
71
+ "echo \"#{login_opts[:password]}\" | \"#{helm_opts[:helm]}\" registry login --username \"#{login_opts[:user]}\" --password-stdin \"#{URI(login_opts[:url]).host}\"" # rubocop:disable Layout/LineLength
72
+ end
73
+
74
+ def pull_classic # rubocop:disable Metrics/AbcSize
75
+ script = []
76
+ if helm_opts[:url] && helm_opts[:chart] && helm_opts[:version]
77
+ suffix = login_classic(helm_opts)
78
+ script << "\"#{helm_opts[:helm]}\" repo add source \"#{helm_opts[:url]}\"#{suffix}"
79
+ script << "\"#{helm_opts[:helm]}\" repo update"
80
+ script << "\"#{helm_opts[:helm]}\" pull \"source/#{helm_opts[:chart]}\" --version \"#{helm_opts[:version]}\""
81
+ end
82
+ script
83
+ end
84
+
85
+ def push_classic(destination)
86
+ script = []
87
+ if destination[:url] && helm_opts[:chart] && helm_opts[:version]
88
+ script << plugin_classic
89
+ suffix = login_classic(destination)
90
+ script << "\"#{helm_opts[:helm]}\" cm-push \"#{helm_opts[:chart]}-#{helm_opts[:version]}.tgz\" \"#{destination[:url]}\"#{suffix}" # rubocop:disable Layout/LineLength
91
+ end
92
+ script
93
+ end
94
+
95
+ def login_classic(login_opts)
96
+ if login_opts[:user] && login_opts[:password]
97
+ " --username \"#{login_opts[:user]}\" --password \"#{login_opts[:password]}\""
98
+ else
99
+ ''
100
+ end
101
+ end
102
+
103
+ def plugin_classic
104
+ "\"#{helm_opts[:helm]}\" plugin list | grep -q cm-push || \"#{helm_opts[:helm]}\" plugin install https://github.com/chartmuseum/helm-push"
105
+ end
106
+ end
107
+ end
data/lib/pipedawg/job.rb CHANGED
@@ -13,7 +13,7 @@ module Pipedawg
13
13
  image: { name: 'ruby:2.5' },
14
14
  needs: [],
15
15
  retry: nil,
16
- rules: [],
16
+ rules: nil,
17
17
  script: [],
18
18
  stage: 'build',
19
19
  tags: []
@@ -0,0 +1,90 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pipedawg
4
+ # kaniko_job class
5
+ class KanikoJob < Job
6
+ attr_accessor :kaniko_opts
7
+
8
+ def initialize(name = 'build', opts = {}, kaniko_opts = {}) # rubocop:disable Metrics/MethodLength
9
+ @kaniko_opts = {
10
+ build_args: {},
11
+ config: {
12
+ '$CI_REGISTRY': {
13
+ username: '$CI_REGISTRY_USER',
14
+ password: '$CI_REGISTRY_PASSWORD'
15
+ }
16
+ },
17
+ config_file: '/kaniko/.docker/config.json',
18
+ context: '${CI_PROJECT_DIR}',
19
+ destinations: [],
20
+ dockerfile: 'Dockerfile',
21
+ executor: '/kaniko/executor',
22
+ external_files: {},
23
+ flags: [],
24
+ ignore_paths: [],
25
+ insecure_registries: [],
26
+ image: {
27
+ entrypoint: [''],
28
+ name: 'gcr.io/kaniko-project/executor:debug'
29
+ },
30
+ options: {},
31
+ registry_certificates: {},
32
+ registry_mirrors: [],
33
+ skip_tls_verify_registry: [],
34
+ trusted_ca_cert_source_files: [],
35
+ trusted_ca_cert_target_file: '/kaniko/ssl/certs/ca-certificates.crt'
36
+ }.merge(kaniko_opts)
37
+ super name, opts
38
+ update
39
+ end
40
+
41
+ def update # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
42
+ require 'json'
43
+ opts[:image] = kaniko_opts[:image] if kaniko_opts[:image]
44
+ script = ["echo #{kaniko_opts[:config].to_json.inspect} > \"#{kaniko_opts[:config_file]}\""]
45
+ cert_copies = Array(kaniko_opts[:trusted_ca_cert_source_files]).map do |cert|
46
+ "cat \"#{cert}\" >> \"#{kaniko_opts[:trusted_ca_cert_target_file]}\""
47
+ end
48
+ script.concat cert_copies
49
+ file_copies = kaniko_opts[:external_files].map do |source, dest|
50
+ "cp \"#{source}\" \"#{kaniko_opts[:context]}/#{dest}\""
51
+ end
52
+ script.concat file_copies
53
+ flags = kaniko_opts[:flags].clone
54
+ flags << 'no-push' if kaniko_opts[:destinations].empty?
55
+ flags_cli = flags.uniq.map { |f| "--#{f}" }.join(' ')
56
+ options_cli = kaniko_opts[:options].map { |k, v| "--#{k}=\"#{v}\"" }.join(' ')
57
+ build_args_cli = kaniko_opts[:build_args].map { |k, v| "--build-arg #{k}=\"#{v}\"" }.join(' ')
58
+ ignore_paths_cli = Array(kaniko_opts[:ignore_paths]).map { |p| "--ignore-path #{p}" }.join(' ')
59
+ insecure_registries_cli = Array(kaniko_opts[:insecure_registries]).map do |r|
60
+ "--insecure-registry #{r}"
61
+ end.join(' ')
62
+ registry_certificates_cli = kaniko_opts[:registry_certificates].map do |k, v|
63
+ "--registry-certificate #{k}=\"#{v}\""
64
+ end.join(' ')
65
+ registry_mirrors_cli = Array(kaniko_opts[:registry_mirrors]).map { |r| "--registry-mirror #{r}" }.join(' ')
66
+ skip_tls_verify_registrys_cli = Array(kaniko_opts[:skip_tls_verify_registry]).map do |r|
67
+ "--skip-tls-verify-registry #{r}"
68
+ end.join(' ')
69
+ destinations_cli = kaniko_opts[:destinations].map { |d| "--destination #{d}" }.join(' ')
70
+ kaniko_cmds = [
71
+ "\"#{kaniko_opts[:executor]}\"",
72
+ '--context',
73
+ "\"#{kaniko_opts[:context]}\"",
74
+ '--dockerfile',
75
+ "\"#{kaniko_opts[:dockerfile]}\"",
76
+ flags_cli,
77
+ options_cli,
78
+ build_args_cli,
79
+ ignore_paths_cli,
80
+ insecure_registries_cli,
81
+ registry_certificates_cli,
82
+ registry_mirrors_cli,
83
+ destinations_cli,
84
+ skip_tls_verify_registrys_cli
85
+ ].reject(&:empty?)
86
+ script << kaniko_cmds.join(' ')
87
+ opts[:script] = script
88
+ end
89
+ end
90
+ end
@@ -10,8 +10,9 @@ module Pipedawg
10
10
  @opts = {
11
11
  jobs: [Pipedawg::Job.new],
12
12
  stages: ['build'],
13
- workflow: {}
13
+ workflow: nil
14
14
  }.merge(opts)
15
+ update
15
16
  end
16
17
 
17
18
  def to_yaml
@@ -28,7 +29,7 @@ module Pipedawg
28
29
  File.write(file, to_yaml)
29
30
  end
30
31
 
31
- def update_stages
32
+ def update
32
33
  stages = []
33
34
  opts[:jobs].each do |job|
34
35
  stage = stage_from_needs(opts[:jobs], job.name)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pipedawg
4
- VERSION = '0.1.1'
4
+ VERSION = '0.3.0'
5
5
  end
data/lib/pipedawg.rb CHANGED
@@ -1,6 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'pipedawg/job'
4
+ require 'pipedawg/helm_copy_job'
5
+ require 'pipedawg/kaniko_job'
4
6
  require 'pipedawg/pipeline'
5
7
  require 'pipedawg/version'
6
8
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pipedawg
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - harbottle
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-28 00:00:00.000000000 Z
11
+ date: 2022-02-04 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Generate GitLab CI pipelines.
14
14
  email:
@@ -20,7 +20,9 @@ files:
20
20
  - LICENSE.txt
21
21
  - README.md
22
22
  - lib/pipedawg.rb
23
+ - lib/pipedawg/helm_copy_job.rb
23
24
  - lib/pipedawg/job.rb
25
+ - lib/pipedawg/kaniko_job.rb
24
26
  - lib/pipedawg/pipeline.rb
25
27
  - lib/pipedawg/version.rb
26
28
  homepage: https://github.com/liger1978/pipedawg
@@ -45,7 +47,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
45
47
  - !ruby/object:Gem::Version
46
48
  version: '0'
47
49
  requirements: []
48
- rubygems_version: 3.0.3
50
+ rubygems_version: 3.1.2
49
51
  signing_key:
50
52
  specification_version: 4
51
53
  summary: Generate GitLab CI pipelines.