diff --git a/.github/workflows/website-publish.yml b/.github/workflows/website-publish.yml index 94df22d..93731cb 100644 --- a/.github/workflows/website-publish.yml +++ b/.github/workflows/website-publish.yml @@ -1,38 +1,78 @@ -name: Website publish +name: Website containerize on: push: branches: - master +# copied everything below this line from https://docs.github.com/en/actions/publishing-packages/publishing-docker-images +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} +# There is a single job in this workflow. It's configured to run on the latest available version of Ubuntu. jobs: - build: + build-and-push-image: runs-on: ubuntu-latest + # Sets the permissions granted to the `GITHUB_TOKEN` for the actions in this job. + permissions: + contents: read + packages: write + # steps: - - name: Code Checkout - uses: actions/checkout@v2 + - name: Checkout repository + uses: actions/checkout@v3 + # Uses the `docker/login-action` action to log in to the Container registry registry using the account and password that will publish the packages. Once published, the packages are scoped to the account defined here. + - name: Log in to the Container registry + uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 with: - submodules: true - fetch-depth: 0 - - name: Hugo Setup - uses: peaceiris/actions-hugo@v2 + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + # This step uses [docker/metadata-action](https://github.com/docker/metadata-action#about) to extract tags and labels that will be applied to the specified image. The `id` "meta" allows the output of this step to be referenced in a subsequent step. The `images` value provides the base name for the tags and labels. + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 with: - hugo-version: '0.91.2' - - name: Build - run: hugo --minify - - name: Clean - uses: appleboy/ssh-action@master + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + # This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages. + # It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository. + # It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step. + - name: Build and push Docker image + uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 with: - host: pagwin.xyz - username: website - key: ${{secrets.SSH_KEY}} - script: rm -rf /var/www/pagwin.xyz/* - - name: Deploy - uses: up9cloud/action-rsync@master - env: - HOST: pagwin.xyz - USER: website - KEY: ${{secrets.SSH_KEY}} - SOURCE: ./public/* - TARGET: /var/www/pagwin.xyz/ - + context: . + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + #jobs: + # build: + # runs-on: ubuntu-latest + # steps: + ## old steps when I was deploying straight to VPS + # - name: Code Checkout + # uses: actions/checkout@v2 + # with: + # submodules: true + # fetch-depth: 0 + # - name: Hugo Setup + # uses: peaceiris/actions-hugo@v2 + # with: + # hugo-version: '0.91.2' + # - name: Build + # run: hugo --minify + # - name: Clean + # uses: appleboy/ssh-action@master + # with: + # host: pagwin.xyz + # username: website + # key: ${{secrets.SSH_KEY}} + # script: rm -rf /var/www/pagwin.xyz/* + # - name: Deploy + # uses: up9cloud/action-rsync@master + # env: + # HOST: pagwin.xyz + # USER: website + # KEY: ${{secrets.SSH_KEY}} + # SOURCE: ./public/* + # TARGET: /var/www/pagwin.xyz/ + # diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..62a86cf --- /dev/null +++ b/Dockerfile @@ -0,0 +1,24 @@ +FROM alpine:3.18 as build +# making my own image for hugo because there doesn't seem to be an official one in repo, lazy so we're going with alpine +RUN ["apk", "update"] +RUN ["apk", "add", "hugo"] +ADD . /root/ +WORKDIR /root +# pass in the uri or ip of the comments api so nginx can forward to it +ARG COMMENTS_BACKEND +ENV COMMENTS_BACKEND=${WITH_BACKEND} +RUN ["hugo", "--minify"] + +FROM python:3.11-alpine as fiddling +RUN ["mkdir", "/application"] +RUN ["adduser", "-D", "-h", "/application", "application"] +# might need to chown these +ADD ./scripts/template_convert.py /application/template_convert.py +ADD ./nginx.conf.template /application/nginx.conf.template +WORKDIR /application +USER application +RUN ["python", "/application/template_convert.py"] + +FROM nginx:1.25-alpine +COPY --from=fiddling /application/nginx.conf /etc/nginx/nginx.conf +COPY --from=build /root/public /usr/share/nginx/html diff --git a/config.toml b/config.toml index 8f367e6..1a61063 100644 --- a/config.toml +++ b/config.toml @@ -29,3 +29,8 @@ theme = "liquorice" [markup.goldmark] [markup.goldmark.renderer] unsafe = true + +[security] + [security.funcs] + # first 2 vars are things hugo has built in and I'm keeping to avoid breakage + getenv = ['^HUGO_', '^CI$', "COMMENTS_BACKEND"] diff --git a/content/blog/blog_v2.md b/content/blog/blog_v2.md new file mode 100644 index 0000000..cdb27ad --- /dev/null +++ b/content/blog/blog_v2.md @@ -0,0 +1,43 @@ +--- + +title: "Who needs a CMS anyways? (and also redeploying to docker)" + +description: "" + +date: 2023-09-08 + +draft: true + +--- + +# What's up? + +We're adding comments to this blog and setting it up to deploy via docker. + +## Why? + +Because I wanted to add comments. The way I'm planning on doing that will mean that this blog will be more complex. Specifically in addition to generating static files and having nginx serve them I'll need to have a separate service. That separate service (and the database it depends on) will be contained within a docker container for convenience. But with parts of the site being deployed via docker I might as well deploy all of them via docker. + +### Sounds like you've written a CMS so why is the static file hosting not a part of that? + +Because I'm not writing a CMS, I'm just writing the backend for comments and sticking some htmx stuff into the hugo template for a blog for the frontend. + +## [Writing the Go backend] + +### Why Go? + +### Annoyances +- Doesn't handle html multipart forms correctly(or the browser is wrong but I doubt that) +- Magic redirect when there's no trailing / + +### Anyways + +- yeeted oauth midway through + +## [Writing the Hugo templating frontend] + +## [Setting up the docker Containers] + +## [Changing github actions and deployment pipeline to rebuild the docker container] + +## [Conclusion] diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 7f24c24..a315a60 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -37,6 +37,7 @@ {{ partial "author.html" .}} + {{ partial "comments.html" . }}
diff --git a/layouts/partials/comments.html b/layouts/partials/comments.html new file mode 100644 index 0000000..64b0d6a --- /dev/null +++ b/layouts/partials/comments.html @@ -0,0 +1,6 @@ + + + {{ cond (not (eq (os.Getenv "COMMENTS_BACKEND") "")) (safeHTML (printf "
" .File.Path)) ""}} + diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 6202049..b1a80a0 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -9,6 +9,8 @@ {{ cond (eq .Page.Kind "page") (safeHTML (printf "" .Site.BaseURL)) "" }} + + diff --git a/nginx.conf.template b/nginx.conf.template new file mode 100644 index 0000000..c153a10 --- /dev/null +++ b/nginx.conf.template @@ -0,0 +1,29 @@ +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type application/octet-stream; + + sendfile on; + keepalive_timeout 65; + + server { + listen 80; + server_name pagwin.xyz; + location / { + root /usr/share/nginx/html; + index index.html index.htm; + } + # weird comments around this block is so we can easily yeet it if the comments backend isn't present + ###{--- + location /comments { + # we're going to use a python program to substitute this out + proxy_pass http://${{COMMENTS_BACKEND}} + } + ###---} + } +} diff --git a/scripts/template_convert.py b/scripts/template_convert.py new file mode 100755 index 0000000..0015084 --- /dev/null +++ b/scripts/template_convert.py @@ -0,0 +1,20 @@ +#!/bin/env python3 + +import os +import re + +var_name = "COMMENTS_BACKEND" +file_name = "nginx.conf" +back = os.environ.get(var_name) +buf = str() +with open(file_name+".template") as file: + buf = file.read() + +if back == None: + buf = re.sub("###{---(.|\n)*###---}", "", buf) +else: + # ${{\s*$var_name\s*}} + buf = re.sub(rf"\${{{{\s*{var_name}\s*}}}}",var_name,buf) + +with open(file_name, "w+") as file: + file.write(buf)