-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
252 lines (220 loc) · 7.05 KB
/
Copy pathRakefile
File metadata and controls
252 lines (220 loc) · 7.05 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# frozen_string_literal: true
require 'rake_git'
require 'rake_git_crypt'
require 'rake_github'
require 'rake_gpg'
require 'rake_slack'
require 'rspec/core/rake_task'
require 'rubocop/rake_task'
require 'securerandom'
task default: %i[
library:fix
test:unit
]
RakeGitCrypt.define_standard_tasks(
namespace: :git_crypt,
provision_secrets_task_name: :'secrets:provision',
destroy_secrets_task_name: :'secrets:destroy',
install_commit_task_name: :'git:commit',
uninstall_commit_task_name: :'git:commit',
gpg_user_key_paths: %w[
config/gpg
config/secrets/ci/gpg.public
]
)
namespace :git do
RakeGit.define_commit_task(
argument_names: [:message]
) do |t, args|
t.message = args.message
end
end
namespace :encryption do
namespace :directory do
desc 'Ensure CI secrets directory exists.'
task :ensure do
FileUtils.mkdir_p('config/secrets/ci')
end
end
namespace :passphrase do
desc 'Generate encryption passphrase for CI GPG key'
task generate: ['directory:ensure'] do
File.write(
'config/secrets/ci/encryption.passphrase',
SecureRandom.base64(36)
)
end
end
end
namespace :keys do
namespace :gpg do
RakeGPG.define_generate_key_task(
output_directory: 'config/secrets/ci',
name_prefix: 'gpg',
owner_name: 'InfraBlocks Maintainers',
owner_email: 'maintainers@infrablocks.io',
owner_comment: 'rake_process_manager CI Key'
)
end
end
namespace :secrets do
namespace :directory do
desc 'Ensure secrets directory exists and is set up correctly'
task :ensure do
FileUtils.mkdir_p('config/secrets')
unless File.exist?('config/secrets/.unlocked')
File.write('config/secrets/.unlocked', 'true')
end
end
end
desc 'Generate all generatable secrets.'
task generate: %w[
encryption:passphrase:generate
keys:gpg:generate
]
desc 'Provision all secrets.'
task provision: [:generate]
desc 'Delete all secrets.'
task :destroy do
rm_rf 'config/secrets'
end
desc 'Rotate all secrets.'
task rotate: [:'git_crypt:reinstall']
end
RuboCop::RakeTask.new
namespace :library do
desc 'Run all checks of the library'
task check: [:rubocop]
desc 'Attempt to automatically fix issues with the library'
task fix: [:'rubocop:autocorrect_all']
desc 'Build the library'
task :build do
sh 'gem build rake_process_manager.gemspec'
end
end
namespace :test do
RSpec::Core::RakeTask.new(:unit)
end
RakeGithub.define_repository_tasks(
namespace: :github,
repository: 'infrablocks/rake_process_manager'
) do |t|
# Operator's ambient auth. Resolve once and fail fast: a missing,
# unauthenticated, or absent gh yields an empty string, which would
# otherwise surface later as an opaque Octokit 401. An empty or
# whitespace-only GITHUB_TOKEN is treated as absent so an authenticated
# operator falls through to `gh auth token` rather than hitting the raise.
github_token = ENV['GITHUB_TOKEN'].to_s.strip
if github_token.empty?
github_token = begin
`gh auth token`
rescue Errno::ENOENT
''
end.strip
end
if github_token.empty?
raise 'No GitHub token available: set GITHUB_TOKEN or run `gh auth login`'
end
t.access_token = github_token
# Actions store only: dependabot runs never reach the passphrase — the
# only pr.yaml job that unlocks git-crypt (prerelease) is guarded to
# same-repo human PRs. Guard against a locked clone: without
# it, File.read returns git-crypt ciphertext and github:secrets:ensure
# silently uploads garbage that only surfaces much later as an opaque
# GPG unlock failure in the release job.
passphrase_path = 'config/secrets/ci/encryption.passphrase'
unless File.exist?(passphrase_path)
raise "Passphrase file not found: #{passphrase_path} — expected a " \
'git-crypt-unlocked clone with the CI secrets present'
end
passphrase = File.binread(passphrase_path)
if passphrase.start_with?("\x00GITCRYPT")
raise 'encryption.passphrase is git-crypt ciphertext — unlock the ' \
'clone before provisioning'
end
t.secrets = [
{ name: 'ENCRYPTION_PASSPHRASE', value: passphrase.chomp }
]
t.environments = [
{ name: 'release',
reviewers: [{ team: 'maintainers' }] }
]
end
namespace :slack do
RakeSlack.define_notification_tasks do |t|
t.bot_token = ENV.fetch('SLACK_BOT_TOKEN', nil)
t.routing_rules = [
{ when: { type: 'on_hold' },
channel: 'C038EDCRSQJ', format: :on_hold }, # release
{ when: { actor: 'dependabot[bot]', outcome: 'success' },
channel: 'C03N711HVDG', format: :success }, # builds-dependabot
{ when: { actor: 'dependabot[bot]' },
channel: 'C03N711HVDG', format: :failure }, # builds-dependabot
{ when: { outcome: 'success' },
channel: 'C023XUE76GH', format: :success }, # builds
# Failures go to builds, not team-dev (org default), to keep noise
# out of a popular channel while this pipeline beds in.
{ when: {},
channel: 'C023XUE76GH', format: :failure } # builds
]
end
end
namespace :repository do
desc 'Set the git author for CI'
task :set_ci_author do
sh 'git config --global user.name "InfraBlocks CI"'
sh 'git config --global user.email "ci@infrablocks.io"'
end
end
namespace :pipeline do
desc 'Prepare GitHub Actions pipeline'
task prepare: %i[
github:secrets:ensure
github:environments:ensure
]
end
namespace :version do
desc 'Bump version for specified type (pre, major, minor, patch)'
task :bump, [:type] do |_, args|
bump_version_for(args.type)
end
end
desc 'Release gem'
task :release do
sh 'gem release --tag --push'
end
namespace :prerelease do
desc 'Build and push a namespaced pre-release to RubyGems ' \
'(PR CI only; no bump, no tag, no commit, no push)'
task :publish, %i[pr_number run_number run_attempt] do |_, args|
%i[pr_number run_number run_attempt].each do |name|
raise "Missing task argument: #{name}" if args[name].to_s.empty?
end
version_file = 'lib/rake_process_manager/version.rb'
version_pattern = /(VERSION\s*=\s*')([^']+)(')/
source = File.read(version_file)
base = source[version_pattern, 2]
raise "Could not read VERSION from #{version_file}" unless base
version = "#{base}.pr#{args.pr_number}" \
".#{args.run_number}.#{args.run_attempt}"
gem_file = "rake_process_manager-#{version}.gem"
begin
File.write(version_file,
source.sub(version_pattern, "\\1#{version}\\3"))
# Build + push directly: `gem release` aborts on the (deliberately)
# uncommitted version rewrite. PR CI must not tag, commit, or push
# (contrast the `release` task).
sh 'gem build rake_process_manager.gemspec'
sh "gem push #{gem_file}"
ensure
File.write(version_file, source)
rm_f gem_file
end
end
end
def bump_version_for(version_type)
sh "gem bump --version #{version_type} " \
'&& bundle install ' \
'&& export LAST_MESSAGE="$(git log -1 --pretty=%B)" ' \
'&& git commit -a --amend -m "${LAST_MESSAGE} [ci skip]"'
end