Delete GitHub Repository
In the following guide, you are going to create a self-service action in Port that executes a GitHub workflow to delete a GitHub repository.
This guide includes one or more steps that require integration with GitHub.
Port supports two GitHub integrations:
- GitHub (Legacy) - uses a GitHub app, which is soon to be deprecated.
- GitHub (Ocean) - uses the Ocean framework, recommended for new integrations.
Both integration options are present in this guide via tabs, choose the one that fits your needs.
Common use cases
- Declutter Development: Clean up outdated, unused, or test repositories to streamline your GitHub environment.
- Project Sunset: Offboard completed projects by gracefully deleting their repositories.
- Enhanced Control: Manage repository lifecycles without needing in-depth GitHub permissions.
Prerequisites
- Port's GitHub Integration: Install it by clicking here. This is essential for Port to interact with your GitHub repositories.
- GitHub Data in Port: Ensure your repositories are synced with Port. If you haven't set this up yet, follow this quick guide.
- Workflow Repository: Decide on an existing repository where you'll store your GitHub workflow file, or create a dedicated repository for your Port actions.
Add GitHub secrets
- Create the following GitHub Action secrets:
- Create the following Port credentials:
PORT_CLIENT_ID- Port Client ID learn more.PORT_CLIENT_SECRET- Port Client Secret learn more.
GH_TOKEN- a Classic Personal Access Token with the following scopes:repoanddelete_repo
- Create the following Port credentials:
Set up self-service action
-
Go to the self-service page.
-
Click on the
+ New Actionbutton. -
Click on the
{...} Edit JSONbutton. -
Copy and paste the following JSON configuration into the editor.
- GitHub (Legacy)
- GitHub (Ocean)
Port Action: Delete GitHub Repository (click to expand)
Replace the variables-
<GITHUB-ORG>- your GitHub organization or user name. -
<GITHUB-REPO-NAME>- your GitHub repository name.
Note: Replace the
blueprintIdentifieron line 30 with the id of your own blueprint.{"identifier": "service_delete_repo","title": "Delete Repo","icon": "Github","description": "A github action that deletes a github repo","trigger": {"type": "self-service","operation": "DELETE","userInputs": {"properties": {"org_name": {"icon": "Github","title": "Organisation Name","type": "string","default": "default-org"},"delete_dependents": {"icon": "Github","title": "Delete Dependent Items","type": "boolean","default": false}},"required": [],"order": ["org_name","delete_dependents"]},"blueprintIdentifier": "service"},"invocationMethod": {"type": "GITHUB","org": "<GITHUB-ORG>","repo": "<GITHUB-REPO-NAME>","workflow": "delete-repo.yml","workflowInputs": {"org_name": "{{inputs.org_name}}","delete_dependents": "{{inputs.delete_dependents}}","port_context": {"entity": "{{.entity.identifier}}","blueprint": "{{.action.blueprint}}","runId": "{{.run.id}}","trigger": "{{ .trigger }}"}},"reportWorkflowStatus": true},"requiredApproval": false}Port Action: Delete GitHub Repository (click to expand)
Replace the variables-
<GITHUB-ORG>- your GitHub organization or user name. -
<GITHUB-REPO-NAME>- your GitHub repository name.
Note: Replace the
blueprintIdentifieron line 30 with the id of your own blueprint.{"identifier": "service_delete_repo","title": "Delete Repo","icon": "Github","description": "A github action that deletes a github repo","trigger": {"type": "self-service","operation": "DELETE","userInputs": {"properties": {"org_name": {"icon": "Github","title": "Organisation Name","type": "string","default": "default-org"},"delete_dependents": {"icon": "Github","title": "Delete Dependent Items","type": "boolean","default": false}},"required": [],"order": ["org_name","delete_dependents"]},"blueprintIdentifier": "service"},"invocationMethod": {"type": "INTEGRATION_ACTION","installationId": "<YOUR_GITHUB_OCEAN_INTEGRATION_ID>","integrationActionType": "dispatch_workflow","integrationActionExecutionProperties": {"org": "<GITHUB-ORG>","repo": "<GITHUB-REPO-NAME>","workflow": "delete-repo.yml","workflowInputs": {"org_name": "{{inputs.org_name}}","delete_dependents": "{{inputs.delete_dependents}}","port_context": {"entity": "{{.entity.identifier}}","blueprint": "{{.action.blueprint}}","runId": "{{.run.id}}","trigger": "{{ .trigger }}"}},"reportWorkflowStatus": true}},"requiredApproval": false} -
Click
Saveto create the action.
Create GitHub workflow
Create a workflow file under .github/workflows/delete-repo.yml with the following content:
GitHub workflow script
name: Delete Repository
on:
workflow_dispatch:
inputs:
org_name:
required: true
type: string
delete_dependents:
required: false
type: boolean
default: false
port_context:
required: true
type: string
jobs:
delete-repo:
runs-on: ubuntu-latest
steps:
- name: Inform starting of deletion
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
operation: PATCH_RUN
runId: ${{ fromJson(inputs.port_context).runId }}
logMessage: |
Deleting a github repository... ⛴️
- name: Delete Repository
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
REPO_NAME: ${{ fromJson(inputs.port_context).entity }}
run: |
echo $GH_TOKEN
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
"https://api.github.com/repos/${{ inputs.org_name }}/$REPO_NAME")
echo "HTTP Status: $HTTP_STATUS"
# Check if HTTP_STATUS is 204 (No Content)
if [ $HTTP_STATUS -eq 204 ]; then
echo "Repository deleted successfully."
echo "delete_successful=true" >> $GITHUB_ENV
else
echo "Failed to delete repository. HTTP Status: $HTTP_STATUS"
echo "delete_successful=false" >> $GITHUB_ENV
fi
- name: Delete record in Port
if: ${{ env.delete_successful == 'true' }}
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
operation: DELETE
delete_dependents: ${{ inputs.delete_dependents }}
identifier: ${{ fromJson(inputs.port_context).entity }}
blueprint: ${{ fromJson(inputs.port_context).blueprint }}
- name: Inform completion of deletion
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
operation: PATCH_RUN
runId: ${{ fromJson(inputs.port_context).runId }}
logMessage: |
GitHub repository deleted! ✅
Let's test it!
- Go to the self-service page of your Port application.
- Choose the GitHub repository you want to delete.
- Click on
Execute. - Done! wait for the repository to be deleted in GitHub.