From 22a6ee24794072f91d7f12f1f031bcd834e2bd81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Drouet?= Date: Tue, 3 Mar 2020 16:20:20 +0100 Subject: [PATCH] ci: clean netlify when PR get closed or merged MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérémie Drouet --- .github/actions/netlify-clean/Dockerfile | 8 +++++ .github/actions/netlify-clean/action.yml | 15 ++++++++++ .github/actions/netlify-clean/entrypoint.sh | 33 +++++++++++++++++++++ .github/workflows/clean-pr.yml | 21 +++++++++++++ 4 files changed, 77 insertions(+) create mode 100644 .github/actions/netlify-clean/Dockerfile create mode 100644 .github/actions/netlify-clean/action.yml create mode 100755 .github/actions/netlify-clean/entrypoint.sh create mode 100644 .github/workflows/clean-pr.yml diff --git a/.github/actions/netlify-clean/Dockerfile b/.github/actions/netlify-clean/Dockerfile new file mode 100644 index 0000000000..2fc83bc164 --- /dev/null +++ b/.github/actions/netlify-clean/Dockerfile @@ -0,0 +1,8 @@ +FROM node:lts-alpine + +RUN apk add --no-cache jq +RUN npm i -g netlify-cli + +COPY entrypoint.sh /entrypoint.sh + +ENTRYPOINT [ "/entrypoint.sh" ] diff --git a/.github/actions/netlify-clean/action.yml b/.github/actions/netlify-clean/action.yml new file mode 100644 index 0000000000..486e155e49 --- /dev/null +++ b/.github/actions/netlify-clean/action.yml @@ -0,0 +1,15 @@ +name: netlify action +description: handle the integration with netlify +inputs: + netlify_token: + description: Access token for netlify + required: true + site_name: + description: Site name on netlify + required: true +runs: + using: 'docker' + image: 'Dockerfile' + args: + - ${{ inputs.netlify_token }} + - ${{ inputs.site_name }} diff --git a/.github/actions/netlify-clean/entrypoint.sh b/.github/actions/netlify-clean/entrypoint.sh new file mode 100755 index 0000000000..cad9ee7223 --- /dev/null +++ b/.github/actions/netlify-clean/entrypoint.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +set -xe + +function usage() { + echo "Usage: " + exit 1 +} + +function slug() { + echo $1 | sed -E s/[^a-zA-Z0-9]+/-/g | sed -E s/^-+\|-+$//g | tr A-Z a-z +} + +function get_site_id() { + netlify sites:list --json | jq --raw-output ".[] | select(.name==\"$1\") | .id" +} + +if [ -z "$1" ] || [ -z "$2" ]; then + usage +fi + +export NETLIFY_AUTH_TOKEN=$1 +site_name=$2 + + +echo "searching site ${site_name}" + +clean_site_name=$(slug $site_name) +site_id=$(get_site_id $clean_site_name) + +echo "deleting site" + +netlify sites:delete --force "${site_id}" diff --git a/.github/workflows/clean-pr.yml b/.github/workflows/clean-pr.yml new file mode 100644 index 0000000000..26e4d53c72 --- /dev/null +++ b/.github/workflows/clean-pr.yml @@ -0,0 +1,21 @@ +name: remove published site from netlify + +on: + pull_request: + types: + - closed + +jobs: + remove-site-from-netlify: + name: build + runs-on: ubuntu-latest + if: github.repository == 'docker/docker.github.io' + steps: + - uses: actions/checkout@v2 + with: + ref: ${{ github.event.pull_request.head.sha }} + - name: delete from netlify + uses: ./.github/actions/netlify-clean + with: + netlify_token: ${{ secrets.NETLIFY_AUTH_TOKEN }} + site_name: "${{ github.repository }}/${{ github.head_ref }}"