|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Pyxis |
| 4 | + module Commands |
| 5 | + class Internal < Thor |
| 6 | + include Thor::Actions |
| 7 | + |
| 8 | + RETICULUM_CI_BUILDS_PREFIX = 'ghcr.io/code0-tech/reticulum/ci-builds/' |
| 9 | + CONTAINER_RELEASE_PREFIX = 'registry.gitlab.com/code0-tech/packages/' |
| 10 | + |
| 11 | + desc 'release_canary_tmp_branch', '' |
| 12 | + method_option :build_id_to_promote, required: true, type: :numeric |
| 13 | + def release_canary_tmp_branch |
| 14 | + component_information = Pyxis::ManagedVersioning::ComponentInfo.new( |
| 15 | + build_id: options[:build_id_to_promote] |
| 16 | + ).execute |
| 17 | + |
| 18 | + raise 'Build not found' if component_information.nil? |
| 19 | + |
| 20 | + GitlabClient.client.create_branch( |
| 21 | + Project::Reticulum.api_gitlab_path, |
| 22 | + "pyxis/canary-build/#{options[:build_id_to_promote]}", |
| 23 | + component_information[:reticulum] |
| 24 | + ) |
| 25 | + |
| 26 | + version_variables = component_information.map do |component, version| |
| 27 | + next nil unless Project.components.include?(component) |
| 28 | + |
| 29 | + ["OVERRIDE_#{component}_VERSION", version] |
| 30 | + end.compact |
| 31 | + |
| 32 | + create_env_file( |
| 33 | + 'reticulum_variables', |
| 34 | + version_variables + [['C0_GH_TOKEN', Pyxis::Environment.github_reticulum_publish_token]] |
| 35 | + ) |
| 36 | + end |
| 37 | + |
| 38 | + desc 'release_canary_tmp_branch_cleanup', '' |
| 39 | + method_option :build_id_to_promote, required: true, type: :numeric |
| 40 | + def release_canary_tmp_branch_cleanup |
| 41 | + GitlabClient.client.delete_branch( |
| 42 | + Project::Reticulum.api_gitlab_path, |
| 43 | + "pyxis/canary-build/#{options[:build_id_to_promote]}" |
| 44 | + ) |
| 45 | + end |
| 46 | + |
| 47 | + desc 'release_canary_publish_tags', '' |
| 48 | + method_option :coordinator_pipeline_id, required: true, type: :numeric |
| 49 | + def release_canary_publish_tags |
| 50 | + build_id = GitlabClient.client |
| 51 | + .list_pipeline_bridges(Project::Pyxis.api_gitlab_path, options[:coordinator_pipeline_id]) |
| 52 | + .find { |bridge| bridge['name'] == 'release-coordinator:canary:build' } |
| 53 | + .dig('downstream_pipeline', 'id') |
| 54 | + |
| 55 | + info = ManagedVersioning::ComponentInfo.new(build_id: build_id) |
| 56 | + container_tag = info.find_container_tag_for_build_id |
| 57 | + container_tags = info.find_manifests.map do |manifest| |
| 58 | + next nil unless Project.components.include?(manifest.first.to_sym) |
| 59 | + |
| 60 | + next "#{manifest.first}:#{container_tag}" if manifest.length == 1 |
| 61 | + |
| 62 | + "#{manifest.first}:#{container_tag}-#{manifest.last}" |
| 63 | + end.compact |
| 64 | + |
| 65 | + File.write('tmp/gitlab_token', Pyxis::Environment.gitlab_release_tools_token) |
| 66 | + run 'crane auth login -u code0-release-tools --password-stdin registry.gitlab.com < tmp/gitlab_token' |
| 67 | + |
| 68 | + overall_success = true |
| 69 | + |
| 70 | + original_pretend = options[:pretend] |
| 71 | + options[:pretend] = Pyxis::GlobalStatus.dry_run? |
| 72 | + container_tags.each do |tag| |
| 73 | + success = run "crane copy #{RETICULUM_CI_BUILDS_PREFIX}#{tag} #{CONTAINER_RELEASE_PREFIX}#{tag}", |
| 74 | + abort_on_failure: false |
| 75 | + overall_success &&= success |
| 76 | + |
| 77 | + logger.error('Failed to copy container image to release registry', image: tag) unless success |
| 78 | + end |
| 79 | + options[:pretend] = original_pretend |
| 80 | + |
| 81 | + run 'crane auth logout registry.gitlab.com' |
| 82 | + File.delete('tmp/gitlab_token') |
| 83 | + |
| 84 | + abort unless overall_success || Pyxis::GlobalStatus.dry_run? |
| 85 | + end |
| 86 | + |
| 87 | + no_commands do |
| 88 | + include SemanticLogger::Loggable |
| 89 | + |
| 90 | + def create_env_file(name, variables) |
| 91 | + path = File.absolute_path(File.join(__FILE__, "../../../../tmp/#{name}.env")) |
| 92 | + File.write(path, variables.map { |k, v| "#{k}=#{v}" }.join("\n")) |
| 93 | + end |
| 94 | + end |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments