Skip to content

Commit

Permalink
new workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
leoBitto committed Mar 17, 2024
1 parent ba99921 commit 456b307
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 227 deletions.
72 changes: 0 additions & 72 deletions .github/workflows/CD.yml

This file was deleted.

70 changes: 70 additions & 0 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: CI

# 1
# Controls when the workflow will run
on:
# Triggers the workflow on push events but only for the main branch
push:
branches: main

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
inputs:
version:
description: 'Image version'
required: true
#2
env:
REGISTRY: "registry.digitalocean.com/code-vessel"
IMAGE_NAME: "django:latest"

#3
jobs:
build_and_push:
runs-on: ubuntu-latest
steps:
- name: Checkout the repo
uses: actions/checkout@v2

- 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: 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@v0.1.3
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 running container
docker stop $(echo $IMAGE_NAME)
# Remove old container
docker rm $(echo $IMAGE_NAME)
# Run a new container from a new image
docker run -d \
--restart always \
--name $(echo $IMAGE_NAME) \
$(echo $REGISTRY)/$(echo $IMAGE_NAME):$(echo $GITHUB_SHA | head -c7)
144 changes: 0 additions & 144 deletions .github/workflows/django.yml

This file was deleted.

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
build:
context: ./src
dockerfile: Dockerfile
image: django:latest
command: gunicorn base.wsgi:application --bind 0.0.0.0:8000 --workers 3 --timeout 90
volumes:
- static_volume:/home/app/web/static
Expand All @@ -24,6 +25,7 @@ services:

nginx:
build: ./nginx
image: nginx:latest
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
Expand Down
Loading

0 comments on commit 456b307

Please sign in to comment.