0

Build on git push

 1 year ago
source link: https://willschenk.com/howto/2023/build_on_git_push/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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.

  ssh [email protected]

  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:

  ssh [email protected]

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

Adding a post-receive hook

  groupmod -a -U git docker

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

  #!/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

  chmod +x post-receive

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

  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.

  git clone https://github.com/lkhrs/eleventy-simple
  cd eleventy-simple
  git remote add production [email protected]:/apps/apple.willschenk.com

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

  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:

  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:

    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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK