Skip to content

Commit 50e2953

Browse files
committed
Support repository variables in ghub-set-vars
ghub-set-vars currently sets every provided environment variable as a repository secret. GitHub automatically masks values of secrets with asterisks. Some of the environment variables are not confidential and should actually be shown to users. This can be done by setting the environment variables as 'repository variables' instead of 'repository secrets' in GitHub Signed-off-by: Tomáš Nevrlka <[email protected]>
1 parent 4561945 commit 50e2953

File tree

1 file changed

+54
-32
lines changed

1 file changed

+54
-32
lines changed

hack/ghub-set-vars

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,67 @@
11
#!/bin/bash
2-
SCRIPTDIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" > /dev/null 2>&1 && pwd)"
32

4-
if [ $# -eq 1 ]; then
5-
REPO_URL=$1
6-
# Assume $REPO_URL is like "https://github.com/org/repo"
7-
# and use cut to pull out the "org/repo" part
8-
ORG_AND_REPO=$(echo "$REPO_URL" | cut -d/ -f4,5)
3+
# Helper script used to simplify setting
4+
# variables and secrets in a GitHub repository
5+
6+
set -euo pipefail
7+
8+
function echo_usage() {
9+
echo "Usage: $0 OWNER/REPO"
10+
echo " $0 https://github.com/OWNER/REPO"
11+
}
12+
13+
if [ $# -ne 1 ]; then
14+
echo "Invalid number of arguments"
15+
echo
16+
echo_usage
17+
exit 1
918
fi
1019

11-
function setVars() {
12-
NAME=$1
13-
VALUE=$2
20+
github_repository=$1
21+
22+
# Naive check that the provided repository in the argument matches
23+
# the expected format (see usage)
24+
if ! [[ "$github_repository" =~ ^(https://github.com/)?(.+/.+)$ ]]; then
25+
echo "Invalid format of the provided argument '${github_repository}'"
26+
echo
27+
echo_usage
28+
fi
29+
30+
# Set repository variable via GitHub CLI
31+
# The value of the variable will NOT be hidden in the logs
32+
function set_variable() {
33+
echo "Setting variable '$1' in $github_repository..."
34+
gh variable set "$1" --body "$2" --repo "$github_repository"
35+
}
1436

15-
echo "setting Secret $NAME in github.com/$ORG_AND_REPO"
16-
if [ -z "$VALUE" ]; then
17-
gh secret set "$NAME" --body " " --repo "$ORG_AND_REPO"
18-
else
19-
gh secret set "$NAME" --body "$VALUE" --repo "$ORG_AND_REPO"
20-
fi
37+
# Set repository secret via GitHub CLI
38+
function set_secret() {
39+
echo "Setting secret '$1' in $github_repository..."
40+
gh secret set "$1" --body "$2" --repo "$github_repository"
2141
}
2242

23-
setVars IMAGE_REGISTRY quay.io/$QUAY_IO_CREDS_USR
24-
setVars IMAGE_REGISTRY_USER $QUAY_IO_CREDS_USR
25-
setVars IMAGE_REGISTRY_PASSWORD $QUAY_IO_CREDS_PSW
43+
# Set the minimum required variables and secrets
44+
set_variable IMAGE_REGISTRY quay.io/"$QUAY_IO_CREDS_USR"
45+
set_variable IMAGE_REGISTRY_USER "$QUAY_IO_CREDS_USR"
46+
set_secret IMAGE_REGISTRY_PASSWORD "$QUAY_IO_CREDS_PSW"
2647

27-
setVars ROX_CENTRAL_ENDPOINT $ROX_CENTRAL_ENDPOINT
28-
setVars ROX_API_TOKEN $ROX_API_TOKEN
48+
set_variable ROX_CENTRAL_ENDPOINT "$ROX_CENTRAL_ENDPOINT"
49+
set_secret ROX_API_TOKEN "$ROX_API_TOKEN"
2950

30-
setVars GITOPS_AUTH_PASSWORD $GITOPS_AUTH_PASSWORD
51+
set_secret GITOPS_AUTH_PASSWORD "$GITOPS_AUTH_PASSWORD"
3152

32-
setVars QUAY_IO_CREDS_USR $QUAY_IO_CREDS_USR
33-
setVars QUAY_IO_CREDS_PSW $QUAY_IO_CREDS_PSW
53+
set_variable QUAY_IO_CREDS_USR "$QUAY_IO_CREDS_USR"
54+
set_secret QUAY_IO_CREDS_PSW "$QUAY_IO_CREDS_PSW"
3455

35-
setVars COSIGN_SECRET_PASSWORD $COSIGN_SECRET_PASSWORD
36-
setVars COSIGN_SECRET_KEY $COSIGN_SECRET_KEY
37-
setVars COSIGN_PUBLIC_KEY $COSIGN_PUBLIC_KEY
56+
set_secret COSIGN_SECRET_PASSWORD "$COSIGN_SECRET_PASSWORD"
57+
set_secret COSIGN_SECRET_KEY "$COSIGN_SECRET_KEY"
58+
set_variable COSIGN_PUBLIC_KEY "$COSIGN_PUBLIC_KEY"
3859

39-
setVars TRUSTIFICATION_BOMBASTIC_API_URL "$TRUSTIFICATION_BOMBASTIC_API_URL"
40-
setVars TRUSTIFICATION_OIDC_ISSUER_URL "$TRUSTIFICATION_OIDC_ISSUER_URL"
41-
setVars TRUSTIFICATION_OIDC_CLIENT_ID "$TRUSTIFICATION_OIDC_CLIENT_ID"
42-
setVars TRUSTIFICATION_OIDC_CLIENT_SECRET "$TRUSTIFICATION_OIDC_CLIENT_SECRET"
43-
setVars TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION "$TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION"
60+
set_variable TRUSTIFICATION_BOMBASTIC_API_URL "$TRUSTIFICATION_BOMBASTIC_API_URL"
61+
set_variable TRUSTIFICATION_OIDC_ISSUER_URL "$TRUSTIFICATION_OIDC_ISSUER_URL"
62+
set_variable TRUSTIFICATION_OIDC_CLIENT_ID "$TRUSTIFICATION_OIDC_CLIENT_ID"
63+
set_variable TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION "$TRUSTIFICATION_SUPPORTED_CYCLONEDX_VERSION"
64+
set_secret TRUSTIFICATION_OIDC_CLIENT_SECRET "$TRUSTIFICATION_OIDC_CLIENT_SECRET"
4465

45-
gh secret list
66+
echo
67+
echo "All variables and secrets are set."

0 commit comments

Comments
 (0)