Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version selection: add AWS redirect configs for latest docs version #561

Merged
merged 10 commits into from
Apr 6, 2022
10 changes: 2 additions & 8 deletions .github/workflows/deploy-book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
29 changes: 29 additions & 0 deletions .github/workflows/deploy-config.yml
Original file line number Diff line number Diff line change
@@ -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'
52 changes: 52 additions & 0 deletions scripts/aws-config.json
Original file line number Diff line number Diff line change
@@ -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/"
}
}
]
}
29 changes: 18 additions & 11 deletions scripts/docs-versions.sh
Original file line number Diff line number Diff line change
@@ -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