Skip to content

Commit 8b5f57d

Browse files
Update: [AEA-5930] - Copy Documents from INT to other environments (#278)
## Summary Copies documents from INT to other environments ### Details - Downloads S3 documents from INT - Check if the files exist in the destination environment's S3 (dev, qa, prod, pr) - Upload S3 documents to destination environment's S3 --------- Co-authored-by: bencegadanyi1-nhs <bence.gadanyi1@nhs.net>
1 parent 413d899 commit 8b5f57d

4 files changed

Lines changed: 130 additions & 0 deletions

File tree

.gitallowed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ client = WebClient\(token=slack_event_data\["bot_token"\]\)
3434
context accountId=123456789012
3535
.*:sample_docs/.*
3636
token = get_bot_token\(\)
37+
"AWS_ACCOUNT_ID": "123456789012"
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
name: "Document Sync"
2+
description: "Sync Documents between INT and a target Environment"
3+
inputs:
4+
TARGET_ENVIRONMENT:
5+
required: true
6+
description: "The Environment to Copy Files into (e.g., DEV, PROD)"
7+
STACK:
8+
required: false
9+
description: "The stack being deployed (ie., 'epsam' or 'epsam-pr-123')"
10+
default: "epsam"
11+
INT_CLOUD_FORMATION_DEPLOY_ROLE:
12+
required: true
13+
description: "The role to assume for the source (INT) account"
14+
TARGET_CLOUD_FORMATION_DEPLOY_ROLE:
15+
required: true
16+
description: "The role to assume for the target account"
17+
18+
runs:
19+
using: "composite"
20+
steps:
21+
- name: Connect to Source Account (INT)
22+
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708
23+
with:
24+
aws-region: eu-west-2
25+
role-to-assume: ${{ inputs.INT_CLOUD_FORMATION_DEPLOY_ROLE }}
26+
role-session-name: epsam-document-sync-source
27+
28+
- name: Find Source Bucket by Partial Name
29+
id: find-source-bucket
30+
shell: bash
31+
working-directory: .github/scripts
32+
env:
33+
STACK: "epsam"
34+
run: ./find_s3_bucket.sh
35+
36+
- name: Download all Files from Source Bucket
37+
shell: bash
38+
run: |
39+
mkdir -p ./s3-content
40+
aws s3 sync s3://${{ steps.find-source-bucket.outputs.BUCKET_NAME }} ./s3-content
41+
42+
- name: Connect to Target Account
43+
uses: aws-actions/configure-aws-credentials@61815dcd50bd041e203e49132bacad1fd04d2708
44+
with:
45+
aws-region: eu-west-2
46+
role-to-assume: ${{ inputs.TARGET_CLOUD_FORMATION_DEPLOY_ROLE }}
47+
role-session-name: epsam-document-sync-target
48+
49+
- name: Find Destination Bucket by Partial Name
50+
id: find-destination-bucket
51+
shell: bash
52+
working-directory: .github/scripts
53+
env:
54+
STACK: ${{ inputs.STACK }}
55+
run: ./find_s3_bucket.sh
56+
57+
- name: Check Discrepancies
58+
id: compare
59+
shell: bash
60+
run: |
61+
printf "\n"
62+
echo "Comparing local files with s3://${{ steps.find-destination-bucket.outputs.BUCKET_NAME }}..."
63+
DIFFS=$(aws s3 sync ./s3-content s3://${{ steps.find-destination-bucket.outputs.BUCKET_NAME }} --dryrun)
64+
65+
if [ -z "$DIFFS" ]; then
66+
echo -e "\033[0;32m✔ NO DISCREPANCIES FOUND.\033[0m"
67+
else
68+
echo -e "\033[0;33m⚠ WARNING: DISCREPANCIES FOUND:"
69+
70+
echo "$DIFFS"
71+
echo "--------------------------------------------------\033[0m"
72+
73+
CLEAN_DIFFS="${DIFFS//$'\n'/'%0A'}"
74+
echo "::warning title=Discrepancy Found in ${{ inputs.TARGET_ENVIRONMENT }}::$CLEAN_DIFFS"
75+
fi
76+
printf "\n"
77+
78+
- name: Upload Files to Target S3
79+
shell: bash
80+
run: |
81+
echo "Updating s3://${{ steps.find-destination-bucket.outputs.BUCKET_NAME }}..."
82+
aws s3 sync ./s3-content s3://${{ steps.find-destination-bucket.outputs.BUCKET_NAME }} --delete

.github/scripts/find_s3_bucket.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env bash
2+
3+
echo "Searching for bucket in CloudFormation exports..."
4+
5+
# Ensure the STACK variable is present
6+
if [ -z "$STACK" ]; then
7+
echo "Error: The STACK environment variable is not set."
8+
exit 1
9+
fi
10+
11+
echo "Searching for bucket in CloudFormation exports for stack prefix: $STACK..."
12+
13+
# List buckets and filter using JMESPath
14+
# We use 'tail -n 1' or 'awk' to ensure we only get one result if multiple match
15+
BUCKET_NAME=$(aws cloudformation list-exports --query "Exports[?Name=='${STACK}:kbDocsBucket:Name'].Value" --output text)
16+
17+
if [ -z "$BUCKET_NAME" ] || [ "$BUCKET_NAME" == "None" ]; then
18+
echo "Error: No bucket found matching '$PARTIAL_NAME'"
19+
exit 1
20+
fi
21+
22+
echo "Success: Found bucket '$BUCKET_NAME'"
23+
24+
# This special syntax tells GitHub Actions to set an output variable
25+
echo "BUCKET_NAME=$BUCKET_NAME" >> "$GITHUB_OUTPUT"

.github/workflows/release_all_stacks.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ on:
6161
required: false
6262
DEV_CLOUD_FORMATION_EXECUTE_LAMBDA_ROLE:
6363
required: false
64+
INT_CLOUD_FORMATION_EXECUTE_LAMBDA_ROLE:
65+
required: false
66+
PROD_CLOUD_FORMATION_EXECUTE_LAMBDA_ROLE:
67+
required: false
6468
REGRESSION_TESTS_PEM:
6569
required: false
6670
SLACK_BOT_TOKEN:
@@ -186,6 +190,24 @@ jobs:
186190
cdk-utils-build-repo:latest
187191
shell: bash
188192

193+
- name: Normalize Environment Name
194+
if: ${{ inputs.TARGET_ENVIRONMENT != 'int' && (inputs.DEPLOY_CODE == true || inputs.IS_PULL_REQUEST == true) }}
195+
run: |
196+
# Convert TARGET_ENVIRONMENT to Uppercase (e.g., 'prod' -> 'PROD')
197+
VAL=$(echo "$TARGET_ENVIRONMENT" | tr '[:lower:]' '[:upper:]')
198+
echo "UPPER_TARGET_ENVIRONMENT=$VAL" >> "$GITHUB_OUTPUT"
199+
env:
200+
TARGET_ENVIRONMENT: ${{ inputs.TARGET_ENVIRONMENT }}
201+
202+
- name: Sync Documents
203+
uses: ./.github/actions/sync_documents
204+
if: ${{ inputs.TARGET_ENVIRONMENT != 'int' && (inputs.DEPLOY_CODE == true || inputs.IS_PULL_REQUEST == true) }}
205+
with:
206+
TARGET_ENVIRONMENT: ${{ inputs.TARGET_ENVIRONMENT }}
207+
STACK: ${{ inputs.STACK_NAME }}
208+
INT_CLOUD_FORMATION_DEPLOY_ROLE: ${{ secrets.INT_CLOUD_FORMATION_EXECUTE_LAMBDA_ROLE }}
209+
TARGET_CLOUD_FORMATION_DEPLOY_ROLE: ${{ secrets[format('{0}_CLOUD_FORMATION_EXECUTE_LAMBDA_ROLE', github.event.inputs.UPPER_TARGET_ENVIRONMENT)] }}
210+
189211
- name: create_int_release_notes
190212
uses: ./.github/actions/update_confluence_jira
191213
if: ${{ inputs.CREATE_INT_RELEASE_NOTES == true && always() && !failure() && !cancelled() }}

0 commit comments

Comments
 (0)