-
Notifications
You must be signed in to change notification settings - Fork 0
feat: extract package upload helper #158
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
Open
staticaland
wants to merge
19
commits into
main
Choose a base branch
from
refactor-package-upload-helper
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
82f5d44
feat: extract package upload helper
staticaland 685e6e4
refactor: clarify helper flow
staticaland e93ae8c
refactor: adopt bash bible style
staticaland 2c6c126
refactor: inline packaging flow
staticaland fbcdb9a
refactor: simplify environment loop
staticaland 0b427e7
chore: rename summary helper
staticaland 1b88068
docs: explain artifact packaging flow
staticaland 33d1efe
docs: tighten packaging description
staticaland 023614d
docs: describe helper functions
staticaland 19eb1d2
chore: ok
staticaland 779bfb2
chore: no need for sed
staticaland 7eaac2d
chore: shfmt
staticaland f78fc3b
feat: separate scripts - maybe revert here
staticaland 7ad26d3
fix: use action path
staticaland aef20fd
feat: oidc with curl
staticaland bf5a667
feat: no need to know about docker here
staticaland 8fb31fe
fix: various bugs and improvements
staticaland 5342b53
experiment with split
yngvark 178b5db
Revert "experiment with split"
yngvark 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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
| #!/usr/bin/env bash | ||
| # Common helpers shared between artifact scripts. | ||
|
|
||
| log_info() { | ||
| printf '[INFO] %s\n' "$*" | ||
| } | ||
|
|
||
| log_error() { | ||
| printf '[ERROR] %s\n' "$*" >&2 | ||
| } | ||
|
|
||
| die() { | ||
| log_error "$1" | ||
| exit 1 | ||
| } | ||
|
|
||
| configure_aws() { | ||
| : "${AWSCREDS:?Missing AWSCREDS}" | ||
| aws_config_file="$(mktemp)" | ||
| printf '%s\n' "$AWSCREDS" >"$aws_config_file" | ||
| export AWS_CONFIG_FILE="$aws_config_file" | ||
| } | ||
|
|
||
| environment_defined() { | ||
| : "${CONFIG:?Missing CONFIG}" | ||
| local environment="$1" | ||
| printf '%s' "$CONFIG" | jq -e ".${environment} != null" >/dev/null 2>&1 | ||
| } | ||
|
|
||
| environment_value() { | ||
| : "${CONFIG:?Missing CONFIG}" | ||
| local environment="$1" key="$2" | ||
| printf '%s' "$CONFIG" | jq -e -r ".${environment}.${key}" | ||
| } | ||
|
|
||
| write_github_summary() { | ||
| local final_tag="$1" | ||
| : "${PARTIAL_WORKFLOW_DISPATCH_URL:?Missing PARTIAL_WORKFLOW_DISPATCH_URL}" | ||
| : "${GITHUB_WORKFLOW_REF:?Missing GITHUB_WORKFLOW_REF}" | ||
| : "${GITHUB_OUTPUT:?Missing GITHUB_OUTPUT}" | ||
| : "${GITHUB_STEP_SUMMARY:?Missing GITHUB_STEP_SUMMARY}" | ||
| : "${GITHUB_ENV:?Missing GITHUB_ENV}" | ||
|
|
||
| local workflow_filename workflow_dispatch_url | ||
| workflow_filename="$(basename "${GITHUB_WORKFLOW_REF%%@*}")" | ||
| workflow_dispatch_url="$PARTIAL_WORKFLOW_DISPATCH_URL/$workflow_filename" | ||
|
|
||
| printf 'tag=%s\n' "$final_tag" >>"$GITHUB_OUTPUT" | ||
| printf 'ARTIFACT_TAG=%s\n' "$final_tag" >>"$GITHUB_ENV" | ||
| cat <<EOF >>"$GITHUB_STEP_SUMMARY" | ||
| Built and uploaded artifact with tag: | ||
| \`\`\` | ||
| $final_tag | ||
| \`\`\` | ||
|
|
||
| --- | ||
|
|
||
| _To manually deploy the artifact, copy the tag and pass it in through a [workflow dispatch]($workflow_dispatch_url)_ | ||
| EOF | ||
| } |
This file contains hidden or 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,60 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| IFS=$'\n\t' | ||
|
|
||
| : "${CONFIG:?Missing CONFIG}" | ||
| : "${SOURCE_TYPE:?Missing SOURCE_TYPE}" | ||
| : "${SOURCE_LOCATION:?Missing SOURCE_LOCATION}" | ||
| : "${TAG:?Missing TAG}" | ||
|
|
||
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| repo_root="$(cd "$script_dir/.." && pwd)" | ||
| source "$repo_root/lib/common.sh" | ||
|
|
||
| source_type="$SOURCE_TYPE" | ||
| source_location="$SOURCE_LOCATION" | ||
| tag="$TAG" | ||
|
|
||
| cleanup() { | ||
| if [[ -n "${aws_config_file:-}" ]]; then | ||
| rm -f "$aws_config_file" | ||
| fi | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| configure_aws | ||
|
|
||
| if [[ "$source_type" != "docker-image" ]]; then | ||
| die "push-ecr-image.sh only supports docker-image sources (received $source_type)" | ||
| fi | ||
|
|
||
| upload_image_artifact() { | ||
| local environment="$1" | ||
| local account_id ecr_repository_name default_region | ||
| local login_password ecr_repository_uri image_tag | ||
|
|
||
| account_id="$(environment_value "$environment" accountId)" | ||
| ecr_repository_name="$(environment_value "$environment" artifactEcrRepositoryName)" | ||
| default_region="$(environment_value "$environment" defaultRegion)" | ||
|
|
||
| export AWS_PROFILE="$environment" | ||
| login_password="$(aws ecr get-login-password --region "$default_region")" | ||
| printf '::add-mask::%s\n' "$login_password" | ||
|
|
||
| ecr_repository_uri="$account_id.dkr.ecr.$default_region.amazonaws.com" | ||
| image_tag="$ecr_repository_uri/$ecr_repository_name:$tag" | ||
|
|
||
| printf '%s\n' "$login_password" | docker login --username AWS --password-stdin "$ecr_repository_uri" | ||
| log_info "Tagging image with image tag: $image_tag" | ||
| docker tag "$source_location" "$image_tag" | ||
| log_info "Pushing image with tag: $image_tag" | ||
| docker push "$image_tag" | ||
| } | ||
|
|
||
| for environment in dev prod; do | ||
| if environment_defined "$environment"; then | ||
| upload_image_artifact "$environment" | ||
| fi | ||
| done | ||
|
|
||
| write_github_summary "$tag" |
This file contains hidden or 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,83 @@ | ||
| #!/usr/bin/env bash | ||
| set -euo pipefail | ||
| IFS=$'\n\t' | ||
|
|
||
| : "${CONFIG:?Missing CONFIG}" | ||
| : "${SOURCE_TYPE:?Missing SOURCE_TYPE}" | ||
| : "${SOURCE_LOCATION:?Missing SOURCE_LOCATION}" | ||
| : "${TAG:?Missing TAG}" | ||
|
|
||
| script_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| repo_root="$(cd "$script_dir/.." && pwd)" | ||
| source "$repo_root/lib/common.sh" | ||
|
|
||
| source_type="$SOURCE_TYPE" | ||
| source_location="$SOURCE_LOCATION" | ||
| tag="$TAG" | ||
| package_tmp_dir="" | ||
|
|
||
| cleanup() { | ||
| if [[ -n "${aws_config_file:-}" ]]; then | ||
| rm -f "$aws_config_file" | ||
| fi | ||
| if [[ -n "$package_tmp_dir" && -d "$package_tmp_dir" ]]; then | ||
| rm -rf "$package_tmp_dir" | ||
| fi | ||
| } | ||
| trap cleanup EXIT | ||
|
|
||
| configure_aws | ||
|
|
||
| archive_folder_source() { | ||
| package_tmp_dir="$(mktemp -d)" | ||
| (cd "$source_location" && zip -r "$package_tmp_dir/archive.zip" .) | ||
| source_location="$package_tmp_dir/archive.zip" | ||
| source_type="file" | ||
| } | ||
|
|
||
| append_file_extension_suffix() { | ||
| local extension="" | ||
| if [[ "$source_location" == *.* ]]; then | ||
| extension="${source_location##*.}" | ||
| fi | ||
| if [[ -n "$extension" ]]; then | ||
| tag="$tag.$extension" | ||
| fi | ||
| } | ||
|
|
||
| upload_file_artifact() { | ||
| local environment="$1" | ||
| local bucket_name | ||
|
|
||
| bucket_name="$(environment_value "$environment" artifactBucketName)" | ||
|
|
||
| log_info "Uploading $source_location to S3 as $tag in $environment" | ||
|
|
||
| export AWS_PROFILE="$environment" | ||
| aws s3 cp "$source_location" "s3://$bucket_name/$tag" | ||
| } | ||
|
|
||
| case "$source_type" in | ||
| folder) | ||
| log_info "Compressing folder artifact into archive for upload" | ||
| archive_folder_source | ||
| ;; | ||
| file) ;; | ||
| docker-image) | ||
| die "upload-s3-artifact.sh only supports file or folder sources" | ||
| ;; | ||
| *) | ||
| die "Unsupported source type: $source_type" | ||
| ;; | ||
| esac | ||
|
|
||
| log_info "Preparing file artifact for upload to S3" | ||
| append_file_extension_suffix | ||
|
|
||
| for environment in dev prod; do | ||
| if environment_defined "$environment"; then | ||
| upload_file_artifact "$environment" | ||
| fi | ||
| done | ||
|
|
||
| write_github_summary "$tag" | ||
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.
Uh oh!
There was an error while loading. Please reload this page.