From dd46e7b71a658bbe6768201073eb8d58cf24337c Mon Sep 17 00:00:00 2001 From: Ismail MOUYAHADA Date: Thu, 27 Jun 2024 23:12:57 +0200 Subject: [PATCH] update CICD --- .github/workflows/dev.yaml | 85 +++++++++++++++++++++++++ .github/workflows/prod.yaml | 115 ++++++++++++++++++++++++++++++++++ satsquare/docker-compose.yaml | 1 + 3 files changed, 201 insertions(+) create mode 100644 .github/workflows/dev.yaml create mode 100644 .github/workflows/prod.yaml diff --git a/.github/workflows/dev.yaml b/.github/workflows/dev.yaml new file mode 100644 index 0000000..e9fdcc1 --- /dev/null +++ b/.github/workflows/dev.yaml @@ -0,0 +1,85 @@ +name: Development Workflow + +on: + push: + branches: + - develop + +jobs: + create-environment: + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:latest + env: + POSTGRES_USER: satsquare_user + POSTGRES_PASSWORD: satsquare_password + POSTGRES_DB: satsquare_db + ports: + - 5432:5432 + options: >- + --health-cmd="pg_isready -U satsquare_user" + --health-interval=10s + --health-timeout=5s + --health-retries=5 + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install dependencies + run: npm install + + - name: Run database migrations + run: npx prisma db push + env: + DATABASE_URL: postgresql://satsquare_user:satsquare_password@localhost:5432/satsquare_db + + - name: Seed the database + run: npx prisma seed + env: + DATABASE_URL: postgresql://satsquare_user:satsquare_password@localhost:5432/satsquare_db + + test: + needs: create-environment + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install dependencies + run: npm install + + - name: Run tests + run: npm test + + security: + needs: create-environment + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install dependencies + run: npm install + + - name: Run security checks + run: npm audit --audit-level=moderate diff --git a/.github/workflows/prod.yaml b/.github/workflows/prod.yaml new file mode 100644 index 0000000..ea80f73 --- /dev/null +++ b/.github/workflows/prod.yaml @@ -0,0 +1,115 @@ +name: Production Deployment + +on: + push: + branches: + - main + +jobs: + create-environment: + runs-on: ubuntu-latest + + services: + postgres: + image: postgres:latest + env: + POSTGRES_USER: satsquare_user + POSTGRES_PASSWORD: satsquare_password + POSTGRES_DB: satsquare_db + ports: + - 5432:5432 + options: >- + --health-cmd="pg_isready -U satsquare_user" + --health-interval=10s + --health-timeout=5s + --health-retries=5 + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install dependencies + run: npm install + + - name: Run database migrations + run: npx prisma db push + env: + DATABASE_URL: postgresql://satsquare_user:satsquare_password@localhost:5432/satsquare_db + + - name: Seed the database + run: npx prisma seed + env: + DATABASE_URL: postgresql://satsquare_user:satsquare_password@localhost:5432/satsquare_db + + test: + needs: create-environment + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install dependencies + run: npm install + + - name: Run tests + run: npm test + + security: + needs: create-environment + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Node.js + uses: actions/setup-node@v3 + with: + node-version: 18 + + - name: Install dependencies + run: npm install + + - name: Run security checks + run: npm audit --audit-level=moderate + + deploy: + needs: [create-environment, test, security] + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v2 + + - name: Login to GitHub Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ secrets.GHCR_USERNAME }} + password: ${{ secrets.GHCR_TOKEN }} + + - name: Build and push Docker image + run: | + docker buildx build --push --tag ghcr.io/${{ github.repository }}/nextjs-app:latest . + + - name: Deploy to production + run: | + docker-compose down + docker-compose pull + docker-compose up --build -d + env: + DATABASE_URL: postgresql://satsquare_user:satsquare_password@db:5432/satsquare_db diff --git a/satsquare/docker-compose.yaml b/satsquare/docker-compose.yaml index dbbf866..38d9d31 100644 --- a/satsquare/docker-compose.yaml +++ b/satsquare/docker-compose.yaml @@ -6,6 +6,7 @@ services: build: . ports: - '3000:3000' + - '5157:5157' depends_on: - db environment: