GitHub Action
build-docker-and-deploy-to-caprover
This is a GitHub Action which builds a Docker image and deploys it to a CapRover
application. CapRover
is a free and open source PaaS (platform as a service) that you can run on your own cloud server. I use it on a DigitalOcean droplet and it works great for hosting multiple sites cheaply. Step by step instructions for how to setup CapRover
for Django specifically are at https://alldjango.com/articles/serve-multiple-django-sites-from-one-cloud-server, but can be modified for other web frameworks.
This is optional, but it is best practice so that secrets are not in plain text.
- Go to https://github.com/USERNAME/REPOSITORY_NAME/settings/secrets/actions
- Click New repository secret and type "CAPROVER_APP_TOKEN" into Name and the generated app token from
CapRover
into Secret; click Add secret - Click New repository secret and type "CAPROVER_SERVER_URL" into Name and the URL for your
CapRover
server into Secret; click Add secret
- Go to https://github.com/USERNAME/REPOSITORY_NAME/actions
- If it is available, click New workflow
- Click set up a workflow for yourself
- Name the workflow "deploy-to-caprover.yml" or something similar
- Copy the following into the YAML file and make sure to update
YOUR-APP-NAME
:
on:
push:
branches: ["main"]
workflow_dispatch:
jobs:
deploy-to-caprover:
name: Deploy to CapRover
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: adamghill/build-docker-and-deploy-to-caprover@main
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
caprover-app-name: YOUR-APP-NAME
caprover-server-url: ${{ secrets.CAPROVER_SERVER_URL }}
caprover-app-token: ${{ secrets.CAPROVER_APP_TOKEN }}
- Click Commit changes...
- Click the Actions sub-navigation to see the workflow run
These are all of the inputs that are available for the build-docker-and-deploy-to-caprover
GitHub Action.
The GitHub personal access token. Required. Set by the GitHub action automatically.
The name of the application in CapRover. Required.
The CapRover server URL. Required.
The CapRover app token. Required.
The name of the docker image to build. Optional. Defaults to "{github.repository}-{inputs.caprover-app-name}".
To ensure the default image name is valid, the values of github.repository
and inputs.caprover-app-name
are slugified using the following shell command:
echo value | sed -e 's/[^[:alnum:]]/-/g' | tr -s '-' | tr A-Z a-z
sed -e 's/[^[:alnum:]]/-/g'
: This replaces any character that is not a letter or a number with a dash.tr -s '-'
: This replaces multiple consecutive dashes with a single dash.tr A-Z a-z
: This converts all uppercase letters to lowercase.
The name of the branch to build and deploy. Optional. Defaults to the current branch.
The name of the Docker file to build. Optional. Defaults to "./Dockerfile".
The name of the registry. Optional. Defaults to "ghcr.io".
If you encounter an error suggesting that the runner is low on resources while building the docker image, you can add the step below to free up space on the host machine:
- name: Delete unnecessary large tools folder
run: rm -rf /opt/hostedtoolcache
If you are using this action on a private repository, the image/package built will automatically be private as well. As a result, you may see an unauthorized
error message when CapRover
tries to pull the image.
To resolve this issue, you must configure your CapRover
instance to access the GitHub registry with the appropriate credentials. Navigate to the Cluster section in the left sidebar in your CapRover
admin site. Here, find an Add a Remote Registry button. Click this button and update the form:
- Username: Your GitHub username
- Password: Generate a classic GitHub token with the
read:packages
scope; this is the only required scope for it to work - Domain: ghcr.io
- Image Prefix: Leave this blank
After saving the form, the CapRover
instance can now read all packages (and Docker images) in your GitHub account.