From 580557779ddc85e9f9b882b045da1ded0c2200cc Mon Sep 17 00:00:00 2001 From: Pagwin Date: Wed, 30 Aug 2023 20:16:47 -0400 Subject: [PATCH 1/6] blog v2 outline done --- content/blog/blog_v2.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 content/blog/blog_v2.md diff --git a/content/blog/blog_v2.md b/content/blog/blog_v2.md new file mode 100644 index 0000000..cae377a --- /dev/null +++ b/content/blog/blog_v2.md @@ -0,0 +1,33 @@ +--- + +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] + +## [Writing the Hugo templating frontend] + +## [Setting up the docker Containers] + +## [Changing github actions and deployment pipeline to rebuild the docker container] + +## [Conclusion] From 92186c3a58226b9bc9f27cd9256823b2dc575819 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Mon, 11 Sep 2023 16:22:44 -0400 Subject: [PATCH 2/6] Beginning work on sticking website into container saved all the relevent docs into a window, can't continue work now because I need to rest before I go back to driving, need to setup a conditional so I can do a docker build without having the comments section enabled though maybe have the arg be the name nginx should hit up for the comments instead of 0 v 1 for on v off --- Dockerfile | 12 ++++++++++++ content/blog/blog_v2.md | 12 +++++++++++- layouts/partials/head.html | 1 + 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2ed1ce6 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +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 +ARG WITH_COMMENTS=0 +RUN ["hugo", "--minify"] + +FROM nginx:1.25 + +RUN diff --git a/content/blog/blog_v2.md b/content/blog/blog_v2.md index cae377a..cdb27ad 100644 --- a/content/blog/blog_v2.md +++ b/content/blog/blog_v2.md @@ -21,9 +21,19 @@ Because I wanted to add comments. The way I'm planning on doing that will mean t ### 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] diff --git a/layouts/partials/head.html b/layouts/partials/head.html index 6202049..d8aa0b8 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -9,6 +9,7 @@ {{ cond (eq .Page.Kind "page") (safeHTML (printf "" .Site.BaseURL)) "" }} + From 5deea869e4861fbac260ad00401fc8706f7d08cb Mon Sep 17 00:00:00 2001 From: Pagwin Date: Tue, 12 Sep 2023 14:55:06 -0400 Subject: [PATCH 3/6] some work done need to go to class now --- Dockerfile | 15 +++++++++++++-- nginx.conf.template | 27 +++++++++++++++++++++++++++ scripts/template_convert.py | 0 3 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 nginx.conf.template create mode 100644 scripts/template_convert.py diff --git a/Dockerfile b/Dockerfile index 2ed1ce6..9411887 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,9 +4,20 @@ RUN ["apk", "update"] RUN ["apk", "add hugo"] ADD . /root/ WORKDIR /root -ARG WITH_COMMENTS=0 +# 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 nginx:1.25 +FROM python:3.11-alpine as fiddling +RUN ["adduser", "-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 +COPY --from=fiddling /application/nginx.conf RUN diff --git a/nginx.conf.template b/nginx.conf.template new file mode 100644 index 0000000..7572587 --- /dev/null +++ b/nginx.conf.template @@ -0,0 +1,27 @@ +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 100644 index 0000000..e69de29 From 4aadacac1e58468a0288c18d906ce6272b6eb332 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Wed, 13 Sep 2023 16:41:26 -0400 Subject: [PATCH 4/6] successful containerization just need to add the UI for comments now --- Dockerfile | 11 ++++++----- nginx.conf.template | 2 ++ scripts/template_convert.py | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) mode change 100644 => 100755 scripts/template_convert.py diff --git a/Dockerfile b/Dockerfile index 9411887..62a86cf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ 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"] +RUN ["apk", "add", "hugo"] ADD . /root/ WORKDIR /root # pass in the uri or ip of the comments api so nginx can forward to it @@ -10,7 +10,8 @@ ENV COMMENTS_BACKEND=${WITH_BACKEND} RUN ["hugo", "--minify"] FROM python:3.11-alpine as fiddling -RUN ["adduser", "-h", "/application", "application"] +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 @@ -18,6 +19,6 @@ WORKDIR /application USER application RUN ["python", "/application/template_convert.py"] -FROM nginx:1.25 -COPY --from=fiddling /application/nginx.conf -RUN +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/nginx.conf.template b/nginx.conf.template index 7572587..c153a10 100644 --- a/nginx.conf.template +++ b/nginx.conf.template @@ -24,4 +24,6 @@ http { # 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 old mode 100644 new mode 100755 index e69de29..0015084 --- a/scripts/template_convert.py +++ 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) From 149ce0df220cfb00f9a47bc3a90258ead6a70189 Mon Sep 17 00:00:00 2001 From: Pagwin Date: Wed, 13 Sep 2023 17:06:00 -0400 Subject: [PATCH 5/6] I think I'm almost at a functional ui if I do the docker compose now but it's annoying --- config.toml | 5 +++++ layouts/_default/single.html | 1 + layouts/partials/comments.html | 6 ++++++ layouts/partials/head.html | 1 + 4 files changed, 13 insertions(+) create mode 100644 layouts/partials/comments.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/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 d8aa0b8..b1a80a0 100644 --- a/layouts/partials/head.html +++ b/layouts/partials/head.html @@ -10,6 +10,7 @@ {{ cond (eq .Page.Kind "page") (safeHTML (printf "" .Site.BaseURL)) "" }} + From d99259b7835f94bad6866a6f3389596abd887ecf Mon Sep 17 00:00:00 2001 From: Pagwin Date: Thu, 14 Sep 2023 14:43:46 -0400 Subject: [PATCH 6/6] Changed from publishing directly to building the container previously did rsync to the server now building a docker container that I can docker-compose --- .github/workflows/website-publish.yml | 92 +++++++++++++++++++-------- 1 file changed, 66 insertions(+), 26 deletions(-) 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/ + #