forked from cf-toolsuite/tanzu-genai-showcase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-gh-secrets.sh
More file actions
executable file
·160 lines (136 loc) · 3.91 KB
/
create-gh-secrets.sh
File metadata and controls
executable file
·160 lines (136 loc) · 3.91 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/bin/bash
#
# Script to create GitHub repository secrets from a .env file
# This script reads key-value pairs from a .env file and creates GitHub repository secrets
# using the GitHub CLI (gh)
set -e
# Default values
ENV_FILE=".env"
VERBOSE=false
FORCE=false
# Function to display usage information
usage() {
echo "Usage: $0 [OPTIONS]"
echo "Create GitHub repository secrets from a .env file"
echo ""
echo "Options:"
echo " -f, --file FILE Specify the .env file to use (default: .env)"
echo " -v, --verbose Enable verbose output"
echo " -y, --yes Skip confirmation prompts"
echo " -h, --help Display this help message"
echo ""
echo "Example:"
echo " $0 --file ./configs/.env.production"
exit 1
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Function to log messages if verbose mode is enabled
log() {
if [ "$VERBOSE" = true ]; then
echo "$1"
fi
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f|--file)
ENV_FILE="$2"
shift
shift
;;
-v|--verbose)
VERBOSE=true
shift
;;
-y|--yes)
FORCE=true
shift
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1"
usage
;;
esac
done
# Check if GitHub CLI is installed
if ! command_exists gh; then
echo "Error: GitHub CLI (gh) is not installed."
echo "Please install it from https://cli.github.com/ and try again."
exit 1
fi
# Check if the user is authenticated with GitHub
if ! gh auth status >/dev/null 2>&1; then
echo "Error: You are not authenticated with GitHub."
echo "Please run 'gh auth login' and try again."
exit 1
fi
# Check if the .env file exists
if [ ! -f "$ENV_FILE" ]; then
echo "Error: File '$ENV_FILE' does not exist."
exit 1
fi
# Count the number of secrets to be created
SECRET_COUNT=$(grep -v '^#' "$ENV_FILE" | grep -v '^$' | wc -l)
echo "Found $SECRET_COUNT potential secrets in $ENV_FILE"
# Confirm before proceeding
if [ "$FORCE" != true ]; then
read -p "Do you want to create/update GitHub secrets from this file? (y/N) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo "Operation cancelled."
exit 0
fi
fi
# Create secrets
SUCCESS_COUNT=0
FAILURE_COUNT=0
echo "Creating GitHub secrets..."
# Process each line in the .env file
while IFS= read -r line || [[ -n "$line" ]]; do
# Skip comments and empty lines
if [[ "$line" =~ ^#.*$ ]] || [[ -z "$line" ]]; then
continue
fi
# Extract key and value
if [[ "$line" =~ ^([^=]+)=(.*)$ ]]; then
KEY="${BASH_REMATCH[1]}"
VALUE="${BASH_REMATCH[2]}"
# Remove surrounding quotes if present
VALUE="${VALUE#\"}"
VALUE="${VALUE%\"}"
VALUE="${VALUE#\'}"
VALUE="${VALUE%\'}"
# Remove trailing comments if present
VALUE=$(echo "$VALUE" | sed 's/[[:space:]]*#.*$//')
# Trim whitespace
KEY=$(echo "$KEY" | xargs)
VALUE=$(echo "$VALUE" | xargs)
log "Setting secret: $KEY"
# Create or update the secret
if echo "$VALUE" | gh secret set "$KEY" 2>/dev/null; then
log "✓ Successfully set secret: $KEY"
((SUCCESS_COUNT++))
else
echo "✗ Failed to set secret: $KEY"
((FAILURE_COUNT++))
fi
fi
done < "$ENV_FILE"
# Summary
echo "Summary:"
echo "- Total secrets processed: $((SUCCESS_COUNT + FAILURE_COUNT))"
echo "- Successfully created/updated: $SUCCESS_COUNT"
echo "- Failed: $FAILURE_COUNT"
if [ $FAILURE_COUNT -gt 0 ]; then
echo "Some secrets failed to be created/updated. Check the output above for details."
exit 1
fi
echo "All secrets were successfully created/updated!"
exit 0