second version of deploy #21
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: old - CI/CD | ||
on: | ||
push: | ||
branches: main | ||
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: Call reusable workflow to create config files | ||
uses: ./.github/workflows/create-config-files.yml | ||
with: | ||
repo_name: ${{ env.REPO_NAME }} | ||
host: ${{ secrets.HOST }} | ||
username: ${{ secrets.USERNAME }} | ||
private_key: ${{ secrets.PRIVATE_KEY }} | ||
working-directory: . | ||
- name: Display content of .env file | ||
run: cat ./config/.env | ||
- 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.GHCR_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: | | ||
docker tag webapp_django ${{ env.REGISTRY }}/${OWNER_LC}/${{ env.IMAGE_NAME }}:latest | ||
- name: List Docker images | ||
run: docker images | ||
- name: Push image to GitHub Container Registry | ||
run: | | ||
docker push ${{ env.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 | ||
with: | ||
submodules: recursive | ||
token: ${{ secrets.GHCR_TOKEN }} | ||
- name: Get repo name to give directory a name | ||
run: | | ||
echo "REPO_NAME=${GITHUB_REPOSITORY#$GITHUB_REPOSITORY_OWNER/}" >> $GITHUB_ENV | ||
echo $GITHUB_ENV | ||
- name: Call reusable workflow to create config files | ||
uses: ./.github/workflows/create-config-files.yml | ||
with: | ||
repo_name: ${{ env.REPO_NAME }} | ||
host: ${{ secrets.HOST }} | ||
username: ${{ secrets.USERNAME }} | ||
private_key: ${{ secrets.PRIVATE_KEY }} | ||
- name: SCP Docker Compose file | ||
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/" | ||
target: "/etc/nginx/sites-available/" | ||
strip_components: 1 | ||
- name: Update Nginx configuration file | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.HOST }} | ||
username: ${{ secrets.USERNAME }} | ||
key: ${{ secrets.PRIVATE_KEY }} | ||
script: | | ||
sed -i "s/localhost/${{ secrets.DOMAIN }}/g" /etc/nginx/sites-available/nginx.conf | ||
- name: Update server | ||
uses: appleboy/[email protected] | ||
with: | ||
host: ${{ secrets.HOST }} | ||
username: ${{ secrets.USERNAME }} | ||
key: ${{ secrets.PRIVATE_KEY }} | ||
script: | | ||
sudo apt update -y | ||
sudo apt upgrade -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.GHCR_TOKEN }} | docker login ghcr.io -u ${{ github.actor }} --password-stdin | ||
docker system prune -f | ||
docker-compose -f /opt/${{ env.REPO_NAME }}/docker-compose.prod.yml down | ||
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 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 | ||