-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate.rb
executable file
·67 lines (58 loc) · 1.75 KB
/
update.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby
require 'date'
require 'uri'
require 'net/http'
require 'json'
require_relative 'get_latest.rb'
def commit(msg)
ok = system('git', 'config', '--local', 'user.email', '[email protected]')
raise('failed to set email') unless ok
ok = system('git', 'config', '--local', 'user.name', 'Github Action')
raise('failed to set name') unless ok
ok = system('git', 'commit', '-m', msg)
raise('failed to git commit') unless ok
ok = system('git', 'push', 'origin', 'main')
raise('failed to git push') unless ok
puts '::set-output name=updated::yes'
end
def update_dockerfile(org, image, version) # rubocop:disable Metrics/MethodLength
old = File.read('Dockerfile').lines
breaker = false
File.open('Dockerfile', 'w') do |fh|
old.each do |line|
if !breaker && line =~ /^FROM ghcr\.io/
fh << "FROM ghcr.io/#{org}/#{image}:#{version}\n"
breaker = true
else
fh << line
end
end
end
end
def github_registry_bump(src)
org, image, current = parse_source(src)
latest = get_latest(src)
if latest == current
return scratch_bump if File.exist? 'stamp'
return
end
update_dockerfile(org, image, latest)
system('git', 'add', 'Dockerfile') || raise('failed to git add')
commit "Bumped source to #{latest}"
end
def scratch_bump
datestamp = DateTime.now.strftime('%Y%m%d-%H%M%S')
File.open('stamp', 'w') { |fh| fh << datestamp }
system('git', 'add', 'stamp') || raise('failed to git add')
commit "Bumping on #{datestamp}"
end
src_image = File.read('Dockerfile').lines.grep(/^FROM/).first.split[1]
case src_image
when 'scratch'
scratch_bump
when /^ghcr\.io/
github_registry_bump src_image
else
raise("Unknown source image: #{src_image}") unless File.exist? 'stamp'
scratch_bump
end