diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index 33c095a7..bd60074e 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -22,7 +22,6 @@ jobs: - uses: webfactory/ssh-agent@v0.5.4 with: ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} - - run: apt update && apt install -y python3 virtualenv - run: hypernode-deploy build -vvv - name: archive production artifacts uses: actions/upload-artifact@v3 diff --git a/bin/generate_nginx_redirects b/bin/generate_nginx_redirects new file mode 100755 index 00000000..afe9733e --- /dev/null +++ b/bin/generate_nginx_redirects @@ -0,0 +1,5 @@ +#!/usr/bin/env python3 +from hypernode.redirect.generate_nginx_redirects import main + +if __name__ == "__main__": + main() diff --git a/deploy.php b/deploy.php index 1d657186..5d04ac59 100644 --- a/deploy.php +++ b/deploy.php @@ -25,6 +25,7 @@ task('python:venv:create', static function () { run('mkdir -p .hypernode'); run('virtualenv -p python3 .venv'); + run('echo export PYTHONPATH=$(pwd) >> .venv/bin/activate'); }); # Install the requirements @@ -32,6 +33,11 @@ run('source .venv/bin/activate && pip install -r requirements/base.txt'); }); +task('python:generate_redirects', static function () { + run('mkdir -p etc/nginx'); + run('source .venv/bin/activate && bin/generate_nginx_redirects > etc/nginx/server.redirects.conf'); +}); + # Build the documentation task('python:build_documentation', static function () { run('source .venv/bin/activate && bin/build_docs'); @@ -49,17 +55,28 @@ } }); -task("deploy:docs_vhost", static function () { - run("hypernode-manage-vhosts --https --force-https {{hostname}} --no --webroot {{current_path}}/{{public_folder}}"); +task('deploy:docs_vhost:acceptance', static function () { + run('hypernode-manage-vhosts --https --force-https {{hostname}} --no --webroot {{current_path}}/{{public_folder}}'); +})->select('stage=acceptance'); + +task('deploy:docs_vhost:production', static function () { + run('hypernode-manage-vhosts --https --force-https docs.hypernode.io --no --webroot {{current_path}}/{{public_folder}}'); +})->select('stage=production'); + +task('deploy:nginx_redirects', static function () { + run('cp {{release_path}}/etc/nginx/server.redirects.conf /data/web/nginx/server.redirects.conf'); }); $configuration = new Configuration(); $configuration->addBuildTask('python:venv:create'); $configuration->addBuildTask('python:venv:requirements'); $configuration->addBuildTask('python:build_documentation'); +$configuration->addBuildTask('python:generate_redirects'); $configuration->addDeployTask('deploy:disable_public'); $configuration->addDeployTask('deploy:hmv_docker'); -$configuration->addDeployTask('deploy:docs_vhost'); +$configuration->addDeployTask('deploy:docs_vhost:acceptance'); +$configuration->addDeployTask('deploy:docs_vhost:production'); +$configuration->addDeployTask('deploy:nginx_redirects'); # Just some sane defaults to exclude from the deploy $configuration->setDeployExclude([ @@ -73,12 +90,11 @@ '.idea', '.gitignore', '.editorconfig', - 'etc/', - '.venv/', - 'bin/', - 'hypernode/', - 'requirements/', - 'tests/' + './.venv', + './bin', + './hypernode', + './requirements', + './tests', ]); $productionStage = $configuration->addStage('production', 'docs.hypernode.io'); diff --git a/hypernode/redirect/generate_nginx_redirects.py b/hypernode/redirect/generate_nginx_redirects.py new file mode 100644 index 00000000..1dd4d06d --- /dev/null +++ b/hypernode/redirect/generate_nginx_redirects.py @@ -0,0 +1,31 @@ +import os.path +from pathlib import Path +from typing import List + +from frontmatter import Frontmatter + +from hypernode.common.docs import get_all_docs +from hypernode.common.settings import DOCS_DIR + + +def get_redirects_from_doc(doc: Path) -> List[str]: + fm = Frontmatter.read_file(doc) + attributes = fm["attributes"] or {} + return attributes.get("redirect_from", []) + + +def get_path_for_doc(doc: Path) -> str: + relative_path = Path(os.path.relpath(doc, DOCS_DIR)) + path = "/{}/{}".format( + relative_path.parent, relative_path.name.replace(".md", ".html") + ) + path = path.replace("/./", "/") + path = path.replace("/index.html", "/") + return path + + +def main(): + for doc in get_all_docs(): + for redirect in get_redirects_from_doc(doc): + doc_path = get_path_for_doc(doc) + print("rewrite ^{}$ {} permanent;".format(redirect, doc_path))