From c52480b75981ac347132edceed3aa7f12f6db706 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Fri, 10 Dec 2021 14:08:21 +0100 Subject: [PATCH 1/9] Version selection: add AWS redirect configs for latest docs version --- .github/workflows/deploy-book.yml | 10 +---- .github/workflows/deploy-config.yml | 27 +++++++++++++ scripts/docs-versions.sh | 60 +++++++++++++++++++++++------ 3 files changed, 78 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/deploy-config.yml diff --git a/.github/workflows/deploy-book.yml b/.github/workflows/deploy-book.yml index f58b930c8..197a755bd 100644 --- a/.github/workflows/deploy-book.yml +++ b/.github/workflows/deploy-book.yml @@ -35,19 +35,13 @@ jobs: if [[ $GITHUB_REF =~ ^refs/heads/(master|[0-9][0-9.]+)$ ]]; then echo "::set-output name=branch::${BASH_REMATCH[1]}" fi - - name: Build versions file - if: github.event_name == 'push' && steps.branch.outputs.branch != null - run: scripts/docs-versions.sh origin | tee /dev/stderr > versions.json - name: Configure AWS Credentials - if: github.event_name == 'push' && steps.branch.outputs.branch != null + if: github.event_name == 'push' && steps.branch.outputs.branch != null && github.repository == 'crystal-lang/crystal-book' uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: us-east-1 - name: Deploy book - if: github.event_name == 'push' && steps.branch.outputs.branch != null + if: github.event_name == 'push' && steps.branch.outputs.branch != null && github.repository == 'crystal-lang/crystal-book' run: aws s3 sync ./site s3://crystal-book/reference/${{ steps.branch.outputs.branch }} --delete - - name: Deploy versions file - if: github.event_name == 'push' && steps.branch.outputs.branch != null - run: aws s3 sync . s3://crystal-book/reference --exclude '*' --include 'versions.json' diff --git a/.github/workflows/deploy-config.yml b/.github/workflows/deploy-config.yml new file mode 100644 index 000000000..ef74fc75f --- /dev/null +++ b/.github/workflows/deploy-config.yml @@ -0,0 +1,27 @@ +name: Deploy config +on: + push: + branches: + - '[0-9]+.[0-9]+' + +jobs: + build: + name: Deploy config + runs-on: ubuntu-latest + steps: + - name: Download source + uses: actions/checkout@v2 + - name: Build versions files + run: | + scripts/docs-versions.sh origin + grep '' versions.json .aws-config # Display the files + - name: Configure AWS Credentials + if: github.repository == 'crystal-lang/crystal-book' + uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + - name: Deploy versions files + if: github.repository == 'crystal-lang/crystal-book' + run: aws s3 sync . s3://crystal-book/reference --exclude '*' --include 'versions.json' --include '.aws-config' diff --git a/scripts/docs-versions.sh b/scripts/docs-versions.sh index 53e919110..208859601 100755 --- a/scripts/docs-versions.sh +++ b/scripts/docs-versions.sh @@ -1,12 +1,50 @@ -#!/usr/bin/env sh -set -e +#!/bin/bash +set -eu -printf '[' -latest='"latest"' -for ver in $(git ls-remote --heads "${1:-.}" | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r); do - printf '{"version": "%s", "title": "%s", "aliases": [%s]}, ' "$ver" "$ver" "$latest" - latest='' -done -printf '{"version": "master", "title": "nightly", "aliases": []}' -printf ']' -test -z "$latest" # Check that we wrote at least one version +branches=( $(git ls-remote --heads "${1:-.}" | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r) ) +latest_branch="${branches[0]}" + +{ + printf '[\n' + latest='"latest"' + for ver in "${branches[@]}"; do + printf '{"version": "%s", "title": "%s", "aliases": [%s]},\n' "$ver" "$ver" "$latest" + latest='' + done + printf '{"version": "master", "title": "nightly", "aliases": []}\n' + printf ']\n' + test -z "$latest" # Check that we wrote at least one version +} > versions.json + +cat < .aws-config +{ + "IndexDocument": { + "Suffix": "index.html" + }, + "RoutingRules": [ + { + "Condition": { + "KeyPrefixEquals": "reference/latest/" + }, + "Redirect": { + "HttpRedirectCode": "302", + "ReplaceKeyPrefixWith": "reference/${latest_branch}/", + "Protocol": "https", + "HostName": "crystal-lang.org" + } + }, + { + "Condition": { + "KeyPrefixEquals": "reference/", + "HttpErrorCodeReturnedEquals": "404" + }, + "Redirect": { + "HttpRedirectCode": "301", + "ReplaceKeyPrefixWith": "reference/latest/", + "Protocol": "https", + "HostName": "crystal-lang.org" + } + } + ] +} +EOF From bed4540ad4e57e997a5cff3c15b6893c2e621aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 10 Dec 2021 16:42:19 +0100 Subject: [PATCH 2/9] Use create and delete triggers --- .github/workflows/deploy-config.yml | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/.github/workflows/deploy-config.yml b/.github/workflows/deploy-config.yml index ef74fc75f..0c7dc83d0 100644 --- a/.github/workflows/deploy-config.yml +++ b/.github/workflows/deploy-config.yml @@ -1,8 +1,7 @@ name: Deploy config on: - push: - branches: - - '[0-9]+.[0-9]+' + - create + - delete jobs: build: From ae44785f3d674415d48bd0220a934413d9aef720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 10 Dec 2021 16:58:15 +0100 Subject: [PATCH 3/9] Untangle versions.json and website configuration --- .github/workflows/deploy-config.yml | 17 +++++-- scripts/aws-config.json | 77 +++++++++++++++++++++++++++++ scripts/docs-versions.sh | 53 ++++---------------- 3 files changed, 98 insertions(+), 49 deletions(-) create mode 100644 scripts/aws-config.json diff --git a/.github/workflows/deploy-config.yml b/.github/workflows/deploy-config.yml index 0c7dc83d0..b08ae42b0 100644 --- a/.github/workflows/deploy-config.yml +++ b/.github/workflows/deploy-config.yml @@ -7,13 +7,17 @@ jobs: build: name: Deploy config runs-on: ubuntu-latest + env: + S3_BUCKET: crystal-book steps: - name: Download source uses: actions/checkout@v2 - - name: Build versions files + - name: Build versions.json + run: scripts/docs-versions.sh origin | tee /dev/stderr > versions.json + - name: Build aws-config.json run: | - scripts/docs-versions.sh origin - grep '' versions.json .aws-config # Display the files + latest_version=$(git ls-remote --heads origin | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r | head -n1) + sed 's/\${LATEST_VERSION}/${latest_version}/g' scripts/aws-config.json | tee /dev/stderr > aws-config.json - name: Configure AWS Credentials if: github.repository == 'crystal-lang/crystal-book' uses: aws-actions/configure-aws-credentials@v1 @@ -21,6 +25,9 @@ jobs: aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} aws-region: us-east-1 - - name: Deploy versions files + - name: Deploy versions.json if: github.repository == 'crystal-lang/crystal-book' - run: aws s3 sync . s3://crystal-book/reference --exclude '*' --include 'versions.json' --include '.aws-config' + run: aws s3 cp versions.json "s3://${{ env.S3_BUCKET }}/reference/versions.json" + - name: Deploy website configuration + if: github.repository == 'crystal-lang/crystal-book' + run: aws s3api put-bucket-website --bucket "${{ env.S3_BUCKET }}" --website-configuration "file://aws-config.json" diff --git a/scripts/aws-config.json b/scripts/aws-config.json new file mode 100644 index 000000000..017c36c2e --- /dev/null +++ b/scripts/aws-config.json @@ -0,0 +1,77 @@ +{ + "IndexDocument": { + "Suffix": "index.html" + }, + "ErrorDocument": { + "Key": "reference/404.html" + }, + "RoutingRules": [ + { + "Condition": { + "KeyPrefixEquals": "reference/installation" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "301", + "Protocol": "https", + "ReplaceKeyPrefixWith": "install" + } + }, + { + "Condition": { + "KeyPrefixEquals": "reference/overview/hello_world.html" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "301", + "Protocol": "https", + "ReplaceKeyPrefixWith": "reference/getting_started" + } + }, + { + "Condition": { + "KeyPrefixEquals": "reference/overview" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "301", + "Protocol": "https", + "ReplaceKeyPrefixWith": "reference/getting_started" + } + }, + { + "Condition": { + "KeyPrefixEquals": "reference/${LATEST_VERSION}/${LATEST_VERSION}/" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "301", + "Protocol": "https", + "ReplaceKeyWith": "404" + } + }, + { + "Condition": { + "KeyPrefixEquals": "reference/latest" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "302", + "Protocol": "https", + "ReplaceKeyPrefixWith": "reference/${LATEST_VERSION}/" + } + }, + { + "Condition": { + "HttpErrorCodeReturnedEquals": "404", + "KeyPrefixEquals": "reference/" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "301", + "Protocol": "https", + "ReplaceKeyPrefixWith": "reference/${LATEST_VERSION}/" + } + } + ] +} diff --git a/scripts/docs-versions.sh b/scripts/docs-versions.sh index 208859601..98a63ae05 100755 --- a/scripts/docs-versions.sh +++ b/scripts/docs-versions.sh @@ -4,47 +4,12 @@ set -eu branches=( $(git ls-remote --heads "${1:-.}" | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r) ) latest_branch="${branches[0]}" -{ - printf '[\n' - latest='"latest"' - for ver in "${branches[@]}"; do - printf '{"version": "%s", "title": "%s", "aliases": [%s]},\n' "$ver" "$ver" "$latest" - latest='' - done - printf '{"version": "master", "title": "nightly", "aliases": []}\n' - printf ']\n' - test -z "$latest" # Check that we wrote at least one version -} > versions.json - -cat < .aws-config -{ - "IndexDocument": { - "Suffix": "index.html" - }, - "RoutingRules": [ - { - "Condition": { - "KeyPrefixEquals": "reference/latest/" - }, - "Redirect": { - "HttpRedirectCode": "302", - "ReplaceKeyPrefixWith": "reference/${latest_branch}/", - "Protocol": "https", - "HostName": "crystal-lang.org" - } - }, - { - "Condition": { - "KeyPrefixEquals": "reference/", - "HttpErrorCodeReturnedEquals": "404" - }, - "Redirect": { - "HttpRedirectCode": "301", - "ReplaceKeyPrefixWith": "reference/latest/", - "Protocol": "https", - "HostName": "crystal-lang.org" - } - } - ] -} -EOF +printf '[\n' +latest='"latest"' +for ver in "${branches[@]}"; do + printf '{"version": "%s", "title": "%s", "aliases": [%s]},\n' "$ver" "$ver" "$latest" + latest='' +done +printf '{"version": "master", "title": "nightly", "aliases": []}\n' +printf ']\n' +test -z "$latest" # Check that we wrote at least one version From 946d495e399133bf359ac1d0e55193c48645ab2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Fri, 10 Dec 2021 17:00:00 +0100 Subject: [PATCH 4/9] Revert unnecessary changes --- scripts/docs-versions.sh | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/scripts/docs-versions.sh b/scripts/docs-versions.sh index 98a63ae05..dbd95b0a5 100755 --- a/scripts/docs-versions.sh +++ b/scripts/docs-versions.sh @@ -1,8 +1,7 @@ -#!/bin/bash +#!/usr/bin/env sh set -eu branches=( $(git ls-remote --heads "${1:-.}" | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r) ) -latest_branch="${branches[0]}" printf '[\n' latest='"latest"' From 552ff45759f5456b51b737cb6121d258787c9b2c Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Fri, 10 Dec 2021 17:27:32 +0100 Subject: [PATCH 5/9] Reintroduce some refactors --- .github/workflows/deploy-config.yml | 14 +++++--------- scripts/docs-versions.sh | 25 +++++++++++++++---------- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/.github/workflows/deploy-config.yml b/.github/workflows/deploy-config.yml index b08ae42b0..f3bc25eca 100644 --- a/.github/workflows/deploy-config.yml +++ b/.github/workflows/deploy-config.yml @@ -7,17 +7,13 @@ jobs: build: name: Deploy config runs-on: ubuntu-latest - env: - S3_BUCKET: crystal-book steps: - name: Download source uses: actions/checkout@v2 - - name: Build versions.json - run: scripts/docs-versions.sh origin | tee /dev/stderr > versions.json - - name: Build aws-config.json + - name: Build versions files run: | - latest_version=$(git ls-remote --heads origin | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r | head -n1) - sed 's/\${LATEST_VERSION}/${latest_version}/g' scripts/aws-config.json | tee /dev/stderr > aws-config.json + scripts/docs-versions.sh origin + grep '' versions.json aws-config.json # Display the files - name: Configure AWS Credentials if: github.repository == 'crystal-lang/crystal-book' uses: aws-actions/configure-aws-credentials@v1 @@ -27,7 +23,7 @@ jobs: aws-region: us-east-1 - name: Deploy versions.json if: github.repository == 'crystal-lang/crystal-book' - run: aws s3 cp versions.json "s3://${{ env.S3_BUCKET }}/reference/versions.json" + run: aws s3 cp versions.json 's3://crystal-book/reference/versions.json' - name: Deploy website configuration if: github.repository == 'crystal-lang/crystal-book' - run: aws s3api put-bucket-website --bucket "${{ env.S3_BUCKET }}" --website-configuration "file://aws-config.json" + run: aws s3api put-bucket-website --bucket 'crystal-book' --website-configuration 'file://aws-config.json' diff --git a/scripts/docs-versions.sh b/scripts/docs-versions.sh index dbd95b0a5..599772bb7 100755 --- a/scripts/docs-versions.sh +++ b/scripts/docs-versions.sh @@ -1,14 +1,19 @@ -#!/usr/bin/env sh +#!/bin/bash set -eu branches=( $(git ls-remote --heads "${1:-.}" | grep -P -o '(?<=refs/heads/)[0-9][0-9.]+' | sort -V -r) ) +latest_version="${branches[0]}" -printf '[\n' -latest='"latest"' -for ver in "${branches[@]}"; do - printf '{"version": "%s", "title": "%s", "aliases": [%s]},\n' "$ver" "$ver" "$latest" - latest='' -done -printf '{"version": "master", "title": "nightly", "aliases": []}\n' -printf ']\n' -test -z "$latest" # Check that we wrote at least one version +{ + printf '[\n' + latest='"latest"' + for ver in "${branches[@]}"; do + printf '{"version": "%s", "title": "%s", "aliases": [%s]},\n' "$ver" "$ver" "$latest" + latest='' + done + printf '{"version": "master", "title": "nightly", "aliases": []}\n' + printf ']\n' + test -z "$latest" # Check that we wrote at least one version +} > versions.json + +sed 's/\${LATEST_VERSION}/'"${latest_version}"'/g' "$(dirname "$0")/aws-config.json" > aws-config.json From 20114eb9a8faad95e31212c6db05f7bae8d818fc Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 15 Dec 2021 00:57:01 +0100 Subject: [PATCH 6/9] Change to a better-targeted 404 redirect MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matías García Isaía --- scripts/aws-config.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/aws-config.json b/scripts/aws-config.json index 017c36c2e..0e2bb8efc 100644 --- a/scripts/aws-config.json +++ b/scripts/aws-config.json @@ -41,13 +41,13 @@ }, { "Condition": { - "KeyPrefixEquals": "reference/${LATEST_VERSION}/${LATEST_VERSION}/" + "KeyPrefixEquals": "reference/latest/${LATEST_VERSION}/" }, "Redirect": { "HostName": "crystal-lang.org", - "HttpRedirectCode": "301", + "HttpRedirectCode": "302", "Protocol": "https", - "ReplaceKeyWith": "404" + "ReplaceKeyWith": "reference/${LATEST_VERSION}/404.html" } }, { From b3530134094e2f88167509f6629e6dcb2aa5c500 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 15 Dec 2021 00:59:30 +0100 Subject: [PATCH 7/9] Make the config similar to api/ config, drop obsolete redirects --- scripts/aws-config.json | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/scripts/aws-config.json b/scripts/aws-config.json index 0e2bb8efc..5cf3b7200 100644 --- a/scripts/aws-config.json +++ b/scripts/aws-config.json @@ -2,9 +2,6 @@ "IndexDocument": { "Suffix": "index.html" }, - "ErrorDocument": { - "Key": "reference/404.html" - }, "RoutingRules": [ { "Condition": { @@ -17,28 +14,6 @@ "ReplaceKeyPrefixWith": "install" } }, - { - "Condition": { - "KeyPrefixEquals": "reference/overview/hello_world.html" - }, - "Redirect": { - "HostName": "crystal-lang.org", - "HttpRedirectCode": "301", - "Protocol": "https", - "ReplaceKeyPrefixWith": "reference/getting_started" - } - }, - { - "Condition": { - "KeyPrefixEquals": "reference/overview" - }, - "Redirect": { - "HostName": "crystal-lang.org", - "HttpRedirectCode": "301", - "Protocol": "https", - "ReplaceKeyPrefixWith": "reference/getting_started" - } - }, { "Condition": { "KeyPrefixEquals": "reference/latest/${LATEST_VERSION}/" @@ -52,7 +27,7 @@ }, { "Condition": { - "KeyPrefixEquals": "reference/latest" + "KeyPrefixEquals": "reference/latest/" }, "Redirect": { "HostName": "crystal-lang.org", @@ -70,7 +45,7 @@ "HostName": "crystal-lang.org", "HttpRedirectCode": "301", "Protocol": "https", - "ReplaceKeyPrefixWith": "reference/${LATEST_VERSION}/" + "ReplaceKeyPrefixWith": "reference/latest/" } } ] From 12dd02b05441bd9884a32ca86a315c7442499c76 Mon Sep 17 00:00:00 2001 From: Oleh Prypin Date: Wed, 15 Dec 2021 01:02:52 +0100 Subject: [PATCH 8/9] Add ability to run workflow manually --- .github/workflows/deploy-config.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/deploy-config.yml b/.github/workflows/deploy-config.yml index f3bc25eca..6d6e9be8b 100644 --- a/.github/workflows/deploy-config.yml +++ b/.github/workflows/deploy-config.yml @@ -2,6 +2,7 @@ name: Deploy config on: - create - delete + - workflow_dispatch jobs: build: From 9dd1cd3915e43e7c000cec9839ec1e967c41fa75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Wed, 6 Apr 2022 11:10:54 +0200 Subject: [PATCH 9/9] Remove delete trigger Release branches are usually not deleted, so we can ignore that. In the odd case, the workflow can be triggered manually. --- .github/workflows/deploy-config.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/deploy-config.yml b/.github/workflows/deploy-config.yml index 6d6e9be8b..e2dadc666 100644 --- a/.github/workflows/deploy-config.yml +++ b/.github/workflows/deploy-config.yml @@ -1,7 +1,6 @@ name: Deploy config on: - create - - delete - workflow_dispatch jobs: