Skip to content

Commit 1057e5d

Browse files
committed
Add github release creation to canary release
1 parent dea2927 commit 1057e5d

6 files changed

Lines changed: 136 additions & 22 deletions

File tree

.gitlab/ci/release-coordinator.canary.gitlab-ci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ release-coordinator:canary:publish-containers:
6565
- bin/pyxis internal release_canary_publish_tags --coordinator-pipeline-id $CI_PIPELINE_ID
6666
variables:
6767
DRY_RUN: "false"
68+
69+
release-coordinator:canary:publish-release:
70+
extends:
71+
- .release-coordinator:canary
72+
stage: release-coordinator:canary:publish
73+
needs:
74+
- release-coordinator:canary:publish-containers
75+
script:
76+
- bin/pyxis internal release_canary_publish_release --coordinator-pipeline-id $CI_PIPELINE_ID
77+
variables:
78+
DRY_RUN: "false"

lib/pyxis/commands/internal.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,25 @@ class Internal < Thor
88
desc 'release_canary_tmp_branch', ''
99
method_option :build_id_to_promote, required: true, type: :numeric
1010
def release_canary_tmp_branch
11-
Release::Canary.new.create_build_branch(options[:build_id_to_promote])
11+
Pyxis::Release::Canary.new.create_build_branch(options[:build_id_to_promote])
1212
end
1313

1414
desc 'release_canary_tmp_branch_cleanup', ''
1515
method_option :build_id_to_promote, required: true, type: :numeric
1616
def release_canary_tmp_branch_cleanup
17-
Release::Canary.new.remove_build_branch(options[:build_id_to_promote])
17+
Pyxis::Release::Canary.new.remove_build_branch(options[:build_id_to_promote])
1818
end
1919

2020
desc 'release_canary_publish_tags', ''
2121
method_option :coordinator_pipeline_id, required: true, type: :numeric
2222
def release_canary_publish_tags
23-
Release::Canary.new.publish_tags(options[:coordinator_pipeline_id])
23+
Pyxis::Release::Canary.new.publish_tags(options[:coordinator_pipeline_id])
24+
end
25+
26+
desc 'release_canary_publish_release', ''
27+
method_option :coordinator_pipeline_id, required: true, type: :numeric
28+
def release_canary_publish_release
29+
Pyxis::Release::Canary.new.publish_release(options[:coordinator_pipeline_id])
2430
end
2531
end
2632
end

lib/pyxis/managed_versioning/component_info.rb

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ def initialize(build_id: nil, container_tag: nil)
1414

1515
# @return [Hash<Symbol, String>] The versions of each component in the build
1616
# @return [nil] If the build does not exist
17-
def execute
18-
@build_id = find_build_id_for_container_tag unless container_tag.nil?
17+
def execute(filter_components: nil)
18+
find_build_id_for_container_tag unless container_tag.nil?
1919

2020
return nil if build_id.nil?
2121

@@ -28,12 +28,13 @@ def execute
2828
manifests = find_manifests_from_jobs(jobs)
2929

3030
components = {
31-
reticulum: pipeline.body.sha,
31+
reticulum: (pipeline.body.sha if filter_components.nil? || filter_components.include?(:reticulum)),
3232
}
3333

3434
manifests.each do |image|
3535
component = image.first.to_sym
3636
next if components.key?(component)
37+
next if filter_components && !filter_components.include?(component)
3738

3839
image_tag = image.length == 1 ? container_version : "#{container_version}-#{image.last}"
3940

@@ -48,18 +49,22 @@ def execute
4849
end
4950

5051
def find_build_id_for_container_tag
51-
annotation_for(
52+
return build_id unless build_id.nil?
53+
54+
@build_id = annotation_for(
5255
'code0-tech/reticulum/ci-builds/mise',
5356
container_tag,
5457
'tech.code0.reticulum.pipeline.id'
5558
)
5659
end
5760

5861
def find_container_tag_for_build_id
62+
return container_tag unless container_tag.nil?
63+
5964
_, jobs = load_pipeline(build_id)
6065
return nil if jobs.nil?
6166

62-
find_container_version(jobs)
67+
@container_tag = find_container_version(jobs)
6368
end
6469

6570
def find_manifests

lib/pyxis/project/codezero.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# frozen_string_literal: true
2+
3+
module Pyxis
4+
module Project
5+
class Codezero < Base
6+
class << self
7+
def paths
8+
{
9+
github: 'code0-tech/codezero',
10+
}
11+
end
12+
end
13+
end
14+
end
15+
end

lib/pyxis/release/canary.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ def remove_build_branch(build_to_promote)
3838
end
3939

4040
def publish_tags(coordinator_pipeline_id)
41-
build_id = GitlabClient.client
42-
.list_pipeline_bridges(Project::Pyxis.api_gitlab_path, coordinator_pipeline_id)
43-
.find { |bridge| bridge['name'] == 'release-coordinator:canary:build' }
44-
.dig('downstream_pipeline', 'id')
41+
build_id = find_build_id(coordinator_pipeline_id)
4542

4643
info = ManagedVersioning::ComponentInfo.new(build_id: build_id)
4744
common = Common.new
@@ -50,6 +47,24 @@ def publish_tags(coordinator_pipeline_id)
5047

5148
raise Pyxis::MessageError, 'Failed to copy all container images' unless success
5249
end
50+
51+
def publish_release(coordinator_pipeline_id)
52+
build_id = find_build_id(coordinator_pipeline_id)
53+
54+
info = ManagedVersioning::ComponentInfo.new(build_id: build_id)
55+
common = Common.new
56+
57+
common.publish_github_release(info, prerelease: true)
58+
end
59+
60+
private
61+
62+
def find_build_id(coordinator_pipeline_id)
63+
GitlabClient.client
64+
.list_pipeline_bridges(Project::Pyxis.api_gitlab_path, coordinator_pipeline_id)
65+
.find { |bridge| bridge['name'] == 'release-coordinator:canary:build' }
66+
.dig('downstream_pipeline', 'id')
67+
end
5368
end
5469
end
5570
end

lib/pyxis/release/common.rb

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Common
1414
CONTAINER_IMAGES_TO_RELEASE = Project.components + %i[config-generator]
1515

1616
def copy_container_images_to_release_registry(component_info)
17-
container_tag = info.find_container_tag_for_build_id
17+
container_tag = component_info.find_container_tag_for_build_id
1818
container_tags = component_info.find_manifests.map do |manifest|
1919
next nil unless CONTAINER_IMAGES_TO_RELEASE.include?(manifest.first.to_sym)
2020

@@ -37,21 +37,22 @@ def copy_container_image_to_release_registry(tag)
3737
return if Pyxis::GlobalStatus.dry_run?
3838

3939
success = system("crane copy #{CI_BUILDS_PREFIX}#{tag} #{CONTAINER_RELEASE_PREFIX}#{tag}")
40-
logger.error('Failed to copy container image to release registry', tag: tag)
40+
logger.error('Failed to copy container image to release registry', tag: tag) unless success
4141
success
4242
end
4343
end
4444

4545
def with_release_registry_auth
46-
return yield if File.exist?('tmp/gitlab_token')
46+
token_exists = File.exist?('tmp/gitlab_token')
47+
return yield if token_exists
4748

4849
logger.info('Authentication with release registry')
4950
File.write('tmp/gitlab_token', Pyxis::Environment.gitlab_release_tools_token)
5051
success = system(
51-
"crane auth login
52-
-u #{CONTAINER_RELEASE_PUBLISH_USER}
53-
--password-stdin #{CONTAINER_RELEASE_REGISTRY}
54-
< tmp/gitlab_token"
52+
'crane auth login ' \
53+
"-u #{CONTAINER_RELEASE_PUBLISH_USER} " \
54+
"--password-stdin #{CONTAINER_RELEASE_REGISTRY} " \
55+
'< tmp/gitlab_token'
5556
)
5657

5758
unless success
@@ -61,9 +62,70 @@ def with_release_registry_auth
6162

6263
yield
6364
ensure
64-
system("crane auth logout #{CONTAINER_RELEASE_REGISTRY}")
65-
File.delete('tmp/gitlab_token')
66-
logger.info('Unauthenticated from release registry')
65+
unless token_exists
66+
system("crane auth logout #{CONTAINER_RELEASE_REGISTRY}")
67+
File.delete('tmp/gitlab_token')
68+
logger.info('Unauthenticated from release registry')
69+
end
70+
end
71+
72+
def publish_github_release(component_info, prerelease:)
73+
logger.info('Starting release to codezero repository', tag: component_info.find_container_tag_for_build_id)
74+
75+
release_version = component_info.find_container_tag_for_build_id
76+
reticulum_sha = component_info.execute(filter_components: [:reticulum])[:reticulum]
77+
78+
compose_path = 'docker-compose/docker-compose.yml'
79+
env_path = 'docker-compose/.env'
80+
81+
codezero_compose_content = GithubClient.octokit.contents(
82+
Project::Codezero.github_path,
83+
path: compose_path
84+
)
85+
codezero_env_content = GithubClient.octokit.contents(
86+
Project::Codezero.github_path,
87+
path: env_path
88+
)
89+
90+
reticulum_compose_content = GithubClient.octokit.contents(
91+
Project::Reticulum.github_path,
92+
path: compose_path,
93+
ref: reticulum_sha
94+
)
95+
reticulum_env_content = GithubClient.octokit.contents(
96+
Project::Reticulum.github_path,
97+
path: env_path,
98+
ref: reticulum_sha
99+
)
100+
101+
reticulum_env = Base64.decode64 reticulum_env_content.content
102+
reticulum_compose = Base64.decode64 reticulum_compose_content.content
103+
104+
codezero_env = reticulum_env.sub('IMAGE_TAG=', "IMAGE_TAG=#{release_version}")
105+
106+
GithubClient.octokit.update_contents(
107+
Project::Codezero.github_path,
108+
env_path,
109+
"Update compose env for #{release_version}",
110+
codezero_env_content.sha,
111+
codezero_env,
112+
Project::Codezero.default_branch
113+
)
114+
GithubClient.octokit.update_contents(
115+
Project::Codezero.github_path,
116+
compose_path,
117+
"Update compose file for #{release_version}",
118+
codezero_compose_content.sha,
119+
reticulum_compose,
120+
Project::Codezero.default_branch
121+
)
122+
123+
GithubClient.octokit.create_release(
124+
Project::Codezero.github_path,
125+
release_version,
126+
name: release_version,
127+
prerelease: prerelease
128+
)
67129
end
68130
end
69131
end

0 commit comments

Comments
 (0)