-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from AllenInstitute/feature/update-cicd-pipeli…
…ne-stack-base-class feature/update cicd pipeline stack base class
- Loading branch information
Showing
9 changed files
with
703 additions
and
37 deletions.
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
Large diffs are not rendered by default.
Oops, something went wrong.
119 changes: 119 additions & 0 deletions
119
src/aibs_informatics_cdk_lib/cicd/pipeline/scripts/cicd-release.sh
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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
#!/bin/bash | ||
|
||
################################################################# | ||
# CI/CD Release Script | ||
# Description: | ||
# Purpose of this script is to facilitate submit Pull Requests | ||
# from a source branch/commit to a destination branch. | ||
# | ||
# Input Environment Variables: | ||
# | ||
# CICD_RELEASE_SOURCE_ENV_TYPE: | ||
# Environment Type of source branch | ||
# CICD_RELEASE_TARGET_ENV_TYPE: | ||
# Environment Type of source branch | ||
# CICD_RELEASE_TARGET_BRANCH: | ||
# Target branch to submit pull request into | ||
# CICD_RELEASE_REVIEWER: | ||
# Reviewers for the PR | ||
|
||
################################### | ||
|
||
|
||
export CICD_RELEASE_SOURCE_COMMIT=$CODEBUILD_RESOLVED_SOURCE_VERSION | ||
export CICD_RELEASE_CANDIDATE_BRANCH="candidate/$CICD_RELEASE_TARGET_BRANCH" | ||
|
||
echo "==> CI/CD Release Inputs:" | ||
echo "==> CICD_RELEASE_SOURCE_ENV_TYPE = $CICD_RELEASE_SOURCE_ENV_TYPE" | ||
echo "==> CICD_RELEASE_TARGET_ENV_TYPE = $CICD_RELEASE_TARGET_ENV_TYPE" | ||
echo "==> CICD_RELEASE_SOURCE_COMMIT = $CICD_RELEASE_SOURCE_COMMIT" | ||
echo "==> CICD_RELEASE_CANDIDATE_BRANCH = $CICD_RELEASE_CANDIDATE_BRANCH" | ||
echo "==> CICD_RELEASE_TARGET_BRANCH = $CICD_RELEASE_TARGET_BRANCH" | ||
echo "==> CICD_RELEASE_REVIEWER = $CICD_RELEASE_REVIEWER" | ||
|
||
export CICD_RELEASE_GIT_MESSAGE="$(git log -1 --pretty=%B)" | ||
export CICD_RELEASE_GIT_AUTHOR="$(git log -1 --pretty=%an)" | ||
export CICD_RELEASE_GIT_AUTHOR_EMAIL="$(git log -1 --pretty=%ae)" | ||
export CICD_RELEASE_GIT_COMMIT="$(git log -1 --pretty=%H)" | ||
export CICD_RELEASE_GIT_SHORT_COMMIT="$(git log -1 --pretty=%h)" | ||
|
||
echo "==> CICD_RELEASE_GIT_MESSAGE = $CICD_RELEASE_GIT_MESSAGE" | ||
echo "==> CICD_RELEASE_GIT_AUTHOR = $CICD_RELEASE_GIT_AUTHOR" | ||
echo "==> CICD_RELEASE_GIT_AUTHOR_EMAIL = $CICD_RELEASE_GIT_AUTHOR_EMAIL" | ||
echo "==> CICD_RELEASE_GIT_COMMIT = $CICD_RELEASE_GIT_COMMIT" | ||
echo "==> CICD_RELEASE_GIT_SHORT_COMMIT = $CICD_RELEASE_GIT_SHORT_COMMIT" | ||
echo | ||
|
||
|
||
echo "Verify gh command is on PATH" | ||
|
||
if ! command -v gh &> /dev/null; then | ||
echo "==! Could not find gh command on PATH. EXITING" | ||
exit 1 | ||
fi | ||
|
||
echo | ||
echo "==> Promoting commits up to $CICD_RELEASE_GIT_SHORT_COMMIT to release candidate branch." | ||
echo "==> Release candidate branch: $CICD_RELEASE_CANDIDATE_BRANCH" | ||
|
||
echo "[command] git checkout -B $CICD_RELEASE_CANDIDATE_BRANCH $CICD_RELEASE_SOURCE_COMMIT" | ||
git checkout -B $CICD_RELEASE_CANDIDATE_BRANCH $CICD_RELEASE_SOURCE_COMMIT | ||
echo "[command] git push --set-upstream --force" | ||
git push --set-upstream --force origin $CICD_RELEASE_CANDIDATE_BRANCH | ||
|
||
CICD_RELEASE_DATE=$(date '+%Y-%m-%d') | ||
CICD_RELEASE_PR_TITLE="Release $CICD_RELEASE_SOURCE_ENV_TYPE -> $CICD_RELEASE_TARGET_ENV_TYPE ($CICD_RELEASE_DATE)" | ||
|
||
CICD_RELEASE_PR_MESSAGE_FILE=$(mktemp) | ||
|
||
|
||
cat <<EOF > $CICD_RELEASE_PR_MESSAGE_FILE | ||
# Release | ||
## Release Summary | ||
| Release Attribute | Value | | ||
| --- | --- | | ||
| Target Branch | $CICD_RELEASE_TARGET_BRANCH | | ||
| Source Branch | $CICD_RELEASE_CANDIDATE_BRANCH ($CICD_RELEASE_GIT_SHORT_COMMIT) | | ||
| Date | $(date '+%Y-%m-%d %H:%M:%S') | | ||
## Release Notes | ||
This release includes changes up to $CICD_RELEASE_GIT_SHORT_COMMIT. This includes the following: | ||
- (fill me please) | ||
- (fill me please) | ||
- (fill me please) | ||
## Checklist | ||
- [ ] All of GCS works impeccably | ||
EOF | ||
|
||
|
||
echo "==> Checking for open Pull Requests..." | ||
EXISTING_PR_NUMBER=$(gh pr list -B $CICD_RELEASE_TARGET_BRANCH -L 1 | cut -f1) | ||
|
||
if [[ ! -z $EXISTING_PR_NUMBER ]]; then | ||
echo "==> Pull Request already exists ($EXISTING_PR_NUMBER). Updating..." | ||
|
||
# Update the PR message | ||
echo "" | cat >> $CICD_RELEASE_PR_MESSAGE_FILE | ||
echo "---" | cat >> $CICD_RELEASE_PR_MESSAGE_FILE | ||
echo "# Previous Revisions" | cat >> $CICD_RELEASE_PR_MESSAGE_FILE | ||
echo "---" | cat >> $CICD_RELEASE_PR_MESSAGE_FILE | ||
echo "" | cat >> $CICD_RELEASE_PR_MESSAGE_FILE | ||
gh pr view --json body | jq -r '.body' >> $CICD_RELEASE_PR_MESSAGE_FILE | ||
|
||
gh pr edit $EXISTING_PR_NUMBER \ | ||
--title "$CICD_RELEASE_PR_TITLE" \ | ||
--body-file $CICD_RELEASE_PR_MESSAGE_FILE | ||
|
||
else | ||
|
||
echo "==> Creating new Pull Request" | ||
|
||
gh pr create \ | ||
--base $CICD_RELEASE_TARGET_BRANCH \ | ||
--title "$CICD_RELEASE_PR_TITLE" \ | ||
--body-file "$CICD_RELEASE_PR_MESSAGE_FILE" \ | ||
--reviewer "$CICD_RELEASE_REVIEWER" | ||
fi |
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,6 +1,79 @@ | ||
from enum import Enum | ||
from typing import Optional, Type, TypeVar, Union | ||
|
||
import constructs | ||
from aibs_informatics_core.utils.os_operations import get_env_var | ||
|
||
class CDKStackTarget(str, Enum): | ||
PIPELINE = "pipeline" | ||
INFRA = "infra" | ||
from aibs_informatics_cdk_lib.project.utils import _get_from_context | ||
|
||
CDK_STACK_TARGET_ENV_VAR = "CDK_STACK_TARGET" | ||
|
||
T = TypeVar("T", bound="CDKStackTargetBaseEnum") | ||
|
||
|
||
class CDKStackTargetBaseEnum(Enum): | ||
"""Base class for CDK stack target types | ||
Usage: | ||
class MyCDKStackTarget(str, CDKStackTargetBaseEnum): | ||
INFRA = "pipeline" | ||
""" | ||
|
||
@classmethod | ||
def from_env(cls: Type[T], default: Union[str, T]) -> T: | ||
target = get_env_var(CDK_STACK_TARGET_ENV_VAR) | ||
target = target or default | ||
return cls(target) | ||
|
||
@classmethod | ||
def from_context( | ||
cls: Type[T], | ||
node: constructs.Node, | ||
default: Union[str, T], | ||
context_keys: Optional[list[str]] = None, | ||
) -> T: | ||
"""Resolves the CDK stack target type from context | ||
Args: | ||
cls (Type[T]): subclassed CDKStackTargetBase | ||
node (constructs.Node): cdk construct node | ||
default (str): default to use. | ||
context_keys (Optional[list[str]], optional): overrides for context names. | ||
Defaults to None. | ||
Returns: | ||
T: CDKStackTargetBase instance | ||
""" | ||
context_keys = context_keys or ["target", "stack_target"] | ||
|
||
target = _get_from_context(node, context_keys) or default | ||
|
||
return cls(target) | ||
|
||
@classmethod | ||
def from_context_or_env( | ||
cls: Type[T], | ||
node: constructs.Node, | ||
default: Union[str, T], | ||
context_keys: Optional[list[str]] = None, | ||
) -> T: | ||
"""Resolves the CDK stack target type from context or environment | ||
Order of resolution: | ||
1. CDK context value (specifying -c K=V) | ||
2. env variable | ||
3. default value ("dev") | ||
Args: | ||
cls (Type[T]): subclassed CDKStackTargetBase | ||
node (constructs.Node): cdk construct node | ||
default (str): default to use. | ||
context_keys (Optional[list[str]], optional): overrides for context names. | ||
Defaults to None. | ||
""" | ||
|
||
return cls.from_context( | ||
node=node, default=cls.from_env(default), context_keys=context_keys | ||
) |
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
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
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