|
| 1 | +# name: CI/CD pipeline for the eventify API |
| 2 | + |
| 3 | +# on: |
| 4 | +# push: |
| 5 | +# branches: |
| 6 | +# - master |
| 7 | +# pull_request: |
| 8 | +# branches: |
| 9 | +# - master |
| 10 | + |
| 11 | +# jobs: |
| 12 | +# build-and-test: |
| 13 | +# runs-on: ubuntu-latest |
| 14 | + |
| 15 | +# steps: |
| 16 | +# # Checkout code from the repository |
| 17 | +# - name: Checkout code |
| 18 | +# uses: actions/checkout@v3 |
| 19 | + |
| 20 | +# # Set up Node.js environment |
| 21 | +# - name: Set up Node.js |
| 22 | +# uses: actions/setup-node@v3 |
| 23 | +# with: |
| 24 | +# node-version: '20' |
| 25 | + |
| 26 | +# # Install dependencies |
| 27 | +# - name: Install dependencies |
| 28 | +# run: npm install |
| 29 | + |
| 30 | +# # Create .env file |
| 31 | +# - name: Create .env file |
| 32 | +# run: | |
| 33 | +# echo ${{ secrets.PORT }} > .env |
| 34 | +# echo ${{ secrets.MONGO_ATLAS_URI }} >> .env |
| 35 | +# echo ${{ secrets.MAIL_HOST }} >> .env |
| 36 | +# echo ${{ secrets.MAIL_PORT }} >> .env |
| 37 | +# echo ${{ secrets.MAIL_USER }} >> .env |
| 38 | +# echo ${{ secrets.MAIL_PASSWORD }} >> .env |
| 39 | +# echo ${{ secrets.JWT_SECRET }} >> .env |
| 40 | +# echo ${{ secrets.JWT_REFRESH_SECRET }} >> .env |
| 41 | +# echo ${{ secrets.JWT_EXPIRES_IN }} >> .env |
| 42 | +# echo ${{ secrets.JWT_REFRESH_TOKEN_EXPIRES_IN }} >> .env |
| 43 | +# echo ${{ secrets.JWT_ACCESS_EXPIRES_IN }} >> .env |
| 44 | + |
| 45 | +# # Run tests |
| 46 | +# - name: Run unit tests |
| 47 | +# run: npm test |
| 48 | + |
| 49 | +# dockerize: |
| 50 | +# runs-on: ubuntu-latest |
| 51 | +# needs: build-and-test |
| 52 | + |
| 53 | +# steps: |
| 54 | +# # Checkout code from the repository |
| 55 | +# - name: Checkout code |
| 56 | +# uses: actions/checkout@v3 |
| 57 | + |
| 58 | +# # Set up Docker Buildx |
| 59 | +# - name: Set up Docker Buildx |
| 60 | +# uses: docker/setup-buildx-action@v2 |
| 61 | + |
| 62 | +# # Log in to Docker Hub |
| 63 | +# - name: Log in to Docker Hub |
| 64 | +# uses: docker/login-action@v2 |
| 65 | +# with: |
| 66 | +# username: ${{ secrets.DOCKER_USERNAME }} |
| 67 | +# password: ${{ secrets.DOCKER_PASSWORD }} |
| 68 | + |
| 69 | +# # Build And Push Docker image |
| 70 | +# - name: Build and push Docker image |
| 71 | +# uses: docker/build-push-action@v4 |
| 72 | +# with: |
| 73 | +# context: . |
| 74 | +# push: true |
| 75 | +# tags: ${{ secrets.DOCKER_USERNAME }}/eventify-api:latest |
| 76 | + |
| 77 | +# # Log out from Docker Hub |
| 78 | +# - name: Log out from Docker Hub |
| 79 | +# run: docker logout |
| 80 | + |
1 | 81 | name: CI/CD pipeline for the eventify API |
2 | 82 |
|
3 | 83 | on: |
4 | 84 | push: |
5 | | - branches: |
6 | | - - master |
| 85 | + branches: [ master ] |
7 | 86 | pull_request: |
8 | | - branches: |
9 | | - - master |
| 87 | + branches: [ master ] |
10 | 88 |
|
11 | 89 | jobs: |
12 | 90 | build-and-test: |
13 | 91 | runs-on: ubuntu-latest |
14 | | - |
| 92 | + env: |
| 93 | + NODE_ENV: test |
| 94 | + NODE_OPTIONS: --max-old-space-size=256 |
15 | 95 | steps: |
16 | | - # Checkout code from the repository |
17 | | - - name: Checkout code |
18 | | - uses: actions/checkout@v3 |
| 96 | + - name: Checkout |
| 97 | + uses: actions/checkout@v4 |
19 | 98 |
|
20 | | - # Set up Node.js environment |
21 | | - - name: Set up Node.js |
22 | | - uses: actions/setup-node@v3 |
| 99 | + - name: Use Node.js 20.x |
| 100 | + uses: actions/setup-node@v4 |
23 | 101 | with: |
24 | 102 | node-version: '20' |
| 103 | + cache: 'npm' |
| 104 | + |
| 105 | + - name: Install deps (CI) |
| 106 | + run: npm ci |
| 107 | + |
| 108 | + # If your unit tests need env vars, write them as KEY=VALUE lines. |
| 109 | + # (What you had before wrote only values, which doesn't work.) |
| 110 | + - name: Create .env for tests |
| 111 | + run: | |
| 112 | + { |
| 113 | + echo "PORT=${{ secrets.PORT }}" |
| 114 | + echo "MONGO_URI=${{ secrets.MONGO_ATLAS_URI }}" |
| 115 | + echo "MAIL_HOST=${{ secrets.MAIL_HOST }}" |
| 116 | + echo "MAIL_PORT=${{ secrets.MAIL_PORT }}" |
| 117 | + echo "MAIL_USER=${{ secrets.MAIL_USER }}" |
| 118 | + echo "MAIL_PASSWORD=${{ secrets.MAIL_PASSWORD }}" |
| 119 | + echo "JWT_SECRET=${{ secrets.JWT_SECRET }}" |
| 120 | + echo "JWT_REFRESH_SECRET=${{ secrets.JWT_REFRESH_SECRET }}" |
| 121 | + echo "JWT_EXPIRES_IN=${{ secrets.JWT_EXPIRES_IN }}" |
| 122 | + echo "JWT_REFRESH_TOKEN_EXPIRES_IN=${{ secrets.JWT_REFRESH_TOKEN_EXPIRES_IN }}" |
| 123 | + echo "JWT_ACCESS_EXPIRES_IN=${{ secrets.JWT_ACCESS_EXPIRES_IN }}" |
| 124 | + } > .env |
25 | 125 |
|
26 | | - # Install dependencies |
27 | | - - name: Install dependencies |
28 | | - run: npm install |
29 | | - |
30 | | - # Create .env file |
31 | | - - name: Create .env file |
32 | | - run: | |
33 | | - echo ${{ secrets.PORT }} > .env |
34 | | - echo ${{ secrets.MONGO_ATLAS_URI }} >> .env |
35 | | - echo ${{ secrets.MAIL_HOST }} >> .env |
36 | | - echo ${{ secrets.MAIL_PORT }} >> .env |
37 | | - echo ${{ secrets.MAIL_USER }} >> .env |
38 | | - echo ${{ secrets.MAIL_PASSWORD }} >> .env |
39 | | - echo ${{ secrets.JWT_SECRET }} >> .env |
40 | | - echo ${{ secrets.JWT_REFRESH_SECRET }} >> .env |
41 | | - echo ${{ secrets.JWT_EXPIRES_IN }} >> .env |
42 | | - echo ${{ secrets.JWT_REFRESH_TOKEN_EXPIRES_IN }} >> .env |
43 | | - echo ${{ secrets.JWT_ACCESS_EXPIRES_IN }} >> .env |
44 | | -
|
45 | | - # Run tests |
46 | | - - name: Run unit tests |
47 | | - run: npm test |
| 126 | + - name: TypeScript build (ensure dist exists) |
| 127 | + run: npm run build |
| 128 | + |
| 129 | + - name: Run unit tests (reduced workers to avoid OOM) |
| 130 | + run: npm test -- --runInBand --maxWorkers=50% |
48 | 131 |
|
49 | 132 | dockerize: |
50 | 133 | runs-on: ubuntu-latest |
51 | 134 | needs: build-and-test |
52 | | - |
| 135 | + permissions: |
| 136 | + contents: read |
| 137 | + packages: write |
53 | 138 | steps: |
54 | | - # Checkout code from the repository |
55 | | - - name: Checkout code |
56 | | - uses: actions/checkout@v3 |
| 139 | + - name: Checkout |
| 140 | + uses: actions/checkout@v4 |
57 | 141 |
|
58 | | - # Set up Docker Buildx |
59 | 142 | - name: Set up Docker Buildx |
60 | | - uses: docker/setup-buildx-action@v2 |
| 143 | + uses: docker/setup-buildx-action@v3 |
61 | 144 |
|
62 | | - # Log in to Docker Hub |
63 | 145 | - name: Log in to Docker Hub |
64 | | - uses: docker/login-action@v2 |
| 146 | + uses: docker/login-action@v3 |
65 | 147 | with: |
66 | 148 | username: ${{ secrets.DOCKER_USERNAME }} |
67 | 149 | password: ${{ secrets.DOCKER_PASSWORD }} |
68 | 150 |
|
69 | | - # Build And Push Docker image |
70 | | - - name: Build and push Docker image |
71 | | - uses: docker/build-push-action@v4 |
| 151 | + # Optional: Add metadata (labels, semver tags, sha) |
| 152 | + - name: Docker meta |
| 153 | + id: meta |
| 154 | + uses: docker/metadata-action@v5 |
| 155 | + with: |
| 156 | + images: ${{ secrets.DOCKER_USERNAME }}/eventify-api |
| 157 | + tags: | |
| 158 | + type=raw,value=latest |
| 159 | + type=sha |
| 160 | +
|
| 161 | + - name: Build and push (prod stage) |
| 162 | + uses: docker/build-push-action@v6 |
72 | 163 | with: |
73 | 164 | context: . |
74 | 165 | push: true |
75 | | - tags: ${{ secrets.DOCKER_USERNAME }}/eventify-api:latest |
| 166 | + target: prod # <- uses the prod stage from the Dockerfile I provided |
| 167 | + platforms: linux/amd64 |
| 168 | + tags: ${{ steps.meta.outputs.tags }} |
| 169 | + labels: ${{ steps.meta.outputs.labels }} |
| 170 | + cache-from: type=gha |
| 171 | + cache-to: type=gha,mode=max |
76 | 172 |
|
77 | | - # Log out from Docker Hub |
78 | | - - name: Log out from Docker Hub |
| 173 | + - name: Logout Docker Hub |
79 | 174 | run: docker logout |
80 | | - |
|
0 commit comments