105 lines
3.6 KiB
YAML
105 lines
3.6 KiB
YAML
name: Build and Deploy StreetUp
|
|
|
|
on:
|
|
workflow_dispatch:
|
|
inputs:
|
|
environment:
|
|
description: 'Environnement de déploiement (test ou production)'
|
|
required: true
|
|
default: 'test'
|
|
type: choice
|
|
options:
|
|
- test
|
|
- production
|
|
|
|
env:
|
|
REGISTRY: ghcr.io
|
|
IMAGE_NAME: ${{ github.repository }}
|
|
|
|
jobs:
|
|
build-and-push:
|
|
runs-on: ubuntu-latest
|
|
permissions:
|
|
contents: read
|
|
packages: write
|
|
|
|
steps:
|
|
- name: Checkout repository
|
|
uses: actions/checkout@v6
|
|
|
|
- name: Log in to the Container Registry
|
|
uses: docker/login-action@v4
|
|
with:
|
|
registry: ${{ env.REGISTRY }}
|
|
username: ${{ github.repository_owner }}
|
|
password: ${{ secrets.GITHUB_TOKEN }}
|
|
|
|
- name: Extract metadata (tags, labels) for Docker
|
|
id: meta
|
|
uses: docker/metadata-action@v6
|
|
with:
|
|
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
|
|
tags: |
|
|
type=raw,value=test,enable=${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'test' }}
|
|
type=raw,value=latest,enable=${{ github.event_name == 'workflow_dispatch' && github.event.inputs.environment == 'production' }}
|
|
type=raw,value=test,enable=${{ github.event_name != 'workflow_dispatch' && github.ref == 'refs/heads/main' }}
|
|
type=raw,value=latest,enable=${{ github.event_name != 'workflow_dispatch' && startsWith(github.ref, 'refs/tags/v') }}
|
|
type=semver,pattern={{version}},enable=${{ startsWith(github.ref, 'refs/tags/v') }}
|
|
|
|
|
|
- name: Set up Docker Buildx
|
|
uses: docker/setup-buildx-action@v4
|
|
|
|
- name: Build and push Docker image
|
|
uses: docker/build-push-action@v7
|
|
with:
|
|
context: .
|
|
push: true
|
|
tags: ${{ steps.meta.outputs.tags }}
|
|
labels: ${{ steps.meta.outputs.labels }}
|
|
cache-from: type=gha
|
|
cache-to: type=gha,mode=max
|
|
|
|
deploy:
|
|
needs: build-and-push
|
|
runs-on: ubuntu-latest
|
|
steps:
|
|
- name: Determine Target Environment
|
|
id: target
|
|
run: |
|
|
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
|
|
ENV_CHOICE="${{ github.event.inputs.environment }}"
|
|
else
|
|
if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
|
|
ENV_CHOICE="production"
|
|
else
|
|
ENV_CHOICE="test"
|
|
fi
|
|
fi
|
|
|
|
if [[ "$ENV_CHOICE" == "production" ]]; then
|
|
echo "env=production" >> $GITHUB_OUTPUT
|
|
echo "host=${{ secrets.SSH_HOST_PROD }}" >> $GITHUB_OUTPUT
|
|
echo "path=/home/kdeterme/streetup-prod" >> $GITHUB_OUTPUT
|
|
else
|
|
echo "env=test" >> $GITHUB_OUTPUT
|
|
echo "host=${{ secrets.SSH_HOST_TEST }}" >> $GITHUB_OUTPUT
|
|
echo "path=/home/kdeterme/streetup-test" >> $GITHUB_OUTPUT
|
|
fi
|
|
|
|
- name: Deploy to Server via SSH
|
|
uses: appleboy/ssh-action@v1.0.3
|
|
with:
|
|
host: ${{ steps.target.outputs.host }}
|
|
username: ${{ secrets.SSH_USER }}
|
|
key: ${{ secrets.SSH_PRIVATE_KEY }}
|
|
script: |
|
|
cd ${{ steps.target.outputs.path }}
|
|
# Pull de la nouvelle image
|
|
docker compose pull
|
|
# Relance des conteneurs
|
|
docker compose up -d
|
|
# Exécution des migrations de base de données
|
|
docker compose exec -T web python manage.py migrate --noinput
|
|
# Collecte des fichiers statiques
|
|
docker compose exec -T web python manage.py collectstatic --noinput --clear
|