Skip to content

Commit

Permalink
Version selection: add AWS redirect configs for latest docs version
Browse files Browse the repository at this point in the history
  • Loading branch information
oprypin committed Dec 10, 2021
1 parent e32e793 commit c52480b
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 19 deletions.
10 changes: 2 additions & 8 deletions .github/workflows/deploy-book.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
27 changes: 27 additions & 0 deletions .github/workflows/deploy-config.yml
Original file line number Diff line number Diff line change
@@ -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'
60 changes: 49 additions & 11 deletions scripts/docs-versions.sh
Original file line number Diff line number Diff line change
@@ -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 <<EOF > .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

0 comments on commit c52480b

Please sign in to comment.