Skip to content

Commit fb48fbf

Browse files
DEVOPS-48 delete org secret
1 parent 9e2d3e7 commit fb48fbf

File tree

4 files changed

+118
-4
lines changed

4 files changed

+118
-4
lines changed

.github/workflows/create_github_secrets_using_workflow.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: create-organization-github-secrets-from-workflow
1+
name: create-or-update-organization-github-secrets-from-workflow
22
on:
33
workflow_dispatch:
44
inputs:
@@ -17,7 +17,7 @@ on:
1717
required: true
1818
run-name: ${{ github.actor }} creating secrets in ${{ inputs.organization }}
1919
jobs:
20-
create-organization-github-secrets-from-workflow:
20+
create-or-update-organization-github-secrets-from-workflow:
2121
runs-on: ubuntu-latest
2222
steps:
2323
- name: git checkout
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: create-organization-github-secrets-from-workflow
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
organization:
6+
type: string
7+
default: 'devwithkrishna'
8+
description: 'The GitHub organization where the repository will be created.'
9+
required: true
10+
secret_name:
11+
type: string
12+
description: "Secret name to create/update on org level"
13+
required: true
14+
15+
run-name: ${{ github.actor }} deleting secret ${{ inputs.secret_name }} in ${{ inputs.organization }}
16+
jobs:
17+
delete-organization-github-secrets-from-workflow:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- name: git checkout
21+
uses: actions/checkout@v4
22+
- name: delete github org secret
23+
run: |
24+
bash delete_github_org_secret.sh ${{ inputs.organization }} ${{ inputs.secret_name}}
25+
echo "Secret deleted"
26+
- name: Completed
27+
run: |
28+
echo "program completed successfully"

README.md

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,62 @@
1-
# automatically-create-github-secrets
2-
from azure keyvault pull secrets and using them update or create github secrets in a specified repo
1+
# automatically-create-delete-update-github-organization-secrets
2+
create , delete or update github organization secrets using github workflow
3+
4+
# Pre requesites
5+
* This requires an authorization method which has organization-secret with write permission
6+
* Personal Access Token (PAT) is the recommended way to authenticate. In this demo PAT is USED.
7+
* You can generate a new one from Github settings
8+
* You need to encrypt a secret before you can create or update secrets.
9+
10+
11+
# How code works for create or update secret
12+
13+
14+
* First this will execute the `get_public_key.sh` shell script to get the Organization public key
15+
* This public key is required and used for encryption of secret
16+
17+
`Reference`: [get-an-organization-public-key](https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#get-an-organization-public-key)
18+
19+
* Then it will execute the `get_public_key_id.sh` script to get the organization key id.
20+
* This is required for creation or updation of secret
21+
22+
* Then the `python program` `encrypt_using_libnacl` this uses the public key from step 1 and encrypts the secret
23+
using the prefered method by GitHub.
24+
25+
`Reference`: [create-or-update-an-organization-secret](https://docs.github.com/en/rest/actions/secrets?apiVersion=2022-11-28#create-or-update-an-organization-secret)
26+
27+
- Reference used for encryption : [example-encrypting-a-secret-using-python](https://docs.github.com/en/rest/guides/encrypting-secrets-for-the-rest-api?apiVersion=2022-11-28#example-encrypting-a-secret-using-python )
28+
29+
* Then `Python program` `create_or_update_github_org_secret` is used to take the public key id from step 2 and encrypted secret value from step 3 to create or update the secret.
30+
31+
| status code | operation |
32+
|-------------|-----------|
33+
| 201 | Create Org secret|
34+
| 204 | Update an Org secret |
35+
36+
37+
- visibility of organization secret has been set to all organization repositories. selected means only the repositories specified by selected_repository_ids can access the secret.
38+
- Can be one of: `all`, `private`, `selected`
39+
40+
41+
## Inputs of workflow
42+
43+
| input name | description|
44+
|------------|------------|
45+
| organization | name of github organization |
46+
| secret_name | organization Secret name |
47+
| secret_value | Secret value |
48+
49+
50+
# # How code works for deleting an organization secret
51+
52+
* This runs the shell script `delete_github_org_secret.sh` which takes 2 inputs from github workflow
53+
1. organization name
54+
2. secret name
55+
56+
* Then deletes the secret
57+
58+
| input | description|
59+
|-------|--------------|
60+
| organization | GitHub Organization name |
61+
| secret_name | Secert to be deleted |
62+

delete_github_org_secret.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#! /bin/bash
2+
3+
# The organization name. The name is not case sensitive.
4+
ORGANIZATION=$1
5+
SECRET_NAME=$2
6+
# Checking if the Organization name or secret name is non-empty
7+
if [ -z "$ORGANIZATION" ] || [ -z "$SECRET_NAME" ]; then
8+
echo "Either Organization name or Secret name is empty"
9+
echo "Usage: $0 <Organization name> <Secret name>"
10+
exit 1
11+
fi
12+
# Making the API call and capturing the response
13+
response=$(curl -sL \
14+
-X DELETE \
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/$SECRET_NAME)
19+
20+
21+
# Checking if the request was successful (status code 200)
22+
if [[ "$response" == *"Status: 204"* ]];; then
23+
echo "Secret $SECRET_NAME deleted successfully from organization $ORGANIZATION"
24+
else
25+
echo "Failed to delete secret $SECRET_NAME from organization $ORGANIZATION"
26+
fi

0 commit comments

Comments
 (0)