I miss the days of git push heroku. How hard would it be to do this ourselves?

I did find an old project called git-deploy which integrates very nicely with the git command, but has a bunch of ruby and rails specific stuff inside of the build script itself. I love ruby, but I'm in more of a docker based life-style right now. So i took the opportinity to time travel back into blogs from the early '10s to pick apart this information for you!

The Basic Plan

plan.png

Create the app user

First we are going to setup git on the server. We'll create an app user that will do the deployment and host the repositories.

Connect to the server and create the user with the docker group so it can do builds. We'll copy of the .ssh keys so that we can log in without a password.

Finally, lets create a place for the apps to live.

1
2
3
4
5
6
7
8
9
  ssh root@apple.willschenk.com

  adduser app -g docker
  adduser app sudo
  cp -r ~root/.ssh ~app
  chown -R app:app ~app/.ssh

  mkdir /apps
  chown app:app /apps

Create an empty repo and push to it

On the server:

1
2
3
4
5
6
  ssh app@apple.willschenk.com

  mkdir -p /apps/apple.willschenk.com
  cd /apps/apple.willschenk.com
  git init --bare
  git config core.sharedRepository group

Adding a post-receive hook

1
  groupmod -a -U git docker

Then in /apps/summarize.willschenk.com/hooks/post-receive:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
  #!/bin/bash

  read oldrev newrev refname
  echo newrev is $newrev

  BASENAME=$(basename $PWD)
  echo Building $BASENAME

  WORKDIR=`mktemp -d`
  echo Checking out $newrev to $WORKDIR

  git --work-tree=$WORKDIR checkout -f $newrev

  cd $WORKDIR
  echo Running build

  docker build . \
         -t registry.willschenk.com/${BASENAME} \
         -t registry.willschenk.com/${BASENAME}:$newrev \
         --push

Then make sure it's executable

1
  chmod +x post-receive

And also make sure that you've logged into your registry!

1
  docker login registry.willschenk.com -u registry-user -p password

Test it out

Back on your laptop:

Lets pull down a repo, give it a Dockerfile, and push it to our server to see if it builds.

1
2
3
  git clone https://github.com/lkhrs/eleventy-simple
  cd eleventy-simple
  git remote add production app@apple.willschenk.com:/apps/apple.willschenk.com

Lets create a Dockerfile that we'll use to build this up:

1
2
3
4
5
6
7
8
9
  FROM node:18 as build
  WORKDIR /app
  COPY package* .
  RUN npm install
  COPY . .
  RUN npx @11ty/eleventy

  FROM jitesoft/lighttpd
  COPY --from=build /app/_site/ /var/www/html/

Now push:

1
2
3
  git add .
  git commit -m "Adding dockerfile"
  git push production main

And you should see it build and push to the repository in your git client!

Manual deploy

On the server you can test this with:

1
2
3
4
5
6
7
8
    docker pull registry.willschenk.com/apple.willschenk.com:latest
    docker run \
           --detach \
           --name apple \
           --network caddy \
           --label caddy=apple.willschenk.com \
           --label caddy.reverse_proxy='{{upstreams 80}}' \
           registry.willschenk.com/apple.willschenk.com:latest

So it works, it builds and is in the registry. This isn't a generalized solution but its a good start.

Previously

XXIIVV — discourse

2023-06-26

Next

Static Mastodon toots in Hugo

2023-06-27

howto

Previously

Using caddy docker proxy straight from the dockerfiles

2023-06-22

Next

Emacs Blogging mode take 2 emacs and hugo sitting in a tree

2023-07-02