Skip to content

Instantly share code, notes, and snippets.

@Cooops
Created September 12, 2024 02:03
Show Gist options
  • Save Cooops/c34131798960487d2171413f3180d1ba to your computer and use it in GitHub Desktop.
Save Cooops/c34131798960487d2171413f3180d1ba to your computer and use it in GitHub Desktop.
# Name our workflow
name: build-and-deploy-api-post
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# Build and push the stack to docker hub, and then update our DO droplet with the new stack
push:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Push our Production image to Docker Hub
- name: Login to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}
# Builds our Production image from the yaml file we define
- name: Build image
run: docker compose -f docker-compose.prod.yaml build
# Tags our Production image with the username and latest tag (can change this) for Docker Hub
- name: Tag image
run: docker tag lodestar-api lodestardev/lodestar-api:latest
# Pushes containers to Docker Hub so that we can pull it afterwards
- name: Push image
run: docker push lodestardev/lodestar-api:latest
# Tells our Droplet:
# power down existing containers (otherwise we conflict)
# purge all existing images and containers
# pull the most recent image from docker hub
# run the most recent image
- name: Update Stack
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.GL_SSH_HOST }}
username: ${{ secrets.GL_SSH_USERNAME }}
password: ${{ secrets.GL_SSH_PASSWORD }}
port: ${{ secrets.GL_SSH_PORT }}
script: |
cd /etc/myapp && docker-compose down && docker system prune -a --force && docker network prune --force && docker-compose pull && docker-compose up -d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment