-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash.sh
79 lines (62 loc) · 1.91 KB
/
bash.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
# Check if jq is installed
if ! command -v jq &> /dev/null; then
echo "jq is required but it's not installed. Install jq and try again."
exit 1
fi
# Set default region
REGION=${INPUT_REGION:-${AWS_REGION:-us-east-1}}
# Set secret name
SECRET_NAME=${INPUT_SECRET_NAME:-${SECRET_NAME}}
# Load environment variables from .env if it exists
if [ -f .env ]; then
export $(cat .env | xargs)
fi
# Check if running in GitHub Actions
IS_GITHUB_ACTION=${GITHUB_ACTIONS:-false}
# Application name or slug
APP_NAME=${1:-${INPUT_SLUG}}
# Logging function
log() {
echo '###############################################################'
echo "$1"
echo '###############################################################'
}
log "APP_SLUG ENV ~ $APP_NAME"
log "REGION ~ $REGION"
log "SECRET NAME ~ $SECRET_NAME"
# Fetch secrets from AWS Secrets Manager
fetch_secrets() {
aws secretsmanager get-secret-value --region "$REGION" --secret-id "$SECRET_NAME" --query SecretString --output text
}
# Process and write secrets to files
process_secrets() {
local secrets_json
secrets_json=$(fetch_secrets)
if [ -z "$secrets_json" ]; then
echo "Failed to fetch secrets or secret is empty"
exit 1
fi
local env_file=""
local eb_file=""
# Loop through each key-value pair in the JSON
echo "$secrets_json" | jq -r 'to_entries|map("\(.key)=\(.value|tostring)")|.[]' | while IFS= read -r line; do
key=$(echo $line | cut -d'=' -f1)
value=$(echo $line | cut -d'=' -f2)
env_file+="${key}=${value}\n"
eb_file+=" ${key}: ${value}\n"
done
local eb_map="option_settings:
aws:elasticbeanstalk:application:environment:
$eb_file"
if [ "$IS_GITHUB_ACTION" = true ]; then
echo -e "$eb_map" > ./.ebextensions/options.config
echo -e "$env_file" > ./.env
log "GITHUB_ACTION EB ~ $APP_NAME"
fi
}
# Main execution
process_secrets
# Dummy wait to replicate original JavaScript delay
sleep 5
echo "done"