A script for uploading dotenv files to Github environments:
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
| #!/bin/bash
PAGER="" # Avoid pager when using zsh
# Check if the correct number of arguments are passed
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <org/repo> <environment> < .env"
exit 1
fi
# Parse arguments
ORG_REPO=$1
ENVIRONMENT_NAME=$2
echo "ORG_REPO: $ORG_REPO"
echo "ENVIRONMENT_NAME: $ENVIRONMENT_NAME"
# Get repository ID
REPOSITORY_ID=$(gh api /repos/$ORG_REPO --jq '.id')
if [ -z "$REPOSITORY_ID" ]; then
echo "Error: Repository ID for $ORG/$REPO could not be found." >&2
exit 1
fi
echo "REPOSITORY_ID: $REPOSITORY_ID"
# Read from standard input
while read -r line || [[ -n "$line" ]]; do
# Skip empty and comment lines
if [[ -z "$line" || "${line:0:1}" == "#" ]]; then
continue
fi
# Parse the key-value pair
key=$(echo "$line" | cut -d '=' -f 1)
value=$(echo "$line" | cut -d '=' -f 2)
echo ""
echo "Creating $ENVIRONMENT_NAME/variables/$key..."
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repositories/$REPOSITORY_ID/environments/$ENVIRONMENT_NAME/variables" \
-f "name=$key" \
-f "value=$value"
# echo "Deleting $ENVIRONMENT_NAME/secrets/$key..."
# gh api \
# --method DELETE \
# -H "Accept: application/vnd.github+json" \
# -H "X-GitHub-Api-Version: 2022-11-28" \
# "/repositories/$REPOSITORY_ID/environments/$ENVIRONMENT_NAME/secrets/$key"
done
|