Skip to content
79 changes: 79 additions & 0 deletions .github/workflows/self-hosted-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Self-Hosted CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
workflow_dispatch:

jobs:
build-and-test:
runs-on: self-hosted

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log Node.js version
run: node --version

- name: Cache node modules
uses: actions/cache@v3
id: npm-cache
with:
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-

- name: Install dependencies
if: steps.npm-cache.outputs.cache-hit != 'true'
run: npm ci

- name: Run tests
run: npm test -- --watch=false --browsers=ChromeHeadless --no-progress

- name: Build application
run: npm run build -- --configuration production

docker-build:
runs-on: self-hosted
needs: build-and-test

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Build Docker image
run: docker build -t angular-app:${{ github.sha }} .

- name: Test Docker image
run: |
echo "Testing Docker image..."
docker run -d --name test-container -p 8080:80 angular-app:${{ github.sha }}
echo "Container started, waiting for it to initialize..."
sleep 10
curl -s http://localhost:8080 | grep -q "conduit" && echo "Application is running correctly" || echo "Failed to verify application"
docker stop test-container
docker rm test-container

cleanup:
runs-on: self-hosted
needs: [build-and-test, docker-build]
if: always()

steps:
- name: Clean workspace
run: |
echo "Cleaning workspace to free up disk space"
rm -rf node_modules || true
rm -rf dist || true
npm cache clean --force || true

- name: Prune Docker images
run: |
echo "Keeping only recent Docker images"
docker image ls
# Conserver seulement les 3 images les plus récentes
docker image prune -a -f --filter "until=24h"
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Étape 1: Build de l'application
FROM node:16-alpine as build
WORKDIR /app

# Optimisation des couches de cache
COPY package.json package-lock.json ./
RUN npm i --force

# Copie du reste des fichiers
COPY . .
RUN npm run build --

# Étape 2: Servir l'application avec NGINX
FROM nginx:alpine
COPY --from=build /app/dist/ /usr/share/nginx/html

# Configuration pour les Single Page Applications
RUN echo 'server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html; try_files $uri $uri/ /index.html; } }' > /etc/nginx/conf.d/default.conf

# Sécurité : Exécution en tant qu'utilisateur non-root
RUN chown -R nginx:nginx /usr/share/nginx/html && \
chmod -R 755 /usr/share/nginx/html && \
chown -R nginx:nginx /var/cache/nginx && \
chown -R nginx:nginx /var/log/nginx && \
chown -R nginx:nginx /etc/nginx/conf.d
RUN touch /var/run/nginx.pid && \
chown -R nginx:nginx /var/run/nginx.pid

USER nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]