Delete pre-release artifacts #10
This file contains 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 pre-release artifacts | |
on: | |
delete: | |
pull_request: | |
types: [closed] | |
branches: | |
- dev/** | |
jobs: | |
delete: | |
if: | | |
github.event.pull_request.merged == true | |
|| (github.event.ref_type == 'branch' && startsWith(github.event.ref, 'dev/')) | |
name: Delete artifacts | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout main | |
uses: actions/checkout@v4 | |
- name: Extract branch name | |
id: extract_branch | |
shell: bash | |
run: | | |
branch=${{ github.event.pull_request.head.ref || github.event.ref }} | |
if [[ -z "$branch" ]]; then | |
echo "Error: neither github.event.pull_request.head.ref nor github.event.ref exist." | |
exit 1 | |
fi | |
echo "BRANCH_NAME=$(echo $branch | sed 's/^dev\///')" >> $GITHUB_ENV | |
- name: Install doctl | |
uses: digitalocean/action-doctl@v2 | |
with: | |
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN_DEV }} | |
- name: Save DigitalOcean kubeconfig with short-lived credentials | |
run: doctl kubernetes cluster kubeconfig save --expiry-seconds 600 ${{ secrets.K8S_DEV_CLUSTER_ID }} | |
- name: Remove pre-release artifacts | |
continue-on-error: true | |
run: | | |
NAME=${{ env.BRANCH_NAME }} && sed -i 's|<BRANCH>|'${NAME}'|' $GITHUB_WORKSPACE/deployment/development/all.yaml | |
NAME=${{ env.BRANCH_NAME }} && sed -i 's|<BRANCH>|'${NAME}'|' $GITHUB_WORKSPACE/deployment/development/deployment.yaml | |
kubectl delete -f $GITHUB_WORKSPACE/deployment/development/ | |
- name: Get Cloudflare record ID | |
continue-on-error: true | |
id: cf | |
shell: bash | |
run: | | |
LOADBALANCER_IP=$(kubectl get svc -n nginx nginx-ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}') | |
echo "record=$(echo $(curl -X GET "https://api.cloudflare.com/client/v4/zones/${{ secrets.CF_ZONE }}/dns_records?type=A&name=dev-${{ env.BRANCH_NAME }}-epicapp.subsocial.network&content=$LOADBALANCER_IP&proxied=true&page=1&per_page=100&order=type&direction=desc&match=all" -H "X-Auth-Email:${{ secrets.CF_MAIL }}" -H "Authorization:Bearer ${{ secrets.CF_TOKEN }}" -H "Content-Type:application/json" | jq . | jq -c '.result[]' | jq -r .id) )" >> $GITHUB_ENV | |
- name: Delete Cloudflare record | |
continue-on-error: true | |
run: curl -X DELETE "https://api.cloudflare.com/client/v4/zones/${{ secrets.CF_ZONE }}/dns_records/${{ env.record }}" -H "X-Auth-Email:${{ secrets.CF_MAIL }}" -H "Authorization:Bearer ${{ secrets.CF_TOKEN }}" -H "Content-Type:application/json" |