139 lines
4.5 KiB
YAML
139 lines
4.5 KiB
YAML
name: Build, Test and Deploy
|
|
|
|
on:
|
|
push:
|
|
branches: [main, int, dev]
|
|
|
|
concurrency:
|
|
group: print-calculator-${{ gitea.ref }}
|
|
cancel-in-progress: true
|
|
|
|
jobs:
|
|
test-backend:
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
# Evito actions/setup-python (spesso fragile su act_runner)
|
|
- name: Install Python deps + run tests
|
|
shell: bash
|
|
run: |
|
|
apt-get update
|
|
apt-get install -y --no-install-recommends python3 python3-pip
|
|
python3 -m pip install --upgrade pip
|
|
python3 -m pip install -r backend/requirements.txt
|
|
python3 -m pip install pytest httpx
|
|
export PYTHONPATH="${PYTHONPATH}:$(pwd)/backend"
|
|
pytest backend/tests
|
|
|
|
build-and-push:
|
|
needs: test-backend
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set TAG
|
|
shell: bash
|
|
run: |
|
|
if [[ "${{ gitea.ref }}" == "refs/heads/main" ]]; then
|
|
echo "TAG=prod" >> "$GITHUB_ENV"
|
|
elif [[ "${{ gitea.ref }}" == "refs/heads/int" ]]; then
|
|
echo "TAG=int" >> "$GITHUB_ENV"
|
|
else
|
|
echo "TAG=dev" >> "$GITHUB_ENV"
|
|
fi
|
|
|
|
- name: Ensure docker CLI exists
|
|
shell: bash
|
|
run: |
|
|
if ! command -v docker >/dev/null 2>&1; then
|
|
apt-get update
|
|
apt-get install -y --no-install-recommends docker.io
|
|
fi
|
|
docker version
|
|
|
|
- name: Login to Gitea Registry
|
|
shell: bash
|
|
run: |
|
|
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login "${{ secrets.REGISTRY_URL }}" \
|
|
-u "${{ secrets.REGISTRY_USER }}" --password-stdin
|
|
|
|
- name: Build & Push Backend
|
|
shell: bash
|
|
run: |
|
|
BACKEND_IMAGE="${{ secrets.REGISTRY_URL }}/${{ gitea.repository_owner }}/print-calculator-backend:${{ env.TAG }}"
|
|
docker build -t "$BACKEND_IMAGE" ./backend
|
|
docker push "$BACKEND_IMAGE"
|
|
|
|
- name: Build & Push Frontend
|
|
shell: bash
|
|
run: |
|
|
FRONTEND_IMAGE="${{ secrets.REGISTRY_URL }}/${{ gitea.repository_owner }}/print-calculator-frontend:${{ env.TAG }}"
|
|
docker build -t "$FRONTEND_IMAGE" ./frontend
|
|
docker push "$FRONTEND_IMAGE"
|
|
|
|
deploy:
|
|
needs: build-and-push
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Checkout (serve per avere compose + env nel workspace)
|
|
uses: actions/checkout@v4
|
|
|
|
- name: Set ENV
|
|
shell: bash
|
|
run: |
|
|
if [[ "${{ gitea.ref }}" == "refs/heads/main" ]]; then
|
|
echo "ENV=prod" >> "$GITHUB_ENV"
|
|
elif [[ "${{ gitea.ref }}" == "refs/heads/int" ]]; then
|
|
echo "ENV=int" >> "$GITHUB_ENV"
|
|
else
|
|
echo "ENV=dev" >> "$GITHUB_ENV"
|
|
fi
|
|
|
|
- name: Create Remote Directory
|
|
uses: appleboy/ssh-action@v0.1.10
|
|
with:
|
|
host: ${{ secrets.SERVER_HOST }}
|
|
username: ${{ secrets.SERVER_USER }}
|
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
script: mkdir -p /mnt/user/appdata/print-calculator/${{ env.ENV }}/
|
|
|
|
- name: Copy Compose File to Server
|
|
uses: appleboy/scp-action@v0.1.4
|
|
with:
|
|
host: ${{ secrets.SERVER_HOST }}
|
|
username: ${{ secrets.SERVER_USER }}
|
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
source: "docker-compose.deploy.yml"
|
|
target: "/mnt/user/appdata/print-calculator/${{ env.ENV }}/"
|
|
|
|
- name: Copy Env File to Server
|
|
uses: appleboy/scp-action@v0.1.4
|
|
with:
|
|
host: ${{ secrets.SERVER_HOST }}
|
|
username: ${{ secrets.SERVER_USER }}
|
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
source: "deploy/envs/${{ env.ENV }}.env"
|
|
target: "/mnt/user/appdata/print-calculator/${{ env.ENV }}/"
|
|
|
|
- name: Execute Remote Deployment
|
|
uses: appleboy/ssh-action@v0.1.10
|
|
with:
|
|
host: ${{ secrets.SERVER_HOST }}
|
|
username: ${{ secrets.SERVER_USER }}
|
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
script: |
|
|
set -e
|
|
cd /mnt/user/appdata/print-calculator/${{ env.ENV }}/
|
|
|
|
# il file copiato si chiama "dev.env"/"int.env"/"prod.env"
|
|
mv "${{ env.ENV }}.env" .env
|
|
|
|
echo "${{ secrets.REGISTRY_TOKEN }}" | docker login "${{ secrets.REGISTRY_URL }}" \
|
|
-u "${{ secrets.REGISTRY_USER }}" --password-stdin
|
|
|
|
docker compose --env-file .env -f docker-compose.deploy.yml pull
|
|
docker compose --env-file .env -f docker-compose.deploy.yml up -d --remove-orphans
|