-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
582 lines (477 loc) Β· 19.6 KB
/
Rakefile
File metadata and controls
582 lines (477 loc) Β· 19.6 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
# frozen_string_literal: true
require 'fileutils'
require 'json'
require 'asciidoctor'
require 'yaml'
require 'date'
# Load DocOps Lab development tooling
require 'docopslab/dev'
# Configuration
JEKYLL_CONFIG = YAML.load_file('_config.yml')
DEPLOY_BRANCH = 'gh-pages'
BUILD_DIR = JEKYLL_CONFIG['destination'] || '_site'
SLIDES_DIR = 'slides'
PROJECTS_DATA = YAML.safe_load_file('_data/docops-lab-projects.yml', permitted_classes: [Date])
desc 'Extract AsciiDoc attributes for single-sourcing'
task :extract_readme_attrs do
puts 'π Extracting AsciiDoc attributes from README.adoc...'
attrs = {}
doc = Asciidoctor.load_file('README.adoc', safe: :safe)
sensitive_keys = %w[user-home docfile docdir]
sensitive_keys.each { |key| doc.attributes.delete(key) if doc.attributes.key?(key) }
doc.attributes.each do |key, value|
if key.end_with?('-desc') && value && !value.empty?
attrs[key.sub('-desc', '')] = value
else
attrs[key] = value
end
end
FileUtils.mkdir_p('_data/built')
File.write('_data/built/attrs.yml', attrs.to_yaml)
puts 'β
Extracted to _data/built/attrs.yml'
end
desc 'Validate projects YAML file'
task :validate_projects do
require_relative 'scripts/validate-projects-yaml'
file_path = '_data/docops-lab-projects.yml'
puts "π Validating #{file_path}..."
validator = ProjectsYAMLValidator.new(file_path)
exit 1 unless validator.validate
end
# Utility: Convert arbitrary HTML to Markdown using our ReverseMarkdown extensions
namespace :util do
desc 'Convert HTML to Markdown (args: source, dest). Uses MarkDownGrade and writes to .agent by default.'
task :html_to_md, [:source, :dest] do |_, args|
require 'nokogiri'
require_relative 'scripts/mark_down_grade'
MarkDownGrade.bootstrap!
source = args[:source] || File.join(BUILD_DIR, 'metablog', 'tech-blogging-in-asciidoc', 'index.html')
dest = args[:dest] || File.join('.agent', 'converted', 'metablog-tech-blogging-in-asciidoc.md')
unless File.exist?(source)
puts "π¨ Building site to generate #{source}..."
Rake::Task['build_site'].invoke
end
unless File.exist?(source)
puts "β Source HTML not found: #{source}"
next
end
html = File.read(source)
doc = Nokogiri::HTML(html)
# Prefer specific content wrappers to avoid bringing site chrome
container = doc.at_css('div.document-body') ||
doc.at_css('div.post-content.metablog') ||
doc.at_css('div.post-content') ||
doc.at_css('article.blog-post .post-content') ||
doc.at_css('article.blog-post') ||
doc.at_css('article') ||
doc.at_css('main') ||
doc.at_css('body')
if container.nil?
puts "β Could not find content container in #{source}"
next
end
# Remove non-content bits inside the container
container.css(
'script,
style,
nav,
.post-navigation,
.back-to-metablog,
.back-to-blog,
.metablog-banner,
footer').remove
markdown = MarkDownGrade.convert(container.inner_html, github_flavored: true)
FileUtils.mkdir_p(File.dirname(dest))
File.write(dest, markdown)
puts "β
Wrote #{dest}"
end
end
desc 'Generate project pages from project data (AsciiDoc)'
task :generate_project_pages do
puts 'π Generating project pages (.adoc)...'
# Create _projects directory if it doesn't exist
FileUtils.mkdir_p('_projects')
# Load project data
projects_data = PROJECTS_DATA
projects = projects_data['projects']
# select only projects with page or star (not nil)
paged_projects = projects.select { |p| p['page'] || p['star'] }
# Clear existing project pages
Dir.glob('_projects/*.adoc').each { |f| File.delete(f) }
paged_projects.each do |project|
slug = project['slug'] || project['name']&.downcase&.gsub(/[^a-z0-9\-_]/, '-')
next unless slug
filename = "_projects/#{slug}.adoc"
# Generate frontmatter
project_name = project['name'] || slug.split('-').map(&:capitalize).join(' ')
frontmatter = {
'layout' => 'projects',
'title' => project_name,
'slug' => slug,
'type' => 'profile',
'generated' => true,
'generation_date' => Time.now.strftime('%Y-%m-%d %H:%M:%S'),
'liquid' => true
}
# Add optional fields if present
frontmatter['category'] = project['type'] if project['type']
frontmatter['status'] = project['done'] ? 'live' : 'development' if project.key?('live')
frontmatter['tags'] = project['tags'] if project['tags']
# Create the file content
content = "#{frontmatter.to_yaml}---\n\n"
content += "// This page is auto-generated by rake generate_project_pages\n"
content += "// Source data: _data/docops-lab-projects.yml\n\n"
content += "++++\n"
content += "{% include project-page.html slug=page.slug %}\n"
content += "++++\n"
# Write the file
File.write(filename, content)
puts " β Generated #{filename}"
end
puts "β
Generated #{paged_projects.count} project pages (.adoc)"
end
desc 'Generate metadata master files for projects'
task :generate_metadata do
puts 'π Generating project metadata...'
# Load project data
projects_data = PROJECTS_DATA
projects = projects_data['projects']
# Collect unique tags and tech and make a types dictionary
tags = projects.flat_map { |p| p['tags'] || [] }.uniq.sort
tech = projects.flat_map { |p| p['tech'] || [] }.uniq.sort
# generate type parameters from $meta['types'] based on text: defaulting to head:
# should yield like:
# types:
# content: Content Repos
# rest-api: REST API
types = projects_data['$meta']['types'].each_with_object({}) do |type_info, hash|
slug = type_info['slug']
display_text = type_info['text'] || type_info['slug']
hash[slug] = display_text
end
# Write to metadata file
metadata = {
'tags' => tags,
'tech' => tech,
'types' => types
}
File.write('_data/built/projects_metadata.yml', metadata.to_yaml)
puts 'β
Project metadata generated at _data/built/projects_metadata.yml'
end
desc 'Copy the Jekyll-AsciiDoc UI config definition file'
task :copy_jekyll_ui_config do
pwd = Dir.pwd
source = '../jekyll-asciidoc-ui/specs/config-def.yml'
if pwd == '/workspace'
puts "β οΈ Warning: Containerized environment cannot see #{source}; skipping copy."
next
end
dest_dir = '_data/'
dest = File.join(dest_dir, 'jekyll-asciidoc-ui-config-def.yml')
unless File.exist?(source)
puts "β οΈ Source file #{source} does not exist. Skipping jekyll-asciidoc-ui config copy (CI/standalone mode)."
next
end
FileUtils.mkdir_p(dest_dir)
FileUtils.cp(source, dest)
puts "β
Copied Jekyll-AsciiDoc UI config definition to #{dest}"
end
desc 'Write RuboCop styles configuration file'
task :write_rubocop_styles do
FileUtils.mkdir_p('_docs/partials/built')
build_cmd = 'bundle exec ruby scripts/rubocop_styles_adoc.rb ' \
'gems/docopslab-dev/assets/config-packs/rubocop/base.yml > ' \
'_docs/partials/built/_rubocop-styles.adoc'
system(build_cmd) or raise 'Failed to generate RuboCop styles'
puts 'β
RuboCop styles written to _docs/partials/built/_rubocop-styles.adoc'
end
desc 'Render an AsciiDoc file of universal attributes'
# use _data/docops-lab-projects.yml data and the Liquid template at _includes/docpslab-universal-attributes.asciidoc to produce a file at _docs/partials/built/_docopslab-universal-attributes.adoc
task :generate_universal_attributes do
puts 'π Generating universal attributes AsciiDoc file...'
# Load project data
projects_data = PROJECTS_DATA
# Prepare Liquid template
template_path = '_includes/docopslab-universal-attributes.asciidoc'
unless File.exist?(template_path)
puts "β Template file not found: #{template_path}"
exit 1
end
template_content = File.read(template_path)
# Render Liquid template with project data
require 'liquid'
liquid_template = Liquid::Template.parse(template_content)
rendered_content = liquid_template.render('site' => { 'data' => { 'docops-lab-projects' => projects_data } })
# Write to destination file
dest_path = '_docs/partials/built/_docopslab-universal-attributes.adoc'
FileUtils.mkdir_p(File.dirname(dest_path))
File.write(dest_path, rendered_content)
puts "β
Generated universal attributes at #{dest_path}"
end
desc 'Build DocOpsLab Vale package for distribution'
task :build_vale_package do
system('bundle exec ruby scripts/build_vale_package.rb') or raise 'Failed to build Vale package'
end
desc 'Build the Jekyll site (with single-sourced cards and project pages)'
task build_site: %i[extract_readme_attrs generate_project_pages generate_metadata copy_jekyll_ui_config
write_rubocop_styles generate_universal_attributes gemdo:gen_agent_docs] do
puts 'π¨ Building Jekyll site...'
system('bundle exec jekyll build') or raise 'Jekyll build failed'
puts 'β
Build complete'
end
desc 'Serve the site locally for development (with single-sourced cards and project pages)'
task serve: [:build_site] do
port = ENV['PORT'] || '4000'
puts "π Starting Jekyll development server on port #{port}..."
serve_cmd = "bundle exec jekyll serve --watch --livereload --port #{port} --skip-initial-build"
system(serve_cmd) or raise 'Jekyll serve failed'
end
namespace :review do
desc 'Fetch and serve a PR review build from GitHub Actions artifact'
task :serve do
require 'fileutils'
require 'tmpdir'
require 'json'
sha = ENV['SHA'] || abort('β SHA environment variable required (e.g., SHA=abc123 rake review:serve)')
port = ENV['PORT'] || '4001'
repo = 'DocOps/lab'
puts "π Fetching artifact for commit #{sha[0..7]}..."
# Check if gh CLI is available
unless system('which gh > /dev/null 2>&1')
abort('β GitHub CLI (gh) not found. Install using your system\'s package manager (or see https://cli.github.com/)')
end
# Create temp directory for review
review_dir = File.join(Dir.tmpdir, 'docops-lab-review', sha)
FileUtils.mkdir_p review_dir
# Find workflow run for this SHA
puts 'π Finding workflow run...'
run_json = `gh run list --repo #{repo} --json databaseId,status,conclusion,name,headSha --limit 20`
runs = JSON.parse(run_json)
# Filter for runs matching this SHA and the main workflow
matching_runs = runs.select { |r| r['headSha'].start_with?(sha) && r['name'] == 'Main CI/CD Pipeline' }
abort("β No workflow runs found for commit #{sha[0..7]}") if matching_runs.empty?
run = matching_runs.find { |r| r['status'] == 'completed' && r['conclusion'] == 'success' }
if run.nil?
in_progress = matching_runs.find { |r| r['status'] == 'in_progress' }
if in_progress
abort('β³ Workflow is still running for this commit. Wait for it to complete.')
else
abort("β No successful workflow run found for commit #{sha[0..7]}")
end
end
run_id = run['databaseId']
puts "β Found workflow run ##{run_id}"
# Get artifacts for this run to find the actual site artifact name
puts 'π Finding site artifact...'
artifacts_json = `gh api repos/#{repo}/actions/runs/#{run_id}/artifacts`
artifacts = JSON.parse(artifacts_json)['artifacts']
site_artifact = artifacts.find { |a| a['name'].start_with?('site-') }
abort('β No site artifact found for this run') unless site_artifact
artifact_name = site_artifact['name']
# Check if site is already cached
if File.exist?(File.join(review_dir, 'index.html'))
puts "β Using cached site from #{review_dir}"
else
puts "π¦ Downloading #{artifact_name}..."
Dir.chdir(review_dir) do
result = system("gh run download #{run_id} --repo #{repo} --name #{artifact_name}")
abort('β Failed to download artifact') unless result
end
end
puts "β
Review site ready at #{review_dir}"
puts "π Starting server on http://localhost:#{port}..."
puts ' (Press Ctrl+C to stop)'
puts ''
# Serve with Jekyll, specifying the source directory
serve_cmd = "bundle exec jekyll serve --watch --port #{port} --skip-initial-build --source #{review_dir}"
system(serve_cmd) or abort('β Server failed')
end
end
desc 'Clean build artifacts'
task :clean do
puts 'π§Ή Cleaning build artifacts...'
FileUtils.rm_rf(BUILD_DIR)
FileUtils.rm_rf('.jekyll-cache')
puts 'β
Clean complete'
end
desc 'Update slides from docs-as-code-school repo'
task :update_slides do
puts 'π Updating slides from docs-as-code-school...'
system('./scripts/copy-slides.sh') or raise 'Slides update failed'
puts 'β
Slides updated'
end
desc 'Switch to gh-pages branch (for manual inspection)'
task :switch_to_deploy do
current_branch = `git branch --show-current`.strip
unless `git status --porcelain`.strip.empty?
puts 'β You have uncommitted changes. Please commit them first.'
exit 1
end
puts "π¦ Switching to #{DEPLOY_BRANCH} branch..."
system("git checkout #{DEPLOY_BRANCH}") or raise "Failed to checkout #{DEPLOY_BRANCH}"
puts "β
Now on #{DEPLOY_BRANCH} branch. Use 'git checkout #{current_branch}' to return."
end
desc 'Prepare deployment files (SAFE - does not commit or push)'
task prepare_deploy: %i[clean build_site] do
puts 'π Preparing deployment files...'
# Save current branch
current_branch = `git branch --show-current`.strip
# Check if we have uncommitted changes
unless `git status --porcelain`.strip.empty?
puts 'β You have uncommitted changes. Please commit them first.'
exit 1
end
# Store the current commit hash
`git rev-parse HEAD`.strip
begin
# Switch to deploy branch
puts "π¦ Switching to #{DEPLOY_BRANCH} branch..."
system("git checkout #{DEPLOY_BRANCH}") or raise "Failed to checkout #{DEPLOY_BRANCH}"
# Clear everything except .git and slides/
puts 'π§Ή Clearing deploy branch (preserving slides/)...'
Dir.glob('*', File::FNM_DOTMATCH).each do |item|
next if ['.', '..', '.git', SLIDES_DIR].include?(item)
FileUtils.rm_rf(item)
end
# Copy built site contents to root
puts 'π Copying built site to deploy branch...'
Dir.glob("#{BUILD_DIR}/*", File::FNM_DOTMATCH).each do |item|
next if ['.', '..'].include?(File.basename(item))
dest = File.basename(item)
# Don't overwrite slides/ if it exists in the build
if dest == SLIDES_DIR && File.exist?(SLIDES_DIR)
puts "β οΈ Preserving existing #{SLIDES_DIR}/ directory"
next
end
FileUtils.cp_r(item, dest)
end
# Create a minimal .gitignore for gh-pages branch
File.write('.gitignore', <<~GITIGNORE)
# Only ignore truly temporary files in the deploy branch
.DS_Store
*.tmp
*.log
GITIGNORE
puts 'β
Deployment files prepared!'
puts "π You are now on the #{DEPLOY_BRANCH} branch."
puts 'π Review the changes with: git status'
puts 'π¦ Commit when ready with: rake commit_deploy'
puts "π Return to main branch with: git checkout #{current_branch}"
rescue StandardError => e
puts "β Preparation failed: #{e.message}"
# Always return to original branch on error
puts "π Returning to #{current_branch} branch..."
system("git checkout #{current_branch}")
exit 1
end
end
desc 'Commit deployment (run this after prepare_deploy and review)'
task :commit_deploy do
current_branch = `git branch --show-current`.strip
unless current_branch == DEPLOY_BRANCH
puts "β You must be on the #{DEPLOY_BRANCH} branch to commit deployment."
puts "οΏ½ Run 'rake prepare_deploy' first."
exit 1
end
# Get the main branch commit hash for reference
main_commit = `git rev-parse main`.strip
puts 'οΏ½πΎ Committing deployment...'
system('git add -A')
commit_message = "Deploy from main branch (#{main_commit[0..7]})"
if system("git commit -m '#{commit_message}'")
puts 'β
Deployment committed!'
puts 'π Push to origin with: rake push_deploy'
puts 'π Return to main branch with: git checkout main'
else
puts 'β οΈ No changes to commit or commit failed'
end
end
desc 'Push deployment to origin (DANGER: this updates the live site!)'
task :push_deploy do
current_branch = `git branch --show-current`.strip
unless current_branch == DEPLOY_BRANCH
puts "β You must be on the #{DEPLOY_BRANCH} branch to push deployment."
puts "π‘ Run 'rake prepare_deploy' and 'rake commit_deploy' first."
exit 1
end
puts 'β οΈ WARNING: This will update the live site at https://docopslab.org'
puts 'π€ Are you sure you want to continue? (yes/no)'
response = $stdin.gets.chomp.downcase
unless %w[yes y].include?(response)
puts 'β Push cancelled.'
exit 0
end
puts 'π Pushing to origin...'
if system("git push origin #{DEPLOY_BRANCH}")
puts 'β
Deployment pushed to origin!'
puts 'π Site updated at: https://docopslab.org'
else
puts 'β Push failed!'
exit 1
end
end
desc 'Full deployment (prepare + commit + push)'
task deploy: %i[prepare_deploy commit_deploy push_deploy]
desc 'Safe deployment workflow (prepare only - no commit/push)'
task deploy_safe: [:prepare_deploy]
desc 'Deploy with slides update (full workflow)'
task deploy_with_slides: %i[update_slides deploy]
desc 'Safe deploy with slides update (prepare only)'
task deploy_with_slides_safe: %i[update_slides deploy_safe]
desc 'Return to main branch from gh-pages'
task :return_to_main do
current_branch = `git branch --show-current`.strip
if current_branch == DEPLOY_BRANCH
puts 'π Returning to main branch...'
system('git checkout main')
puts 'β
Back on main branch'
else
puts "βΉοΈ Already on #{current_branch} branch"
end
end
# namespace 'gemdo' for docopslab-dev gem/project related tasks
namespace :gemdo do
desc 'Build the DocOps Lab Dev Docker image'
task :build_docker do
Rake::Task['build_site'].invoke
# get the image version
version = DocOpsLab::Dev::VERSION
puts "π³ Building DocOps Lab Dev Docker image version #{version}..."
build_cmd = "VERSION=#{version} ./gems/docopslab-dev/build-docker.sh"
system(build_cmd) or raise 'Failed to build DocOps Lab Dev Docker image'
puts 'β
Docker image built successfully'
end
desc 'Build docopslab-dev gem package to gems/docopslab-dev/pkg/'
task :build_gem do
Rake::Task['gemdo:gen_agent_docs'].invoke
puts 'π Building docopslab-dev gem package...'
Dir.chdir('gems/docopslab-dev') do
system('gem build docopslab-dev.gemspec') or raise 'Failed to build docopslab-dev gem'
FileUtils.mkdir_p('pkg')
built_gem = Dir.glob('docopslab-dev-*.gem').max_by { |f| File.mtime(f) }
FileUtils.mv(built_gem, 'pkg/')
puts "β
docopslab-dev gem built at gems/docopslab-dev/pkg/#{File.basename(built_gem)}"
end
end
desc 'Generate agent documentation for docopslab-dev gem'
task :gen_agent_docs do
require_relative 'scripts/gen_agent_docs'
# Build Jekyll site if not already built (for agent docs HTML)
unless Dir.exist?(BUILD_DIR) && !Dir.empty?(BUILD_DIR)
puts 'π Building Jekyll site for agent docs HTML...'
system('bundle exec jekyll build') or raise 'Jekyll build failed'
end
# Run the generation script
GenAgentDocs.run(BUILD_DIR)
end
desc 'Test all labdev rake tasks using definitions from tasks-def.yml'
task :test_tasks, [:filter1, :filter2, :filter3] do |_t, args|
puts 'π§ͺ Running labdev tasks test suite...'
# Build command with optional filters
cmd = 'ruby scripts/test_labdev_tasks.rb'
# Collect all non-nil filter arguments
filters = [args[:filter1], args[:filter2], args[:filter3]].compact
cmd += " #{filters.join(' ')}" if filters.any?
system(cmd) or raise 'Task tests failed'
end
end