Skip to content

Commit d833f18

Browse files
Create .env file from GH actions secret, add util script to update the secret from local .env file
1 parent 1a14654 commit d833f18

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

.github/workflows/build-prod-image.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ jobs:
2626
id: get_version
2727
run: echo "VERSION=${GITHUB_REF#refs/tags/v}" >> $GITHUB_OUTPUT
2828

29+
- name: Create .env file from secret
30+
run: echo "${{ secrets.PRODUCTION_ENV_FILE }}" > .env
31+
2932
- name: Build and push
3033
uses: docker/build-push-action@v4
3134
with:

update_env_secret.sh

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/bin/bash
2+
3+
# Set the repository secret from the local production environment file
4+
# Requires GitHub CLI (gh) to be installed and authenticated
5+
6+
# Default values
7+
ENV_FILE="prod.env"
8+
SECRET_NAME="PRODUCTION_ENV_FILE"
9+
REPO="SoleSearchAPI/api"
10+
11+
# Parse command line arguments
12+
while (("$#")); do
13+
case "$1" in
14+
--file)
15+
ENV_FILE="$2"
16+
shift 2
17+
;;
18+
--secret)
19+
SECRET_NAME="$2"
20+
shift 2
21+
;;
22+
--repo)
23+
REPO="$2"
24+
shift 2
25+
;;
26+
*)
27+
echo "Unknown option: $1"
28+
exit 1
29+
;;
30+
esac
31+
done
32+
33+
# Check if env file exists
34+
if [ ! -f "$ENV_FILE" ]; then
35+
echo "Error: Environment file '$ENV_FILE' not found"
36+
exit 1
37+
fi
38+
39+
# Set the repository parameter if provided
40+
REPO_PARAM=""
41+
if [ -n "$REPO" ]; then
42+
REPO_PARAM="--repo $REPO"
43+
fi
44+
45+
# Update the secret using GitHub CLI
46+
echo "Setting GitHub secret '$SECRET_NAME' from file '$ENV_FILE'..."
47+
gh secret set "$SECRET_NAME" $REPO_PARAM <"$ENV_FILE"
48+
49+
if [ $? -eq 0 ]; then
50+
echo "Secret '$SECRET_NAME' updated successfully!"
51+
else
52+
echo "Failed to update secret. Make sure GitHub CLI is installed and you're authenticated."
53+
exit 1
54+
fi

0 commit comments

Comments
 (0)