Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
leoBitto committed Apr 5, 2024
2 parents 7e63594 + 145f7f3 commit d014f34
Show file tree
Hide file tree
Showing 9 changed files with 303 additions and 205 deletions.
197 changes: 197 additions & 0 deletions .github/workflows/CI-CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
name: CI/CD

on:
push:
branches: main
workflow_dispatch:


env:
REGISTRY: ghcr.io
IMAGE_NAME: djangoforge

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

- name: Create config directory and .env file
run: |
mkdir -p config
echo "DEBUG=False" >> config/.env
echo "SECRET_KEY=foo" >> config/.env
echo "DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] *" >> config/.env
echo "SQL_ENGINE=django.db.backends.postgresql" >> config/.env
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> config/.env
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> config/.env
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> config/.env
echo "SQL_HOST=db" >> config/.env
echo "SQL_PORT=5432" >> config/.env
echo "DATABASE=postgres" >> config/.env
echo "DOMAIN=${{ secrets.DOMAIN }}" >> config/.env
working-directory: .

- name: Display content of .env file
run: cat ./config/.env

- name: Update Nginx configuration file
run: |
sed -i "s/server_name localhost/server_name ${{ secrets.DOMAIN }}/g" ./nginx/nginx.conf
working-directory: .

- name: Build Docker image using Docker Compose
run: docker-compose -f docker-compose.dev.yml build

- name: verify that all the container have been built
run: docker images

- name: LOG IN to container registry
run: echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin

- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
env:
OWNER: '${{ github.repository_owner }}'

- name: Tag image
run: |
echo ${REGISTRY}/${OWNER_LC}/${IMAGE_NAME}:latest
docker tag webapp_django ${REGISTRY}/${OWNER_LC}/${IMAGE_NAME}:latest
- name: List Docker images
run: docker images

- name: Push image to GitHub Container Registry
run: |
echo ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
docker push ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
deploy:
runs-on: ubuntu-latest
needs: build_and_push

steps:

- name: Checkout del repository
uses: actions/checkout@v2

- name: Set ENV variables
run: |
echo "REPO_NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/}" >> $GITHUB_ENV
echo $GITHUB_ENV
- name: Create project directory and conf file
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
mkdir -p "/opt/${{ env.REPO_NAME }}"
mkdir -p "/opt/${{ env.REPO_NAME }}/config"
rm /opt/${{ env.REPO_NAME }}/config/.env
echo "DEBUG=False" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SECRET_KEY=foo" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] *" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SQL_ENGINE=django.db.backends.postgresql" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SQL_HOST=db" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "SQL_PORT=5432" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DATABASE=postgres" >> "/opt/${{ env.REPO_NAME }}/config/.env"
echo "DOMAIN=${{ secrets.DOMAIN }}" >> "/opt/${{ env.REPO_NAME }}/config/.env"
- name: SCP Docker Compose file and Nginx conf
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
source: "./docker-compose.prod.yml"
target: "/opt/${{ env.REPO_NAME }}/"

- name: SCP Nginx conf file
uses: appleboy/scp-action@master
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
source: "./nginx/nginx.conf"
target: "/opt/${{ env.REPO_NAME }}/"

- name: Update Nginx configuration file
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sed -i "s/server_name localhost/server_name ${{ secrets.DOMAIN }}/g" /opt/${{ env.REPO_NAME }}/nginx/nginx.conf
- name: Upgrade server and install Docker
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
sudo apt update -y
sudo apt upgrade -y
# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
# Add the repository to Apt sources:
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update -y
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
- name: set lower case owner name
run: |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV}
env:
OWNER: '${{ github.repository_owner }}'

# - name: LOG IN to container registry
# run: |
# echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin

# - name: Pull image from GitHub Container Registry
# run: |
# echo ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
# docker pull ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
# docker tag ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}
# docker images

- name: Stop And Start containers
uses: appleboy/[email protected]
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.PRIVATE_KEY }}
script: |
echo ${{ secrets.GITHUB_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin
echo ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest
echo ${REGISTRY}
echo ${OWNER_LC}
docker pull ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest
docker tag ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}
docker images
docker tag ${REGISTRY}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest ${{ env.IMAGE_NAME }}
docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose.prod.yml down
docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose.prod.yml up -d --force-recreate
# docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose-prod.yml exec -T web python manage.py migrate --noinput
# docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose-prod.yml exec -T web python manage.py collectstatic --noinput --clear


71 changes: 0 additions & 71 deletions .github/workflows/build-and-push.yml

This file was deleted.

88 changes: 0 additions & 88 deletions .github/workflows/deploy.yml

This file was deleted.

9 changes: 7 additions & 2 deletions docker-compose.yml → docker-compose.dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
build:
context: ./src
dockerfile: Dockerfile
image: webapp_django
command: gunicorn base.wsgi:application --bind 0.0.0.0:8000 --workers 3 --timeout 90
volumes:
- static_volume:/home/app/web/static
Expand All @@ -15,21 +16,25 @@ services:
- ./config/.env
depends_on:
- db


db:
image: postgres
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data/
env_file:
- ./config/.env

nginx:
build: ./nginx
image: nginx:latest
volumes:
- static_volume:/home/app/web/static
- media_volume:/home/app/web/media
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro # metti il file di conf di nginx sul suo container e impostalo in read only
ports:
- 80:80
#- 443:443
restart: always
depends_on:
- web

Expand Down
Loading

0 comments on commit d014f34

Please sign in to comment.