Skip to content

Commit

Permalink
fix post slack message (#34569)
Browse files Browse the repository at this point in the history
fix post slack message #34569
  • Loading branch information
kobymeir authored Dec 11, 2024
1 parent 7185f0c commit af5d1c5
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 69 deletions.
28 changes: 18 additions & 10 deletions docker/build_docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -e

REVISION=${CI_PIPELINE_ID:-$(date +%s)}
PUSHED_DOCKERS=""
IMAGE_ARTIFACTS=""
CURRENT_DIR=$(pwd)
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
DOCKER_SRC_DIR=${SCRIPT_DIR}
Expand Down Expand Up @@ -355,14 +356,13 @@ function docker_build {
exit 1
fi
if [ -n "$CI" ]; then
echo "Creating artifact of docker image..."
ARTDIR="${DOCKER_SRC_DIR}/../artifacts"
mkdir -p "${ARTDIR}"
IMAGENAMESAVE=`echo ${image_full_name} | tr / _`.tar
IMAGESAVE=${ARTDIR}/$IMAGENAMESAVE
docker save -o "$IMAGESAVE" ${image_full_name}
gzip "$IMAGESAVE"
${DOCKER_SRC_DIR}/post_github_comment.py ${image_full_name} "--is_contribution"
IMAGE_NAME_SAVE="$(echo ${image_full_name} | sed -e 's/\//__/g').tar"
IMAGE_SAVE="${ARTIFACTS_FOLDER}/${IMAGE_NAME_SAVE}"
echo "Creating artifact of docker image at ${IMAGE_SAVE}"
docker save -o "${IMAGE_SAVE}" "${image_full_name}"
IMAGE_ARTIFACTS="${IMAGE_SAVE},${IMAGE_ARTIFACTS}"
gzip "${IMAGE_SAVE}"
"${DOCKER_SRC_DIR}/post_github_comment.py" "${image_full_name}" "--is_contribution"
cat << EOF
-------------------------
Docker image [$image_full_name] has been saved as an artifact.
Expand Down Expand Up @@ -423,12 +423,12 @@ fi

echo "DOCKER_ORG: ${DOCKER_ORG}, DIFF_COMPARE: [${DIFF_COMPARE}], SCRIPT_DIR: [${SCRIPT_DIR}], BRANCH: ${CI_COMMIT_REF_NAME}, PWD: [${CURRENT_DIR}]"

# echo to bash env to be used in future steps
ARTIFACTS_FOLDER="${ARTIFACTS_FOLDER:-artifacts}"
if [[ ! -d "${ARTIFACTS_FOLDER}" ]]; then
mkdir -p "${ARTIFACTS_FOLDER}"
fi

# echo to bash env to be used in future steps
echo $DIFF_COMPARE > $ARTIFACTS_FOLDER/diff_compare.txt
echo $SCRIPT_DIR > $ARTIFACTS_FOLDER/script_dir.txt
echo $CURRENT_DIR > $ARTIFACTS_FOLDER/current_dir.txt
Expand Down Expand Up @@ -458,9 +458,17 @@ if [ ${#errors[@]} != 0 ]; then
exit 1
fi


if [ -n "$PUSHED_DOCKERS" ]; then
echo "${PUSHED_DOCKERS}" > "${ARTIFACTS_FOLDER}/pushed_dockers.txt"
echo "Successfully pushed ${PUSHED_DOCKERS}"
echo "Successfully pushed:${PUSHED_DOCKERS}"
else
echo "No dockers were built and pushed"
fi

if [ -n "${IMAGE_ARTIFACTS}" ]; then
echo "${IMAGE_ARTIFACTS}" > "${ARTIFACTS_FOLDER}/image_artifacts.txt"
echo "Successfully saved:${IMAGE_ARTIFACTS}"
else
echo "No image artifacts were saved"
fi
109 changes: 50 additions & 59 deletions docker/post_github_comment.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#!/usr/bin/env python

import argparse
from pathlib import Path

import requests
import subprocess
import os
import re
import time

CIRCLECI_DEFAULT_WORKSPACE_DIR = "/home/circleci/project"
ARTIFACTS_FOLDER = Path(os.getenv("ARTIFACTS_FOLDER", "."))


def get_docker_image_size(docker_image, is_contribution: bool = False) -> str:
"""Get the size of the image from docker hub or CircleCI worker depending
if we're contributing or not.
"""
Get the size of the image from docker hub or locally, depending on whether we're contributing or not.
Arguments:
docker_image {string} -- the full name of hthe image
is_contribution {bool} -- flag whether we should get the image size from Dockerhub or CircleCI artifacts
docker_image {string} -- the full name of the image
is_contribution {bool} -- flag whether we should get the image size from Dockerhub, or locally
Returns:
- `str` containing the Docker image in MB, eg. '12.34 MB'.
- `str` containing the Docker image in MB, e.g. '12.34 MB'.
"""
size = "N/A"
if not is_contribution:
Expand All @@ -31,13 +31,14 @@ def get_docker_image_size(docker_image, is_contribution: bool = False) -> str:
res.raise_for_status()
size_bytes = res.json()['images'][0]['size']
size = '{0:.2f} MB'.format(float(size_bytes)/1024/1024)
break
except Exception as ex:
print("[{}] failed getting image size for image: {}. Err: {}".format(i, docker_image, ex))
print("Attempt [{}] failed getting image size for image: {}. Err: {}".format(i, docker_image, ex))
if i != 3:
print("Sleeping 5 seconds and trying again...")
time.sleep(5)
else:
docker_image_tar = convert_docker_image_tar(docker_image)
docker_image_tar = ARTIFACTS_FOLDER / convert_docker_image_tar(docker_image)
if docker_image_tar.exists():
size_bytes = docker_image_tar.stat().st_size
size = '{0:.2f} MB'.format(float(size_bytes)/1024/1024)
Expand All @@ -46,62 +47,53 @@ def get_docker_image_size(docker_image, is_contribution: bool = False) -> str:
return size


def convert_docker_image_tar(docker_image: str) -> Path:
def convert_docker_image_tar(docker_image: str) -> str:
"""
Helper function to convert the Docker image to valid path on CircleCI worker. For example:
`devdemisto/bottle2:1.0.0.89478.tar.gz` -> `devdemisto_bottle2:1.0.0.89478.tar.gz`.
Helper function to convert the Docker image to a valid path.
For example, `devdemisto/bottle2:1.0.0.89478.tar.gz` -> `devdemisto_bottle2:1.0.0.89478.tar.gz`.
Arguments:
- `docker_image` (``str``): The docker image filename.
Returns:
- `Path` of the fixed Docker image TAR.
- `str` of the fixed Docker image TAR.
"""
return f"{docker_image.replace('/', '__')}.tar.gz"

circleci_root_path = Path(os.environ.get("CIRCLE_WORKING_DIRECTORY", CIRCLECI_DEFAULT_WORKSPACE_DIR))
artifacts_path = circleci_root_path / "artifacts"

return artifacts_path / f"{docker_image.replace('/', '_')}.tar.gz"
def get_pr_comments_url() -> str | None:
if os.getenv('PR_NUM'):
pr_num = os.getenv('PR_NUM')
print("PR number found from environment: " + pr_num)
return f'https://api.github.com/repos/demisto/dockerfiles/issues/{pr_num}/comments'

# try to get from comment
last_comment = subprocess.check_output(["git", "log", "-1", "--pretty=%B"], text=True)
m = re.search(r"#(\d+)", last_comment, re.MULTILINE)
if not m:
print("No issue id found in the last commit comment. Ignoring: \n------\n{}\n-------".format(last_comment))
return None
issue_id = m.group(1)
print("Issue id found from the last commit comment: " + issue_id)
post_url = "https://api.github.com/repos/demisto/dockerfiles/issues/{}/comments".format(issue_id)
return post_url


def main():
desc = """Post a message to github about the created image. Relies on environment variables:
GITHUB_KEY: api key of user to use for posting
CIRCLE_PULL_REQUEST: pull request url to use to get the pull id. Such as: https://github.com/demisto/dockerfiles/pull/9
if CIRCLE_PULL_REQUEST will try to get issue id from last commit comment
XSOAR_BOT_GITHUB_TOKEN: api key of user to use for posting
PR_NUM: the PR number to post to. If not set, it will try to infer it from the last commit message.
"""
parser = argparse.ArgumentParser(description=desc,
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("docker_image", help="The docker image with tag version to use. For example: devdemisto/python3:1.5.0.27")
parser.add_argument("--is_contribution", help="Whether the PR is a contribution or not", action="store_true", default=False)
parser.add_argument(
"-j",
"--job-id",
help="The CircleCI workflow job ID. Default is the environmental variable CIRCLE_WORKFLOW_JOB_ID",
default=os.environ.get('CIRCLE_WORKFLOW_JOB_ID', ""),
required=False,
dest="job_id"
)

args = parser.parse_args()
if not os.environ.get('GITHUB_KEY'):
print("No github key set. Will not post a message!")
print("Posting to github about image: " + args.docker_image)
post_url = get_pr_comments_url()
if not post_url:
return
post_url = ""
if os.environ.get('CIRCLE_PULL_REQUEST'):
# change: https://github.com/demisto/dockerfiles/pull/9
# to: https://api.github.com/repos/demisto/dockerfiles/issues/9/comments
post_url = os.environ['CIRCLE_PULL_REQUEST'].replace('github.com', 'api.github.com/repos').replace('pull', 'issues') + "/comments"
else:
# try to get from comment
last_comment = subprocess.check_output(["git", "log", "-1", "--pretty=%B"]).decode()
m = re.search(r"#(\d+)", last_comment, re.MULTILINE)
if not m:
print("No issue id found in last commit comment. Ignoring: \n------\n{}\n-------".format(last_comment))
return
issue_id = m.group(1)
print("Issue id found from last commit comment: " + issue_id)
post_url = "https://api.github.com/repos/demisto/dockerfiles/issues/{}/comments".format(issue_id)
print('Found PR Comments URL: {post_url}')
inspect_format = f'''
{{{{ range $env := .Config.Env }}}}{{{{ if eq $env "DEPRECATED_IMAGE=true" }}}}## 🔴 IMPORTANT: This image is deprecated 🔴{{{{ end }}}}{{{{ end }}}}
## Docker Metadata
Expand All @@ -120,31 +112,30 @@ def main():
if base_name.startswith('demisto/'):
mode = "Production"
title = f"# Docker Image Ready - {mode}\n\n"
if not args.is_contribution:
if args.is_contribution:
saved_docker_image = convert_docker_image_tar(args.docker_image)
message = (
title +
"Docker automatic build at CircleCI has deployed your docker image: {}\n".format(args.docker_image) +
"It is available now on docker hub at: https://hub.docker.com/r/{}/tags\n".format(base_name) +
"Get started by pulling the image:\n" +
"```\n" +
"docker pull {}\n".format(args.docker_image) +
"Docker automatic build has completed. The Docker image is available from the assigned reviewer to be loaded locally.\n\n" +
"To load it locally run the following command:\n" +
"```bash\n" +
f"gunzip {saved_docker_image} | docker load\n" +
"```\n" +
docker_info
)
elif args.job_id:
saved_docker_image = convert_docker_image_tar(args.docker_image).name
circleci_docker_image_url = f"https://output.circle-artifacts.com/output/job/{args.job_id}/artifacts/{os.environ.get('CIRCLE_NODE_INDEX', '0')}/docker_images/{saved_docker_image}"
else:
message = (
title +
"Docker automatic build at CircleCI has completed. The Docker image is available as an artifact of the build.\n\n" +
"To download it and load it locally run the following command:\n" +
"```bash\n" +
f"curl -L '{circleci_docker_image_url}' | gunzip | docker load\n" +
"Docker automatic build has deployed your docker image: {}\n".format(args.docker_image) +
"It is available now on docker hub at: https://hub.docker.com/r/{}/tags\n".format(base_name) +
"Get started by pulling the image:\n" +
"```\n" +
"docker pull {}\n".format(args.docker_image) +
"```\n" +
docker_info
)
print("Going to post comment:\n\n{}".format(message))
res = requests.post(post_url, json={"body": message}, auth=(os.environ['GITHUB_KEY'], 'x-oauth-basic'))
res = requests.post(post_url, json={"body": message}, auth=(os.environ['XSOAR_BOT_GITHUB_TOKEN'], 'x-oauth-basic'))
try:
res.raise_for_status()
except Exception as ex:
Expand Down

0 comments on commit af5d1c5

Please sign in to comment.