Skip to content

Commit

Permalink
Merge pull request #497 from camptocamp/backport/489-to-1.1
Browse files Browse the repository at this point in the history
[Backport 1.1] Send dispatch event after the Docker publish
  • Loading branch information
sbrunner authored Mar 17, 2022
2 parents 59b3601 + b8253ea commit f301abc
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 32 deletions.
1 change: 1 addition & 0 deletions .prospector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pep8:
max-line-length: 110
disable:
- E203 # whitespace before ':'
- W293 # blank line contains whitespace
- E501 # line too long, Done by Black

pep257:
Expand Down
4 changes: 4 additions & 0 deletions c2cciutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ def get_config(branch: Optional[str] = None) -> c2cciutils.configuration.Configu
},
"dockerhub": {"versions": ["version_tag", "version_branch", "rebuild", "feature_branch"]},
},
"dispatch": {
"event-type": "image-update",
"repository": "camptocamp/argocd-gs-platform-ch-development-apps",
},
},
"helm": {
"versions": ["version_tag"],
Expand Down
19 changes: 19 additions & 0 deletions c2cciutils/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,21 @@
)


# dispatch config
#
# Send a dispatch event to an other repository
DispatchConfig = TypedDict(
"DispatchConfig",
{
# The repository name to be triggered
"repository": str,
# The event type to be triggered
"event-type": str,
},
total=False,
)


# Print versions
#
# The print versions configuration
Expand Down Expand Up @@ -523,6 +538,10 @@
"images": List["PublishDockerImage"],
# The repository where we should publish the images
"repository": Dict[str, "PublishDockerRepository"],
# Send a dispatch event to an other repository
#
# oneOf
"dispatch": Union["DispatchConfig", Literal[False]],
},
total=False,
)
Expand Down
41 changes: 14 additions & 27 deletions c2cciutils/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import subprocess # nosec
import sys
import uuid
from typing import Optional
from typing import List, Optional

import ruamel.yaml
from google.auth.transport.requests import Request
Expand Down Expand Up @@ -293,6 +293,7 @@ def docker(
tag_src: str,
tag_dst: str,
latest: bool,
images_full: List[str],
) -> bool:
"""
Publish to a Docker registry.
Expand All @@ -310,13 +311,15 @@ def docker(
tag_src: The source tag (usually latest)
tag_dst: The tag used for publication
latest: Publish also the tag latest
images_full: The list of published images (with tag), used to build the dispatch event
"""

print(f"::group::Publishing {image_config['name']}:{tag_dst} to {name}")
sys.stdout.flush()
sys.stderr.flush()

try:
new_images_full = []
if "server" in config:
subprocess.run(
[
Expand All @@ -327,6 +330,7 @@ def docker(
],
check=True,
)
new_images_full.append(f"{config['server']}/{image_config['name']}:{tag_dst}")
if latest:
subprocess.run(
[
Expand All @@ -337,19 +341,7 @@ def docker(
],
check=True,
)
subprocess.run(
[
"docker",
"push",
f"{config['server']}/{image_config['name']}:{tag_dst}",
],
check=True,
)
if latest:
subprocess.run(
["docker", "push", f"{config['server']}/{image_config['name']}:{tag_src}"],
check=True,
)
new_images_full.append(f"{config['server']}/{image_config['name']}:{tag_src}")
else:
if tag_src != tag_dst:
subprocess.run(
Expand All @@ -361,19 +353,14 @@ def docker(
],
check=True,
)
subprocess.run(
["docker", "push", f"{image_config['name']}:{tag_dst}"],
check=True,
)
if latest:
subprocess.run(
[
"docker",
"push",
f"{image_config['name']}:{tag_src}",
],
check=True,
)
new_images_full.append(f"{image_config['name']}:{tag_dst}")
if latest and tag_src != tag_dst:
new_images_full.append(f"{image_config['name']}:{tag_src}")

for image in new_images_full:
subprocess.run(["docker", "push", image], check=True)
images_full += new_images_full

print("::endgroup::")
except subprocess.CalledProcessError as exception:
print(f"Error: {exception}")
Expand Down
21 changes: 21 additions & 0 deletions c2cciutils/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,27 @@
}
}
}
},
"dispatch": {
"description": "Send a dispatch event to an other repository",
"oneOf": [
{
"type": "object",
"title": "dispatch config",
"description": "Send a dispatch event to an other repository",
"properties": {
"repository": {
"description": "The repository name to be triggered",
"type": "string"
},
"event-type": {
"description": "The event type to be triggered",
"type": "string"
}
}
},
{ "const": false }
]
}
}
},
Expand Down
10 changes: 8 additions & 2 deletions c2cciutils/scripts/publish.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
import subprocess # nosec
import sys
import tarfile
from typing import Match, Optional, cast
from typing import List, Match, Optional, cast

import requests

import c2cciutils.configuration
import c2cciutils.publish
import c2cciutils.security
from c2cciutils.publish import GoogleCalendar
from c2cciutils.scripts.trigger_image_update import dispatch


def match(tpe: str, base_re: str) -> Optional[Match[str]]:
Expand Down Expand Up @@ -187,6 +188,7 @@ def main() -> None:
version_index = security.headers.index("Version")
latest = security.data[-1][version_index] == version

images_full: List[str] = []
for image_conf in docker_config.get("images", []):
if image_conf.get("group", "") == args.group:
for tag_config in image_conf.get("tags", []):
Expand All @@ -206,7 +208,7 @@ def main() -> None:
)
else:
success &= c2cciutils.publish.docker(
conf, name, image_conf, tag_src, tag_dst, latest
conf, name, image_conf, tag_src, tag_dst, latest, images_full
)
if version_type in google_calendar_config.get("on", []):
if not google_calendar:
Expand All @@ -219,6 +221,10 @@ def main() -> None:

google_calendar.create_event(summary, description)

dispatch_config = docker_config.get("dispatch", False)
if dispatch_config:
dispatch(dispatch_config["repository"], dispatch_config["event-type"], images_full)

helm_config = cast(
c2cciutils.configuration.PublishHelmConfig,
config.get("publish", {}).get("helm", {}) if config.get("publish", {}).get("helm", False) else {},
Expand Down
15 changes: 12 additions & 3 deletions c2cciutils/scripts/trigger_image_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import os.path
import subprocess # nosec
import sys
from typing import List

import requests
import yaml
Expand Down Expand Up @@ -55,8 +56,16 @@ def main() -> None:
for image_config in ci_config.get("publish", {}).get("docker", {}).get("images", []):
images_full.append(image_config["name"])

dispatch(args.repository, args.event_type, [f"{image}:{version}" for image in images_full])


def dispatch(repository: str, event_type: str, images_full: List[str]) -> None:
"""
Trigger an image update on the argocd repository.
"""

response = requests.post(
f"https://api.github.com/repos/{args.repository}/dispatches",
f"https://api.github.com/repos/{repository}/dispatches",
headers={
"Content-Type": "application/json2",
"Accept": "application/vnd.github.v3+json",
Expand All @@ -68,8 +77,8 @@ def main() -> None:
.strip(),
},
json={
"event_type": args.event_type,
"client_payload": {"name": " ".join([f"{image}:{version}" for image in images_full])},
"event_type": event_type,
"client_payload": {"name": " ".join(images_full)},
},
)
response.raise_for_status()
Expand Down

0 comments on commit f301abc

Please sign in to comment.