Skip to content

Instantly share code, notes, and snippets.

@Pyroarsonist
Created November 20, 2023 13:51
Show Gist options
  • Select an option

  • Save Pyroarsonist/d74af1cb8df5a81dfb0054e86abe7085 to your computer and use it in GitHub Desktop.

Select an option

Save Pyroarsonist/d74af1cb8df5a81dfb0054e86abe7085 to your computer and use it in GitHub Desktop.
Build and deploy using ghcr
name: Build and Deploy
on:
push:
branches:
- master
jobs:
build:
runs-on: self-hosted
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Create env file
env:
ENV_FILE: ${{ secrets.ENV_FILE }}
run: echo "$ENV_FILE" > .env
- name: Build docker container and push to ghcr
run: bash ./docker-build.sh
deploy:
runs-on: self-hosted
needs: build
steps:
- name: Remove old docker container
run: docker ps -q --filter "name=your-project" | xargs -r docker stop && docker ps -aq --filter "name=your-project" | xargs -r docker rm
- name: Deploy of docker container
run: docker run --name your-project -p 3230:80 -d ghcr.io/your-user/your-project
#!/bin/bash
PACKAGE_VERSION=$(sed -n 's/.*"version": *"\([^"]*\)".*/\1/p' package.json)
echo "Building your-project with v$PACKAGE_VERSION"
echo "$CR_PAT" | docker login ghcr.io -u your-user --password-stdin
docker build -t "ghcr.io/your-user/your-project:$PACKAGE_VERSION" -t ghcr.io/your-user/your-project .
docker push "ghcr.io/your-user/your-project:$PACKAGE_VERSION"
docker push ghcr.io/your-user/your-project

Prerequisites

  1. You should have github repo with .env created in secrets.ENV_FILE and be registered in ghcr (docker registry from Github).

  2. Ensure, that your working machine is self-hosted (https://github.com/user/project/settings/actions/runners).

  3. CR_PAT variable should be exported as github token on machine env.

  4. You can pull and start docker container without Github actions on host-runner machine.

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