diff --git a/.github/workflows/deploy-book.yml b/.github/workflows/deploy-book.yml index 5ead89151..f016f8806 100644 --- a/.github/workflows/deploy-book.yml +++ b/.github/workflows/deploy-book.yml @@ -44,19 +44,13 @@ jobs: run: crystal --version | tee crystal-version.txt - name: Build book run: LINT=true make build - - 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..e2dadc666 --- /dev/null +++ b/.github/workflows/deploy-config.yml @@ -0,0 +1,29 @@ +name: Deploy config +on: + - create + - workflow_dispatch + +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.json # 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.json + if: github.repository == 'crystal-lang/crystal-book' + 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 'crystal-book' --website-configuration 'file://aws-config.json' diff --git a/scripts/aws-config.json b/scripts/aws-config.json new file mode 100644 index 000000000..5cf3b7200 --- /dev/null +++ b/scripts/aws-config.json @@ -0,0 +1,52 @@ +{ + "IndexDocument": { + "Suffix": "index.html" + }, + "RoutingRules": [ + { + "Condition": { + "KeyPrefixEquals": "reference/installation" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "301", + "Protocol": "https", + "ReplaceKeyPrefixWith": "install" + } + }, + { + "Condition": { + "KeyPrefixEquals": "reference/latest/${LATEST_VERSION}/" + }, + "Redirect": { + "HostName": "crystal-lang.org", + "HttpRedirectCode": "302", + "Protocol": "https", + "ReplaceKeyWith": "reference/${LATEST_VERSION}/404.html" + } + }, + { + "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/" + } + } + ] +} diff --git a/scripts/docs-versions.sh b/scripts/docs-versions.sh index b8f9cf76b..0c59fc7a9 100755 --- a/scripts/docs-versions.sh +++ b/scripts/docs-versions.sh @@ -1,12 +1,19 @@ -#!/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/release/)[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/release/)[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 +} > versions.json + +sed 's/\${LATEST_VERSION}/'"${latest_version}"'/g' "$(dirname "$0")/aws-config.json" > aws-config.json