This document provides a guide for setting up an automated pipe that tests an app, builds a docker container around it, pushes that container to a registry and then sshes in and updates the running application via docker-compose
.
Configure Server
Install Docker
See this guide for more instructions:
Create User to Run Service
Create the user whilst ensuring that we create their home dir (-m
) and that they have permission to use docker services (-G docker
):
As that user… Create docker-compose.yml
- Typically best to version control this file
- Ensure that any secrets like environment variables are in a separate
.env
file and that file is.gitignore
ed to prevent sensitive info from being stored.
Create update.sh
file
- Again we can version control this file
Make sure that it is executable:
Authenticate Docker Repos
If using normal docker registry:
Otherwise see Log in to Docker Repos for authenticating against google docker repo:
Create CI file in Github
Let’s create a new file in the project: .github/workflows/test_build_deploy.yaml
:
Initial Step and Tests
You probably want to put the deploy step behind some automated tests so that it doesn’t deploy if they fail.
We also set some env vars
Option A - Build and Push to GCR
Build a docker container and push to Google Container Registry.
Option B - Build and Push to a Vanilla Docker Registry
See:
- build-push-action for options
- docker/login-action for auth options for different types of registries
Deploy Step
Use SSH to log into the prod machine and have it pull the newly created docker image(s):
Prepare Git Secrets
SSH
- Add the IP address of the host machine under
DEPLOY_SSH_HOST
- Add the username of the user who will run the script under
DEPLOY_SSH_USERNAME
Generate the SSH key for the user:
Copy ssh public key to remote machine
Copy the contents of the private key and add to repo under DEPLOY_SSH_KEY
Docker Secrets for Google GCR
If using Google, generate a service account with Artifact Registry Writer
permission (see this list for more info). Add a JSON key to the service account and download it. Then base64 encode it:
Copy the output and add it to the repo as a secret called GCLOUD_KEY
Docker secrets for non-GCR Repo
Depending on which registry you are using (See docker/login-action) add:
DOCKERHUB_USERNAME
- to push to dockerhub registry withDOCKERHUB_TOKEN
- application token to push to registry with
Checklist
Server Initial Config
- Create server VM
- Install docker
- Create service user (and grant docker permissions)
- Create project folder
- Create docker-compose.yaml
- Create update.sh
- Authenticate against docker repo
CI File
- Add CI file
- Add testing step
- Add build step (either using google or docker registry)
- Add deploy step
Github Secrets
- Add Env Vars:
-
DEPLOY_SSH_HOST
-
DEPLOY_SSH_USERNAME
-
DEPLOY_SSH_KEY
- Docker Auth:
-
DOCKERHUB_USERNAME
-
DOCKERHUB_TOKEN
-
- or
-
GCLOUD_KEY
-
-
Final Checks
- update.sh executable
- Path to update.sh on server is correct in ci file
- container repo name is correct in CI file and docker-compose file.
- all env vars are set up in git
- server is authenticated and can pull docker images from repo