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/actions/netlify-deploy/.gitignore b/.github/actions/netlify-deploy/.gitignore new file mode 100644 index 0000000000..3c3629e647 --- /dev/null +++ b/.github/actions/netlify-deploy/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/.github/actions/netlify-deploy/Dockerfile b/.github/actions/netlify-deploy/Dockerfile new file mode 100644 index 0000000000..2fc83bc164 --- /dev/null +++ b/.github/actions/netlify-deploy/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-deploy/action.yml b/.github/actions/netlify-deploy/action.yml new file mode 100644 index 0000000000..432655f13c --- /dev/null +++ b/.github/actions/netlify-deploy/action.yml @@ -0,0 +1,23 @@ +name: netlify action +description: handle the integration with netlify +inputs: + netlify_token: + description: Access token for netlify + required: true + netlify_account_slug: + description: Account slug on netlify + required: true + site_name: + description: Site name on netlify + required: true + directory: + description: Directory containing the files to deploy + required: true +runs: + using: 'docker' + image: 'Dockerfile' + args: + - ${{ inputs.netlify_token }} + - ${{ inputs.netlify_account_slug }} + - ${{ inputs.site_name }} + - ${{ inputs.directory }} diff --git a/.github/actions/netlify-deploy/entrypoint.sh b/.github/actions/netlify-deploy/entrypoint.sh new file mode 100755 index 0000000000..f2659b9585 --- /dev/null +++ b/.github/actions/netlify-deploy/entrypoint.sh @@ -0,0 +1,57 @@ +#!/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" +} + +function get_site_url() { + netlify sites:list --json | jq --raw-output ".[] | select(.name==\"$1\") | .url" +} + +if [ -z "$1" ] || [ -z "$2" ] || [ -z "$3" ] || [ -z "$4" ]; then + usage +fi + +export NETLIFY_AUTH_TOKEN=$1 +account_slug=$2 +site_name=$3 +directory=$4 + +clean_site_name=$(slug $site_name) + +if [ -z "$(get_site_id $clean_site_name)" ]; then + echo "creating site" + netlify sites:create \ + --account-slug ${account_slug} \ + --manual \ + --name "${clean_site_name}" +else + echo "site already exists" +fi + +echo "fetching site id" + +site_id=$(get_site_id ${clean_site_name}) +site_url=$(get_site_url ${clean_site_name}) + +echo +echo "site id $site_id" +echo "site url $site_url" +echo ::set-output name=site_id::$site_id +echo ::set-output name=site_url::$site_url +echo + +echo "deploy site" + +netlify deploy --prod --dir ${directory} --site $site_id diff --git a/.github/workflows/build-pr.yml b/.github/workflows/build-pr.yml index 23438a634a..71632dcb23 100644 --- a/.github/workflows/build-pr.yml +++ b/.github/workflows/build-pr.yml @@ -14,3 +14,13 @@ jobs: - uses: actions/checkout@v2 - name: build image run: docker build --build-arg=ENABLE_ARCHIVES=false --target=current -t documentation:latest . + - name: copy static files + if: github.repository == 'docker/docker.github.io' + run: docker run -v ${PWD}:/output documentation:latest cp -r /usr/share/nginx/html /output/_site + - uses: ./.github/actions/netlify-deploy + if: github.repository == 'docker/docker.github.io' + with: + directory: _site + netlify_token: ${{ secrets.NETLIFY_AUTH_TOKEN }} + netlify_account_slug: ${{ secrets.NETLIFY_ACCOUNT_SLUG }} + site_name: "${{ github.repository }}/${{ github.head_ref }}" 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 }}"