Last active
November 26, 2023 19:36
-
-
Save adamghill/e63556cb9dbd0ee85dc0334549a7a00f to your computer and use it in GitHub Desktop.
GitHub Action to build and deploy Docker image to CapRover
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
andCAPROVER_APP_TOKEN
Action secrets and then make a new action with this yaml: