|
| 1 | + |
| 2 | +name: delete-deployment-environment |
| 3 | + |
| 4 | +on: |
| 5 | + workflow_dispatch: |
| 6 | + inputs: |
| 7 | + action-tag: |
| 8 | + description: 'Branch or tag to test' |
| 9 | + required: true |
| 10 | + default: 'main' |
| 11 | + |
| 12 | +env: |
| 13 | + ENVIRONMENT: test |
| 14 | + REF: delete-deployment-environment |
| 15 | + REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} |
| 16 | + GH_APP_ID: ${{ secrets.GH_APP_ID }} |
| 17 | + GH_APP_PRIVATE_KEY: ${{ secrets.GH_APP_PRIVATE_KEY }} |
| 18 | + |
| 19 | +jobs: |
| 20 | + full-cleanup: |
| 21 | + name: 🧼 Full Cleanup - Delete Deployments & Environment |
| 22 | + runs-on: ubuntu-latest |
| 23 | + steps: |
| 24 | + - name: Create deployment |
| 25 | + uses: chrnorm/deployment-action@v2 |
| 26 | + with: |
| 27 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 28 | + environment: ${{ env.ENVIRONMENT }} |
| 29 | + ref: ${{ env.REF }} |
| 30 | + initial-status: success |
| 31 | + |
| 32 | + - name: Delete deployments & environment |
| 33 | + uses: step-security/delete-deployment-environment@main |
| 34 | + with: |
| 35 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 36 | + environment: ${{ env.ENVIRONMENT }} |
| 37 | + |
| 38 | + - name: 🧪 Verify environment was deleted |
| 39 | + run: | |
| 40 | + status=$(curl -s -o /dev/null -w "%{http_code}" \ |
| 41 | + -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ |
| 42 | + -H "Accept: application/vnd.github+json" \ |
| 43 | + https://api.github.com/repos/${{ github.repository }}/environments/${{ env.ENVIRONMENT }}) |
| 44 | + if [ "$status" -eq 404 ]; then |
| 45 | + echo "✅ Environment deleted" |
| 46 | + else |
| 47 | + echo "❌ Environment still exists (status: $status)" |
| 48 | + exit 1 |
| 49 | + fi |
| 50 | + only-remove: |
| 51 | + name: 🗑️ Only Remove Deployments |
| 52 | + needs: full-cleanup |
| 53 | + runs-on: ubuntu-latest |
| 54 | + steps: |
| 55 | + - name: Create deployment |
| 56 | + uses: chrnorm/deployment-action@v2 |
| 57 | + with: |
| 58 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 59 | + environment: ${{ env.ENVIRONMENT }} |
| 60 | + ref: ${{ env.REF }} |
| 61 | + initial-status: success |
| 62 | + |
| 63 | + - name: Remove deployments only |
| 64 | + uses: step-security/delete-deployment-environment@main |
| 65 | + with: |
| 66 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 67 | + environment: ${{ env.ENVIRONMENT }} |
| 68 | + onlyRemoveDeployments: true |
| 69 | + |
| 70 | + - name: 🧪 Verify deployments were removed |
| 71 | + run: | |
| 72 | + deployments=$(curl -s \ |
| 73 | + -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ |
| 74 | + -H "Accept: application/vnd.github+json" \ |
| 75 | + "https://api.github.com/repos/${{ github.repository }}/deployments?environment=${{ env.ENVIRONMENT }}") |
| 76 | + count=$(echo "$deployments" | jq 'length') |
| 77 | + if [ "$count" -eq 0 ]; then |
| 78 | + echo "✅ Deployments removed" |
| 79 | + else |
| 80 | + echo "❌ Still $count deployments remain" |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | + only-deactivate: |
| 84 | + name: 🔕 Only Deactivate Deployments |
| 85 | + needs: only-remove |
| 86 | + runs-on: ubuntu-latest |
| 87 | + steps: |
| 88 | + - name: Create deployment |
| 89 | + uses: chrnorm/deployment-action@v2 |
| 90 | + with: |
| 91 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 92 | + environment: ${{ env.ENVIRONMENT }} |
| 93 | + ref: ${{ env.REF }} |
| 94 | + initial-status: success |
| 95 | + |
| 96 | + - name: Deactivate deployments only |
| 97 | + uses: step-security/delete-deployment-environment@main |
| 98 | + with: |
| 99 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 100 | + environment: ${{ env.ENVIRONMENT }} |
| 101 | + onlyDeactivateDeployments: true |
| 102 | + |
| 103 | + - name: 🧪 Verify deployments are marked inactive |
| 104 | + run: | |
| 105 | + deployments=$(curl -s \ |
| 106 | + -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ |
| 107 | + -H "Accept: application/vnd.github+json" \ |
| 108 | + "https://api.github.com/repos/${{ github.repository }}/deployments?environment=${{ env.ENVIRONMENT }}") |
| 109 | + |
| 110 | + total=$(echo "$deployments" | jq 'length') |
| 111 | + inactive_count=0 |
| 112 | + |
| 113 | + for id in $(echo "$deployments" | jq -r '.[].id'); do |
| 114 | + status=$(curl -s \ |
| 115 | + -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ |
| 116 | + -H "Accept: application/vnd.github+json" \ |
| 117 | + "https://api.github.com/repos/${{ github.repository }}/deployments/$id/statuses" | jq -r '.[0].state') |
| 118 | + |
| 119 | + if [ "$status" == "inactive" ]; then |
| 120 | + inactive_count=$((inactive_count + 1)) |
| 121 | + fi |
| 122 | + done |
| 123 | + |
| 124 | + echo "Inactive deployments: $inactive_count / $total" |
| 125 | + if [ "$inactive_count" -eq "$total" ]; then |
| 126 | + echo "✅ All deployments are marked as inactive" |
| 127 | + else |
| 128 | + echo "❌ Some deployments are not inactive" |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | + |
| 132 | + ref-only: |
| 133 | + name: 🎯 Delete Deployments by Ref Only |
| 134 | + needs: only-deactivate |
| 135 | + runs-on: ubuntu-latest |
| 136 | + steps: |
| 137 | + - name: Create deployment |
| 138 | + uses: chrnorm/deployment-action@v2 |
| 139 | + with: |
| 140 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 141 | + environment: ${{ env.ENVIRONMENT }} |
| 142 | + ref: ${{ env.REF }} |
| 143 | + initial-status: success |
| 144 | + |
| 145 | + - name: Remove deployments for a specific ref |
| 146 | + uses: step-security/delete-deployment-environment@main |
| 147 | + with: |
| 148 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 149 | + environment: ${{ env.ENVIRONMENT }} |
| 150 | + ref: ${{ env.REF }} |
| 151 | + onlyRemoveDeployments: true |
| 152 | + |
| 153 | + - name: 🧪 Verify deployments by ref are removed |
| 154 | + run: | |
| 155 | + deployments=$(curl -s \ |
| 156 | + -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ |
| 157 | + -H "Accept: application/vnd.github+json" \ |
| 158 | + "https://api.github.com/repos/${{ github.repository }}/deployments?ref=${{ env.REF }}&environment=${{ env.ENVIRONMENT }}") |
| 159 | + count=$(echo "$deployments" | jq 'length') |
| 160 | + if [ "$count" -eq 0 ]; then |
| 161 | + echo "✅ Ref deployments removed" |
| 162 | + else |
| 163 | + echo "❌ Still $count deployments for ref remain" |
| 164 | + exit 1 |
| 165 | + fi |
| 166 | + github-app-cleanup: |
| 167 | + name: 🛡️ Cleanup Using GitHub App Token |
| 168 | + needs: ref-only |
| 169 | + runs-on: ubuntu-latest |
| 170 | + steps: |
| 171 | + - name: Create deployment |
| 172 | + uses: chrnorm/deployment-action@v2 |
| 173 | + with: |
| 174 | + token: ${{ env.REPO_ACCESS_TOKEN }} |
| 175 | + environment: ${{ env.ENVIRONMENT }} |
| 176 | + ref: ${{ env.REF }} |
| 177 | + initial-status: success |
| 178 | + |
| 179 | + - name: Generate GitHub App token |
| 180 | + uses: actions/create-github-app-token@v2 |
| 181 | + id: get-token |
| 182 | + with: |
| 183 | + app-id: ${{ env.GH_APP_ID }} |
| 184 | + private-key: ${{ env.GH_APP_PRIVATE_KEY }} |
| 185 | + |
| 186 | + - name: Delete with GitHub App token |
| 187 | + uses: step-security/delete-deployment-environment@main |
| 188 | + with: |
| 189 | + token: ${{ steps.get-token.outputs.token }} |
| 190 | + environment: ${{ env.ENVIRONMENT }} |
| 191 | + ref: ${{ env.REF }} |
| 192 | + |
| 193 | + - name: 🧪 Verify deployments by ref are removed |
| 194 | + run: | |
| 195 | + deployments=$(curl -s \ |
| 196 | + -H "Authorization: token ${{ env.REPO_ACCESS_TOKEN }}" \ |
| 197 | + -H "Accept: application/vnd.github+json" \ |
| 198 | + "https://api.github.com/repos/${{ github.repository }}/deployments?ref=${{ env.REF }}&environment=${{ env.ENVIRONMENT }}") |
| 199 | + count=$(echo "$deployments" | jq 'length') |
| 200 | + if [ "$count" -eq 0 ]; then |
| 201 | + echo "✅ Ref deployments removed" |
| 202 | + else |
| 203 | + echo "❌ Still $count deployments for ref remain" |
| 204 | + exit 1 |
| 205 | + fi |
0 commit comments