added manually email e domain #29
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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@v3 | |
with: | |
submodules: recursive | |
token: ${{ secrets.GITHUB_TOKEN }} | |
- 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@v3 | |
- 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 "EMAIL=${{ secrets.EMAIL }}" >> "/opt/${{ env.REPO_NAME }}/config/.env" | |
echo "DOMAIN=${{ secrets.DOMAIN }}" >> "/opt/${{ env.REPO_NAME }}/config/.env" | |
cat "/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.prod.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/yourdomain.com/${{ secrets.DOMAIN }}/g" /opt/${{ env.REPO_NAME }}/nginx/nginx.prod.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 docker-compose-plugin -y | |
- name: set lower case owner name | |
run: | | |
echo "OWNER_LC=${OWNER,,}" >>${GITHUB_ENV} | |
env: | |
OWNER: '${{ github.repository_owner }}' | |
- 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 | |
docker pull ghcr.io/leobitto/${{ env.IMAGE_NAME }}:latest | |
docker tag ghcr.io/leobitto/${{ 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 | |
echo "wait for it.." | |
sleep 5 | |
echo "5 seconds has passed" | |
docker exec web python manage.py migrate | |
docker exec web python manage.py collectstatic --noinput --clear | |