Skip to content

Commit 93d2799

Browse files
DEVOPS-47 updated script and to get public key id
1 parent 10f0095 commit 93d2799

4 files changed

Lines changed: 42 additions & 1 deletion

File tree

.github/workflows/create_github_secrets_using_workflow.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ jobs:
3737
run: |
3838
public_key=$(bash get_public_key.sh ${{inputs.organization}})
3939
echo "PUBLIC_KEY=$public_key" >> $GITHUB_OUTPUT
40+
- name: get public key id
41+
id: get-public-key-id
42+
env:
43+
GH_TOKEN: ${{ secrets.DEVWITHKRISHNA_PERSONAL_ACCESS_TOKEN }}
44+
run: |
45+
public_key_id=$(bash get_public_key_id.sh ${{inputs.organization}})
46+
echo "PUBLIC_KEY_ID=$public_key_id" >> $GITHUB_OUTPUT
4047
- name: Encrypt secret
4148
id: encrypt-secret
4249
env:
@@ -49,6 +56,7 @@ jobs:
4956
organization: ${{ inputs.organization }}
5057
secret_name: ${{ inputs.secret_name }}
5158
ENCRYPTED_SECRET: ${{ env.ENCRYPTED_SECRET }}
59+
PUBLIC_KEY_ID: ${{ steps.get-public-key-id.outputs.public_key_id }}
5260
GH_TOKEN: ${{ secrets.DEVWITHKRISHNA_PERSONAL_ACCESS_TOKEN }}
5361
run: |
5462
pipenv run python3 create_or_update_github_org_secret.py

create_or_update_github_org_secret.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ def create_or_update_organization_secret_github(organization: str, secret_name:
3535
}
3636
data = {
3737
"encrypted_value": encrypted_secret,
38-
"visibility": "all"
38+
"visibility": "all",
39+
"key_id": os.getenv('PUBLIC_KEY_ID')
3940
}
4041
response = requests.put(github_org_secret_endpoint, headers=headers, json=data)
4142
print(response.json())

get_public_key.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ response=$(curl -sL \
1717
-H "X-GitHub-Api-Version: 2022-11-28" \
1818
https://api.github.com/orgs/$ORGANIZATION/actions/secrets/public-key)
1919

20+
echo $response
21+
sleep 10
2022
# Checking if the request was successful (status code 200)
2123
if [ ! -z "$response" ]; then
2224
# Extracting the public key from the response

get_public_key_id.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#! /bin/bash
2+
3+
# The organization name. The name is not case sensitive.
4+
ORGANIZATION=$1
5+
6+
# Checking if the Organization name is non-empty
7+
if [ -z "$ORGANIZATION" ]; then
8+
echo "Organization name is empty"
9+
echo "Usage: $0 <Organization name>"
10+
exit 1
11+
fi
12+
13+
# Making the API call and capturing the response
14+
response=$(curl -sL \
15+
-H "Accept: application/vnd.github+json" \
16+
-H "Authorization: Bearer $GH_TOKEN" \
17+
-H "X-GitHub-Api-Version: 2022-11-28" \
18+
https://api.github.com/orgs/$ORGANIZATION/actions/secrets/public-key)
19+
20+
echo $response
21+
sleep 10
22+
# Checking if the request was successful (status code 200)
23+
if [ ! -z "$response" ]; then
24+
# Extracting the public key from the response
25+
public_key_id=$(echo "$response" | jq -r '.key_id')
26+
echo "$public_key_id"
27+
28+
else
29+
echo "Failed to retrieve public key id for $ORGANIZATION. Response is empty."
30+
fi

0 commit comments

Comments
 (0)