delete-deployment-environment #3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: delete-deployment-environment | |
| on: | |
| workflow_dispatch: | |
| inputs: | |
| action-tag: | |
| description: 'Branch or tag to test' | |
| required: true | |
| default: 'main' | |
| env: | |
| ENVIRONMENT: test | |
| REF: delete-deployment-environment | |
| REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | |
| GH_APP_ID: ${{ secrets.GH_APP_ID }} | |
| GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }} | |
| jobs: | |
| full-cleanup: | |
| name: π§Ό Full Cleanup - Delete Deployments & Environment | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create deployment | |
| uses: chrnorm/deployment-action@v2 | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| initial-status: success | |
| - name: Delete deployments & environment | |
| uses: step-security/delete-deployment-environment@main | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| - name: π§ͺ Verify environment was deleted | |
| run: | | |
| status=$(curl -s -o /dev/null -w "%{http_code}" \ | |
| -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| https://api.github.com/repos/${{ github.repository }}/environments/${{ env.ENVIRONMENT }}) | |
| if [ "$status" -eq 404 ]; then | |
| echo "β Environment deleted" | |
| else | |
| echo "β Environment still exists (status: $status)" | |
| exit 1 | |
| fi | |
| only-remove: | |
| name: ποΈ Only Remove Deployments | |
| needs: full-cleanup | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create deployment | |
| uses: chrnorm/deployment-action@v2 | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| initial-status: success | |
| - name: Remove deployments only | |
| uses: step-security/delete-deployment-environment@main | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| onlyRemoveDeployments: true | |
| - name: π§ͺ Verify deployments were removed | |
| run: | | |
| deployments=$(curl -s \ | |
| -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "https://api.github.com/repos/${{ github.repository }}/deployments?environment=${{ env.ENVIRONMENT }}") | |
| count=$(echo "$deployments" | jq 'length') | |
| if [ "$count" -eq 0 ]; then | |
| echo "β Deployments removed" | |
| else | |
| echo "β Still $count deployments remain" | |
| exit 1 | |
| fi | |
| only-deactivate: | |
| name: π Only Deactivate Deployments | |
| needs: only-remove | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create deployment | |
| uses: chrnorm/deployment-action@v2 | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| initial-status: success | |
| - name: Deactivate deployments only | |
| uses: step-security/delete-deployment-environment@main | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| onlyDeactivateDeployments: true | |
| - name: π§ͺ Verify deployments are marked inactive | |
| run: | | |
| deployments=$(curl -s \ | |
| -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "https://api.github.com/repos/${{ github.repository }}/deployments?environment=${{ env.ENVIRONMENT }}") | |
| total=$(echo "$deployments" | jq 'length') | |
| inactive_count=0 | |
| for id in $(echo "$deployments" | jq -r '.[].id'); do | |
| status=$(curl -s \ | |
| -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "https://api.github.com/repos/${{ github.repository }}/deployments/$id/statuses" | jq -r '.[0].state') | |
| if [ "$status" == "inactive" ]; then | |
| inactive_count=$((inactive_count + 1)) | |
| fi | |
| done | |
| echo "Inactive deployments: $inactive_count / $total" | |
| if [ "$inactive_count" -eq "$total" ]; then | |
| echo "β All deployments are marked as inactive" | |
| else | |
| echo "β Some deployments are not inactive" | |
| exit 1 | |
| fi | |
| ref-only: | |
| name: π― Delete Deployments by Ref Only | |
| needs: only-deactivate | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create deployment | |
| uses: chrnorm/deployment-action@v2 | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| initial-status: success | |
| - name: Remove deployments for a specific ref | |
| uses: step-security/delete-deployment-environment@main | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| onlyRemoveDeployments: true | |
| - name: π§ͺ Verify deployments by ref are removed | |
| run: | | |
| deployments=$(curl -s \ | |
| -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "https://api.github.com/repos/${{ github.repository }}/deployments?ref=${{ env.REF }}&environment=${{ env.ENVIRONMENT }}") | |
| count=$(echo "$deployments" | jq 'length') | |
| if [ "$count" -eq 0 ]; then | |
| echo "β Ref deployments removed" | |
| else | |
| echo "β Still $count deployments for ref remain" | |
| exit 1 | |
| fi | |
| github-app-cleanup: | |
| name: π‘οΈ Cleanup Using GitHub App Token | |
| needs: ref-only | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Create deployment | |
| uses: chrnorm/deployment-action@v2 | |
| with: | |
| token: ${{ env.REPO_ACCESS_TOKEN }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| initial-status: success | |
| - name: Generate GitHub App token | |
| uses: actions/create-github-app-token@v2 | |
| id: get-token | |
| with: | |
| app-id: ${{ env.GH_APP_ID }} | |
| private-key: ${{ env.GH_APP_PRIVATE_KEY }} | |
| - name: Delete with GitHub App token | |
| uses: step-security/delete-deployment-environment@main | |
| with: | |
| token: ${{ steps.get-token.outputs.token }} | |
| environment: ${{ env.ENVIRONMENT }} | |
| ref: ${{ env.REF }} | |
| - name: π§ͺ Verify deployments by ref are removed | |
| run: | | |
| deployments=$(curl -s \ | |
| -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ | |
| -H "Accept: application/vnd.github+json" \ | |
| "https://api.github.com/repos/${{ github.repository }}/deployments?ref=${{ env.REF }}&environment=${{ env.ENVIRONMENT }}") | |
| count=$(echo "$deployments" | jq 'length') | |
| if [ "$count" -eq 0 ]; then | |
| echo "β Ref deployments removed" | |
| else | |
| echo "β Still $count deployments for ref remain" | |
| exit 1 | |
| fi |