Skip to content

Commit 97c697c

Browse files
authored
Merge pull request #19 from code0-tech/coordinator-notifications
Add coordinator notifications
2 parents 417fb23 + 8a858c7 commit 97c697c

7 files changed

Lines changed: 115 additions & 3 deletions

File tree

.gitlab-ci.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ stages:
66
- components
77
- !reference [.release-coordinator:canary:stages]
88

9+
workflow:
10+
name: $PIPELINE_NAME
11+
rules:
12+
- if: $CI_PIPELINE_SOURCE == "push"
13+
variables:
14+
PIPELINE_NAME: "Build pyxis image for $CI_COMMIT_SHORT_SHA"
15+
- if: $PIPELINE_NAME != null
16+
917
default:
1018
image: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
1119
tags:

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
.release-coordinator:canary:stages:
2+
- release-coordinator:canary:notify-start
23
- release-coordinator:canary:build
34
- release-coordinator:canary:publish
5+
- release-coordinator:canary:notify-finish
46

57
.release-coordinator:canary:
68
rules:
79
- if: $RELEASE_COORDINATOR == "canary"
810

11+
release-coordinator:canary:notify-start:
12+
extends:
13+
- .release-coordinator:canary
14+
stage: release-coordinator:canary:notify-start
15+
script:
16+
- bin/pyxis internal notify_new_coordinator --coordinator-pipeline-id $CI_PIPELINE_ID
17+
variables:
18+
DRY_RUN: "false"
19+
920
release-coordinator:canary:tmp-branch:
1021
extends:
1122
- .release-coordinator:canary
@@ -55,6 +66,14 @@ release-coordinator:canary:publish:
5566
- echo "Publishing approved"
5667
when: manual
5768

69+
release-coordinator:canary:notify-publish-pending:
70+
extends:
71+
- .release-coordinator:canary
72+
stage: release-coordinator:canary:publish
73+
needs: !reference [release-coordinator:canary:publish, needs]
74+
script:
75+
- bin/pyxis internal release_notify_publish_pending
76+
5877
release-coordinator:canary:publish-containers:
5978
extends:
6079
- .release-coordinator:canary
@@ -76,3 +95,12 @@ release-coordinator:canary:publish-release:
7695
- bin/pyxis internal release_canary_publish_release --coordinator-pipeline-id $CI_PIPELINE_ID
7796
variables:
7897
DRY_RUN: "false"
98+
99+
release-coordinator:canary:notify-finish:
100+
extends:
101+
- .release-coordinator:canary
102+
stage: release-coordinator:canary:notify-finish
103+
script:
104+
- bin/pyxis internal notify_finish_coordinator --coordinator-pipeline-id $CI_PIPELINE_ID
105+
variables:
106+
DRY_RUN: "false"

lib/pyxis/commands/internal.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,54 @@ def release_canary_publish_tags
2828
def release_canary_publish_release
2929
Pyxis::Release::Canary.new.publish_release(options[:coordinator_pipeline_id])
3030
end
31+
32+
desc 'release_notify_publish_pending', ''
33+
method_option :coordinator_pipeline_id, required: true, type: :numeric
34+
def release_notify_publish_pending
35+
pipeline = GitlabClient.client.get_pipeline(
36+
Project::Pyxis.api_gitlab_path,
37+
options[:coordinator_pipeline_id]
38+
).body
39+
raise 'Pipeline not found' if pipeline.nil?
40+
41+
Pyxis::DiscordClient.new.send_notification(<<~DESC, :warn)
42+
Coordinator pipeline awaiting approval for release publishing
43+
#{"> #{pipeline.name}" if pipeline.name}
44+
#{pipeline.web_url}
45+
DESC
46+
end
47+
48+
desc 'notify_new_coordinator', ''
49+
method_option :coordinator_pipeline_id, required: true, type: :numeric
50+
def notify_new_coordinator
51+
pipeline = GitlabClient.client.get_pipeline(
52+
Project::Pyxis.api_gitlab_path,
53+
options[:coordinator_pipeline_id]
54+
).body
55+
raise 'Pipeline not found' if pipeline.nil?
56+
57+
Pyxis::DiscordClient.new.send_notification(<<~DESC)
58+
New coordinator pipeline started
59+
#{"> #{pipeline.name}" if pipeline.name}
60+
#{pipeline.web_url}
61+
DESC
62+
end
63+
64+
desc 'notify_finish_coordinator', ''
65+
method_option :coordinator_pipeline_id, required: true, type: :numeric
66+
def notify_finish_coordinator
67+
pipeline = GitlabClient.client.get_pipeline(
68+
Project::Pyxis.api_gitlab_path,
69+
options[:coordinator_pipeline_id]
70+
).body
71+
raise 'Pipeline not found' if pipeline.nil?
72+
73+
Pyxis::DiscordClient.new.send_notification(<<~DESC)
74+
Coordinator pipeline has finished
75+
#{"> #{pipeline.name}" if pipeline.name}
76+
#{pipeline.web_url}
77+
DESC
78+
end
3179
end
3280
end
3381
end

lib/pyxis/commands/release.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def create_canary
3333
Project::Pyxis.api_gitlab_path,
3434
Project::Pyxis.default_branch,
3535
variables: {
36+
PIPELINE_NAME: "Release build #{build_id} as canary",
3637
RELEASE_COORDINATOR: 'canary',
3738
BUILD_ID_TO_PROMOTE: build_id.to_s,
3839
}

lib/pyxis/discord_client.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# frozen_string_literal: true
2+
3+
module Pyxis
4+
class DiscordClient
5+
LOG_CHANNEL = 1478849342101000222
6+
7+
COLOR_MAPPING = {
8+
info: '#4caf50',
9+
warn: '#ff8000',
10+
error: '#f44336',
11+
}.freeze
12+
13+
attr_reader :bot
14+
15+
def initialize
16+
@bot = Discordrb::Bot.new(token: Pyxis::Environment.discord_bot_token)
17+
end
18+
19+
def send_notification(message, severity = :info)
20+
embed = Discordrb::Webhooks::Embed.new(description: message, color: COLOR_MAPPING[severity])
21+
22+
bot.send_message(LOG_CHANNEL, nil, false, [embed])
23+
end
24+
end
25+
end

lib/pyxis/gitlab_client.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ def create_pipeline(project_path_or_id, ref, variables: nil)
9595
)
9696
end
9797

98+
def get_pipeline(project_path_or_id, id)
99+
get_json("/api/v4/projects/#{project_path_or_id}/pipelines/#{id}")
100+
end
101+
98102
def list_pipeline_bridges(project_path_or_id, pipeline_id)
99103
paginate_json("/api/v4/projects/#{project_path_or_id}/pipelines/#{pipeline_id}/bridges")
100104
end

lib/pyxis/managed_versioning/component_info.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ def find_container_version(jobs)
141141
end
142142

143143
def load_pipeline(pipeline_id)
144-
pipeline = GitlabClient.client.get_json(
145-
"/api/v4/projects/#{Project::Reticulum.api_gitlab_path}/pipelines/#{pipeline_id}"
146-
)
144+
pipeline = GitlabClient.client.get_pipeline(Project::Reticulum.api_gitlab_path, pipeline_id)
147145
return nil if pipeline.response.status == 404
148146

149147
[

0 commit comments

Comments
 (0)