-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes current branch check problem when tag is pushed to push docker tags #184
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6d3a443
Adds tag name check when tag is pushed
gurkanindibay e45bd7e
Adds tag name check when tag is pushed
gurkanindibay d00d838
Fixes missing argument for manual tagging
gurkanindibay 0565594
Details some messages
gurkanindibay 5032a00
Moves publish flag check into tag block
gurkanindibay bbc9b1c
Fixes DOCKER_IMAGE_NAME
gurkanindibay c938e46
Fixes static code analysis checks
gurkanindibay a00ec59
Changes fetch depth 0 to get all branches
gurkanindibay a3176ef
Adds debug messages to detect unit test errors
gurkanindibay a1c290b
Adds debug messages to detect unit test errors
gurkanindibay 1215a2c
Fixes review notes
gurkanindibay c0675af
Changes publish parameter
gurkanindibay 08d184e
Fixes missing import
gurkanindibay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
import base64 | ||
import os | ||
import re | ||
import shlex | ||
import subprocess | ||
from datetime import datetime | ||
from enum import Enum | ||
from typing import Dict, List | ||
from typing import Tuple | ||
from typing import Dict, List, Tuple | ||
|
||
import base64 | ||
import git | ||
import gnupg | ||
import pathlib2 | ||
import requests | ||
from git import Repo | ||
from github import Repository, PullRequest, Commit, Github | ||
from git import GitCommandError, Repo | ||
from github import Commit, Github, PullRequest, Repository | ||
from jinja2 import Environment, FileSystemLoader | ||
from parameters_validation import validate_parameters | ||
|
||
|
@@ -300,8 +300,25 @@ def prepend_line_in_file(file: str, match_regex: str, append_str: str) -> bool: | |
return has_match | ||
|
||
|
||
def is_tag_on_branch(tag_name: str, branch_name: str): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the new method to check whether branch contains tag |
||
g = git.Git(os.getcwd()) | ||
try: | ||
branches_str = g.execute(["git", "branch", "--contains", f"tags/{tag_name}"]) | ||
branches = remove_prefix(branches_str, "*").split("\n") | ||
print("Branches str:" + branches_str) | ||
if len(branches) > 0: | ||
for branch in branches: | ||
if branch.strip() == branch_name: | ||
return True | ||
return False | ||
except GitCommandError as e: | ||
print("Error:" + str(e)) | ||
return False | ||
|
||
|
||
def get_current_branch(working_dir: str) -> str: | ||
repo = get_new_repo(working_dir) | ||
|
||
return repo.active_branch.name | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
import pathlib2 | ||
from parameters_validation import validate_parameters | ||
|
||
from .common_tool_methods import remove_prefix, get_current_branch | ||
from .common_tool_methods import remove_prefix, get_current_branch, is_tag_on_branch | ||
from .common_validations import is_tag | ||
|
||
BASE_PATH = pathlib2.Path(__file__).parents[1] | ||
|
@@ -93,91 +93,94 @@ def get_image_tag(tag_prefix: str, docker_image_type: DockerImageType) -> str: | |
return f"{tag_prefix}{tag_suffix}" | ||
|
||
|
||
def publish_docker_image_on_push(docker_image_type: DockerImageType, github_ref: str, current_branch: str): | ||
def publish_docker_image_on_push(docker_image_type: DockerImageType, github_ref: str, will_image_be_published: bool): | ||
triggering_event_info, resource_name = decode_triggering_event_info(github_ref) | ||
for regular_image_type in regular_images_to_be_built(docker_image_type): | ||
if triggering_event_info == GithubTriggerEventSource.branch_push: | ||
publish_main_docker_images(regular_image_type, current_branch) | ||
publish_main_docker_images(regular_image_type, will_image_be_published) | ||
else: | ||
publish_tagged_docker_images(regular_image_type, resource_name, current_branch) | ||
publish_tagged_docker_images(regular_image_type, resource_name, will_image_be_published) | ||
|
||
|
||
def publish_docker_image_on_schedule(docker_image_type: DockerImageType, current_branch: str, ): | ||
def publish_docker_image_on_schedule(docker_image_type: DockerImageType, will_image_be_published: bool): | ||
if docker_image_type == DockerImageType.nightly: | ||
publish_nightly_docker_image(current_branch) | ||
publish_nightly_docker_image(will_image_be_published) | ||
else: | ||
for regular_image_type in regular_images_to_be_built(docker_image_type): | ||
publish_main_docker_images(regular_image_type, current_branch) | ||
publish_main_docker_images(regular_image_type, will_image_be_published) | ||
|
||
|
||
def publish_docker_image_manually(manual_trigger_type_param: ManualTriggerType, current_branch: str, | ||
def publish_docker_image_manually(manual_trigger_type_param: ManualTriggerType, will_image_be_published: bool, | ||
docker_image_type: DockerImageType, tag_name: str = "") -> None: | ||
if manual_trigger_type_param == ManualTriggerType.main and not tag_name: | ||
for it in regular_images_to_be_built(docker_image_type): | ||
publish_main_docker_images(it, current_branch) | ||
publish_main_docker_images(it, will_image_be_published) | ||
elif manual_trigger_type_param == ManualTriggerType.tags and tag_name: | ||
for it in regular_images_to_be_built(docker_image_type): | ||
publish_tagged_docker_images(it, tag_name, current_branch) | ||
publish_tagged_docker_images(it, tag_name, will_image_be_published) | ||
elif manual_trigger_type_param == ManualTriggerType.nightly: | ||
publish_nightly_docker_image(current_branch) | ||
publish_nightly_docker_image(will_image_be_published) | ||
|
||
|
||
def publish_main_docker_images(docker_image_type: DockerImageType, current_branch: str): | ||
def publish_main_docker_images(docker_image_type: DockerImageType, will_image_be_published: bool): | ||
print(f"Building main docker image for {docker_image_type.name}...") | ||
docker_image_name = f"{DOCKER_IMAGE_NAME}:{docker_image_type.name}" | ||
docker_client.images.build(dockerfile=docker_image_info_dict[docker_image_type]['file-name'], | ||
tag=docker_image_name, | ||
path=".") | ||
print(f"Main docker image for {docker_image_type.name} built.") | ||
if current_branch == DEFAULT_BRANCH_NAME: | ||
if will_image_be_published: | ||
print(f"Publishing main docker image for {docker_image_type.name}...") | ||
docker_client.images.push(DOCKER_IMAGE_NAME, tag=docker_image_type.name) | ||
print(f"Publishing main docker image for {docker_image_type.name} finished") | ||
else: | ||
print( | ||
f"Since current branch {current_branch} is not equal to " | ||
f"{DEFAULT_BRANCH_NAME} {docker_image_name} will not be pushed.") | ||
current_branch = get_current_branch(os.getcwd()) | ||
if current_branch != DEFAULT_BRANCH_NAME: | ||
print( | ||
f"Since current branch {current_branch} is not equal to " | ||
f"{DEFAULT_BRANCH_NAME} {docker_image_name} will not be pushed.") | ||
|
||
|
||
def publish_tagged_docker_images(docker_image_type, tag_name: str, current_branch: str): | ||
def publish_tagged_docker_images(docker_image_type, tag_name: str, will_image_be_published: bool): | ||
print(f"Building and publishing tagged image {docker_image_type.name} for tag {tag_name}...") | ||
tag_parts = decode_tag_parts(tag_name) | ||
tag_version_part = "" | ||
docker_image_name = f"{DOCKER_IMAGE_NAME}:{docker_image_type.name}" | ||
docker_client.images.build(dockerfile=docker_image_info_dict[docker_image_type]['file-name'], | ||
tag=docker_image_name, | ||
path=".") | ||
if current_branch != DEFAULT_BRANCH_NAME: | ||
print(f"Since current branch {current_branch} is not equal to {DEFAULT_BRANCH_NAME} tags will not be pushed.") | ||
print(f"{docker_image_type.name} image built.Now starting tagging and pushing...") | ||
for tag_part in tag_parts: | ||
tag_version_part = tag_version_part + tag_part | ||
image_tag = get_image_tag(tag_version_part, docker_image_type) | ||
print(f"Tagging {docker_image_name} with the tag {image_tag}...") | ||
docker_api_client.tag(docker_image_name, docker_image_name, image_tag) | ||
print(f"Tagging {docker_image_name} with the tag {image_tag} finished.") | ||
|
||
if current_branch == DEFAULT_BRANCH_NAME: | ||
if will_image_be_published: | ||
print(f"Pushing {docker_image_name} with the tag {image_tag}...") | ||
docker_client.images.push(DOCKER_IMAGE_NAME, tag=image_tag) | ||
print(f"Pushing {docker_image_name} with the tag {image_tag} finished") | ||
else: | ||
print(f"Skipped pushing {docker_image_type} with the tag {image_tag} since will_image_be_published flag is false") | ||
|
||
tag_version_part = tag_version_part + "." | ||
print(f"Building and publishing tagged image {docker_image_type.name} for tag {tag_name} finished.") | ||
|
||
|
||
def publish_nightly_docker_image(current_branch: str): | ||
def publish_nightly_docker_image(will_image_be_published: bool): | ||
print("Building nightly image...") | ||
docker_image_name = f"{DOCKER_IMAGE_NAME}:{docker_image_info_dict[DockerImageType.nightly]['docker-tag']}" | ||
docker_client.images.build(dockerfile=docker_image_info_dict[DockerImageType.nightly]['file-name'], | ||
tag=docker_image_name, | ||
path=".") | ||
print("Nightly image build finished.") | ||
|
||
if current_branch == DEFAULT_BRANCH_NAME: | ||
if will_image_be_published: | ||
print("Pushing nightly image...") | ||
docker_client.images.push(DOCKER_IMAGE_NAME, tag=docker_image_info_dict[DockerImageType.nightly]['docker-tag']) | ||
print("Nightly image push finished.") | ||
else: | ||
print("Nightly image will not be pushed since will_image_be_published flag is false") | ||
|
||
|
||
def validate_and_extract_general_parameters(docker_image_type_param: str, pipeline_trigger_type_param: str) -> Tuple[ | ||
|
@@ -216,23 +219,42 @@ def validate_and_extract_manual_exec_params(manual_trigger_type_param: str, tag_ | |
return manual_trigger_type_param | ||
|
||
|
||
def get_image_publish_status(github_ref: str, is_test: bool): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Centralized the image publish status to manage the publish status in one place |
||
if is_test: | ||
return False | ||
triggering_event_info, resource_name = decode_triggering_event_info(github_ref) | ||
if triggering_event_info == GithubTriggerEventSource.tag_push: | ||
if not is_tag_on_branch(tag_name=resource_name, branch_name=DEFAULT_BRANCH_NAME): | ||
return False | ||
return True | ||
current_branch = get_current_branch(os.getcwd()) | ||
if current_branch != DEFAULT_BRANCH_NAME: | ||
return False | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--github_ref') | ||
parser.add_argument('--github_ref', required=True) | ||
parser.add_argument('--pipeline_trigger_type', choices=[e.name for e in GithubPipelineTriggerType], required=True) | ||
parser.add_argument('--tag_name', nargs='?', default="") | ||
parser.add_argument('--manual_trigger_type', choices=[e.name for e in ManualTriggerType]) | ||
parser.add_argument('--image_type', choices=[e.name for e in DockerImageType]) | ||
parser.add_argument('--is_test', action="store_true") | ||
args = parser.parse_args() | ||
|
||
pipeline_trigger_type, image_type = validate_and_extract_general_parameters(args.image_type, | ||
args.pipeline_trigger_type) | ||
current_branch = get_current_branch(os.getcwd()) | ||
if args.is_test: | ||
print("Script is working in test mode. Images will not be published") | ||
|
||
publish_status = get_image_publish_status(args.github_ref, args.is_test) | ||
if pipeline_trigger_type == GithubPipelineTriggerType.workflow_dispatch: | ||
manual_trigger_type = validate_and_extract_manual_exec_params(args.manual_trigger_type, args.tag_name) | ||
publish_docker_image_manually(manual_trigger_type_param=manual_trigger_type, current_branch=current_branch, | ||
publish_docker_image_manually(manual_trigger_type_param=manual_trigger_type, | ||
will_image_be_published=publish_status, | ||
docker_image_type=image_type, tag_name=args.tag_name) | ||
elif pipeline_trigger_type == GithubPipelineTriggerType.push: | ||
publish_docker_image_on_push(image_type, args.github_ref, current_branch) | ||
publish_docker_image_on_push(image_type, args.github_ref, publish_status) | ||
else: | ||
publish_docker_image_on_schedule(image_type, current_branch) | ||
publish_docker_image_on_schedule(image_type, publish_status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this? Do you really need all the git history?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/citusdata/tools/runs/3653741920?check_suite_focus=true
I can not test the is_tag_on_branch since devevlop is not fetched