From 19b83e9ac14aaf5a1be2487390f6b561addc1617 Mon Sep 17 00:00:00 2001 From: valere Date: Tue, 3 Feb 2026 15:09:58 +0100 Subject: [PATCH] clean CI --- .github/workflows/deploy.yml | 35 ----------------------------- .github/workflows/merge.yml | 23 +++++++++++++++++++ .github/workflows/push.yml | 41 ++++++++++++++++++++++++++++++++++ .github/workflows/setup-env.sh | 34 ++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+), 35 deletions(-) delete mode 100644 .github/workflows/deploy.yml create mode 100644 .github/workflows/merge.yml create mode 100644 .github/workflows/push.yml create mode 100644 .github/workflows/setup-env.sh diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index 4971dd3..0000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,35 +0,0 @@ -name: Deploy App -on: [push] - -jobs: - build: - runs-on: ubuntu-22.04 - container: - volumes: - - /var/docker-web:/var/docker-web - steps: - - uses: actions/checkout@v4 - - name: Prepare and build app - run: | - REPO_NAME="${GITHUB_REPOSITORY##*/}" - APP_DIR="/var/docker-web/apps/${REPO_NAME}" - bash /var/docker-web/src/cli.sh down "${REPO_NAME}" - rm -rf "$APP_DIR" - mkdir "$APP_DIR" - cp -a $(find . -mindepth 1 -maxdepth 1 ! -name '.git' ! -name 'node_modules') "$APP_DIR/" - export COMPOSE_BAKE=false - docker rmi "local/${REPO_NAME}" 2>/dev/null || true - bash /var/docker-web/src/cli.sh build "${REPO_NAME}" - - deploy: - runs-on: ubuntu-22.04 - needs: build - container: - volumes: - - /var/docker-web:/var/docker-web - steps: - - uses: actions/checkout@v4 - - name: Deploy with docker-web - run: | - REPO_NAME="${GITHUB_REPOSITORY##*/}" - bash /var/docker-web/src/cli.sh up "${REPO_NAME}" diff --git a/.github/workflows/merge.yml b/.github/workflows/merge.yml new file mode 100644 index 0000000..e883efe --- /dev/null +++ b/.github/workflows/merge.yml @@ -0,0 +1,23 @@ +name: Delete Merged Branches +on: + pull_request: + types: [closed] + +jobs: + delete-branch: + runs-on: ubuntu-22.04 + container: + volumes: + - /var/docker-web:/var/docker-web + if: github.event.pull_request.merged == true || github.event_name == 'workflow_dispatch' + steps: + - uses: actions/checkout@v4 + - name: Delete merged branch + run: | + BRANCH_NAME="${{ github.event.pull_request.head.ref || github.event.inputs.branch_name }}" + APP_NAME="${BRANCH_NAME}_${GITHUB_REPOSITORY##*/}" + if [ "$BRANCH_NAME" != "main" ] && [ "$BRANCH_NAME" != "develop" ] && [ -n "$APP_NAME" ]; then + bash /var/docker-web/src/cli.sh rm -y "${APP_NAME}" + else + echo "Cannot delete protected branch: $BRANCH_NAME" + fi diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml new file mode 100644 index 0000000..ecf8842 --- /dev/null +++ b/.github/workflows/push.yml @@ -0,0 +1,41 @@ +name: Deploy App +on: [push] + +jobs: + build: + runs-on: ubuntu-22.04 + container: + volumes: + - /var/docker-web:/var/docker-web + steps: + - uses: actions/checkout@v4 + - name: Build app + run: | + bash ./.github/workflows/setup-env.sh + set -a && source .env && set +a + if [ -n "$APP_NAME" ]; then + set -a && source .env && set +a + bash /var/docker-web/src/cli.sh down "${APP_NAME}" + rm -rf "$APP_DIR" + mkdir "$APP_DIR" + cp -a $(find . -mindepth 1 -maxdepth 1 ! -name '.git' ! -name 'node_modules') "$APP_DIR/" + export COMPOSE_BAKE=false + docker rmi "local/${APP_NAME}" 2>/dev/null || true + bash /var/docker-web/src/cli.sh build "${APP_NAME}" + fi + + deploy: + runs-on: ubuntu-22.04 + needs: build + container: + volumes: + - /var/docker-web:/var/docker-web + steps: + - uses: actions/checkout@v4 + - name: Deploy + run: | + bash ./.github/workflows/setup-env.sh + set -a && source .env && set +a + if [ -n "$APP_NAME" ]; then + bash /var/docker-web/src/cli.sh up "${APP_NAME}" + fi diff --git a/.github/workflows/setup-env.sh b/.github/workflows/setup-env.sh new file mode 100644 index 0000000..73cffb1 --- /dev/null +++ b/.github/workflows/setup-env.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +set -a && source .env && set +a + +changeEnvVar() { + local var_name="$1" + local new_value="$2" + local env_file=".env" + + if grep -q "^${var_name}=" "$env_file"; then + sed -i "s|${var_name}=.*|${var_name}=${new_value}|" "$env_file" + else + echo "${var_name}=${new_value}" >> "$env_file" + fi +} + +# Variables GitHub +APP_NAME="${GITHUB_REPOSITORY##*/}" +BRANCH_NAME=$GITHUB_REF_NAME + +# Configuration pour les branches non-principales +if [ "$BRANCH_NAME" != "main" ] && [ "$BRANCH_NAME" != "master" ]; then + DOMAIN="$BRANCH_NAME.$DOMAIN" + APP_NAME="${BRANCH_NAME}_${APP_NAME}" + PORT=$(bash /var/docker-web/src/cli.sh ALLOCATE_PORT) + sed -i "s|${GITHUB_REPOSITORY##*/}|$APP_NAME|g" docker-compose.yml +fi + +APP_DIR="/var/docker-web/apps/$APP_NAME" + +changeEnvVar "DOMAIN" $DOMAIN +changeEnvVar "APP_NAME" $APP_NAME +changeEnvVar "PORT" $PORT +changeEnvVar "APP_DIR" $APP_DIR