Skip to content

Instantly share code, notes, and snippets.

@adamghill
Last active November 26, 2023 19:36
Show Gist options
  • Save adamghill/e63556cb9dbd0ee85dc0334549a7a00f to your computer and use it in GitHub Desktop.
Save adamghill/e63556cb9dbd0ee85dc0334549a7a00f to your computer and use it in GitHub Desktop.
GitHub Action to build and deploy Docker image to CapRover
# Might be outdated! Check https://github.com/marketplace/actions/build-docker-and-deploy-to-caprover for the latest version.
name: Create and publish Docker image to CapRover
# Requires the following Action secrets to be set in your GitHub repo:
# - CAPROVER_APP_TOKEN
# - CAPROVER_SERVER_URL
on:
push:
branches: ["main"]
env:
BRANCH_NAME: ${{ github.ref_name || github.head_ref }}
DOCKER_REGISTRY: ghcr.io
DOCKER_FILE_NAME: ./Dockerfile
IMAGE_NAME: ${{ github.repository }}
CAPROVER_APP: app-name
jobs:
build-and-push-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.DOCKER_REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
push: true
context: .
file: ${{ env.DOCKER_FILE_NAME }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Deploy image to CapRover
run: |
docker run caprover/cli-caprover:latest caprover deploy --host ${{ secrets.CAPROVER_SERVER_URL }} --appToken ${{ secrets.CAPROVER_APP_TOKEN }} --appName ${{ env.CAPROVER_APP }} --imageName ${{ env.DOCKER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.BRANCH_NAME }}
shell: bash
@adamghill
Copy link
Author

adamghill commented May 9, 2023

I converted this into a GitHub Action on the marketplace: https://github.com/marketplace/actions/build-docker-and-deploy-to-caprover.

Create CAPROVER_SERVER_URL and CAPROVER_APP_TOKEN Action secrets and then make a new action with this yaml:

on:
  push:
    branches:
      - main

jobs:
  deploy-to-caprover:
    name: Deploy to CapRover
    runs-on: ubuntu-latest

    # Ensure the correct permissions are granted
    permissions:
      contents: read
      packages: write

    steps:
      - name: Checkout repository
        uses: actions/checkout@v4

      - uses: adamghill/build-docker-and-deploy-to-caprover@v2.3.0
        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 }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment