Skip to content

loop for garbage collection added #16

loop for garbage collection added

loop for garbage collection added #16

name: Build and Deploy
on:
push:
branches: [ master ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
version:
description: 'Image version'
required: true
default: 'latest'
env:
REGISTRY: "registry.digitalocean.com/pathphindr"
IMAGE_NAME: "pathphindr-api"
jobs:
cleanup:
runs-on: ubuntu-latest
steps:
- name: Cleanup Docker
run: |
docker system prune -af
docker volume prune -f
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v3
- name: Build container image
run: docker build -t $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7) .
- name: Install doctl
uses: digitalocean/action-doctl@v2
with:
token: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
- name: Log in to DigitalOcean Container Registry with short-lived credentials
run: doctl registry login --expiry-seconds 600
- name: Remove all old images
run: if [ ! -z "$(doctl registry repository list | grep "$(echo $IMAGE_NAME)")" ]; then doctl registry repository delete-manifest $(echo $IMAGE_NAME) $(doctl registry repository list-tags $(echo $IMAGE_NAME) | grep -o "sha.*") --force; else echo "No repository"; fi
- name: Remove Garbage
run: doctl registry garbage-collection start pathphindr --force
- name: Wait for Garbage Collection to Complete
run: |
while [ "$(doctl registry garbage-collection list | grep active)" ]; do
echo "Garbage collection in progress, waiting..."
sleep 30 # Wait for 30 seconds before checking again
done
- name: Push image to DigitalOcean Container Registry
run: docker push $(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
deploy:
runs-on: ubuntu-latest
needs: build_and_push
steps:
- name: Deploy to Digital Ocean droplet via SSH action
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSHKEY }}
passphrase: ${{ secrets.PASSPHRASE }}
envs: IMAGE_NAME,REGISTRY,{{ secrets.DIGITALOCEAN_ACCESS_TOKEN }},GITHUB_SHA
script: |
# Login to registry
docker login -u ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} -p ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }} registry.digitalocean.com
# Stop and remove old container if it exists
if [ "$(docker ps -q -f name=$(echo $IMAGE_NAME))" ]; then
docker stop $(echo $IMAGE_NAME)
docker rm $(echo $IMAGE_NAME)
fi
# Run a new container from a new image
docker run -d \
--restart always \
--name $(echo $IMAGE_NAME) \
-p 8080:8080 \
-e DB_HOST=${{ secrets.DB_HOST }} \
-e DB_USERNAME=${{ secrets.DB_USERNAME }} \
-e DB_PASSWORD=${{ secrets.DB_PASSWORD }} \
-e DB_NAME=${{ secrets.DB_NAME }} \
-e DB_PORT=${{ secrets.DB_PORT }} \
-e SPRING_PROFILES_ACTIVE=prod \
$(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)