Develop to Main (#6) #9
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: Deploy | |
on: | |
push: | |
branches: | |
- main # or the branch you want to deploy from | |
jobs: | |
build: | |
name: Build and Push Docker Images | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Set up Docker | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to GitHub Container Registry | |
run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin | |
- name: Build Docker Images with Docker Compose | |
run: | | |
docker compose build | |
- name: Push Docker Images to GHCR | |
run: | | |
docker push ghcr.io/ls1intum/thaii/client:latest | |
docker push ghcr.io/ls1intum/thaii/server:latest | |
deploy: | |
name: Deploy Application | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: SSH to VM and Execute Docker-Compose Down | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_DOMAIN }} | |
username: ${{ secrets.SERVER_USER }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
docker compose -f compose.yml --env-file=.env down --remove-orphans --rmi all | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
- name: Copy Files to Server | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_DOMAIN }} | |
username: ${{ secrets.SERVER_USER }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
scp -o StrictHostKeyChecking=no ./compose.yml ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }}:~/compose.yml | |
scp -o StrictHostKeyChecking=no -r ./letsencrypt ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }}:~/letsencrypt | |
- name: Set Up Environment Variables | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_DOMAIN }} | |
username: ${{ secrets.SERVER_USER }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} << 'EOF' | |
touch .env | |
echo "OPENAI_API_KEY=${{ secrets.OPENAI_API_KEY }}" >> .env | |
echo "DEBUG=${{ secrets.DEBUG }}" >> .env | |
echo "SECRET_KEY=${{ secrets.SECRET_KEY }}" >> .env | |
echo "POSTGRES_DB=${{ secrets.POSTGRES_DB }}" >> .env | |
echo "POSTGRES_USER=${{ secrets.POSTGRES_USER }}" >> .env | |
echo "POSTGRES_PASSWORD=${{ secrets.POSTGRES_PASSWORD }}" >> .env | |
echo "POSTGRES_HOST=${{ secrets.POSTGRES_HOST }}" >> .env | |
echo "EMAIL_USE_TLS=${{ secrets.EMAIL_USE_TLS }}" >> .env | |
echo "EMAIL_HOST=${{ secrets.EMAIL_HOST }}" >> .env | |
echo "EMAIL_HOST_USER=${{ secrets.EMAIL_HOST_USER }}" >> .env | |
echo "EMAIL_HOST_PASSWORD=${{ secrets.EMAIL_HOST_PASSWORD }}" >> .env | |
echo "DEFAULT_FROM_EMAIL=${{ secrets.DEFAULT_FROM_EMAIL }}" >> .env | |
echo "EMAIL_PORT=${{ secrets.EMAIL_PORT }}" >> .env | |
echo "DJANGO_SUPERUSER_USERNAME=${{ secrets.DJANGO_SUPERUSER_USERNAME }}" >> .env | |
echo "DJANGO_SUPERUSER_PASSWORD=${{ secrets.DJANGO_SUPERUSER_PASSWORD }}" >> .env | |
echo "DJANGO_SUPERUSER_EMAIL=${{ secrets.DJANGO_SUPERUSER_EMAIL }}" >> .env | |
EOF | |
- name: SSH to VM and Execute Docker-Compose Up | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.SERVER_DOMAIN }} | |
username: ${{ secrets.SERVER_USER }} | |
key: ${{ secrets.SSH_KEY }} | |
script: | | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "mkdir -p ~/" | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "touch ~/letsencrypt/acme.json && chmod 600 ~/letsencrypt/acme.json" | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "docker login ghcr.io -u ${{ github.actor }} --password-stdin <<< ${{ secrets.GITHUB_TOKEN }}" | |
ssh -o StrictHostKeyChecking=no ${{ secrets.SERVER_USER }}@${{ secrets.SERVER_DOMAIN }} "docker compose pull && docker compose up -d && docker compose logs" |