-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-app-token.sh
More file actions
executable file
·44 lines (33 loc) · 1.64 KB
/
generate-app-token.sh
File metadata and controls
executable file
·44 lines (33 loc) · 1.64 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
#!/usr/bin/env bash
set -o pipefail
# Get the directory where the script is located
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Source the utility functions
source "$SCRIPT_DIR/load-env.sh"
source "$SCRIPT_DIR/validate-env.sh"
# Load environment variables
load_env "$SCRIPT_DIR"
# Validate required environment variables
validate_required_vars "CLIENT_ID" "PEM_FILE" "INSTALLATION_ID" "TOKEN_API_DOMAIN"
# call the generate-jwt.sh and pass in CLIENT_ID as first arg and PEM_FILE as second arg
GITHUB_JWT=$("$SCRIPT_DIR/generate-jwt.sh" "$CLIENT_ID" "$PEM_FILE")
# get the url for the app token
APP_TOKEN_URL="https://${TOKEN_API_DOMAIN}/app/installations/${INSTALLATION_ID}/access_tokens"
export APP_TOKEN=$( curl -s -X POST -H "Authorization: Bearer ${GITHUB_JWT}" -H "Accept: application/vnd.github.v3+json" ${APP_TOKEN_URL} | jq -r .token )
# Check if the APP_TOKEN was successfully retrieved
if [[ -z "$APP_TOKEN" ]]; then
echo "Error: Failed to retrieve the app token. Please check your configuration." >&2
exit 1
else
echo -e "\n"
echo "App token successfully generated and stored in APP_TOKEN environment variable."
echo "You can now use this token for further API requests."
echo "Note: The token is valid for 1 hour. You may need to regenerate it periodically."
echo "To regenerate, run this script again."
echo -e "\n-------------------------------------------------"
echo "Generated at: $(date '+%Y-%m-%d %H:%M:%S %Z')"
echo "Expires at: $(date -v+1H '+%Y-%m-%d %H:%M:%S %Z')"
echo -e "\n"
echo "TOKEN: $APP_TOKEN"
echo -e "-------------------------------------------------\n"
fi