|
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 | | - |
81 | 1 | name: CI/CD pipeline for the eventify API |
82 | 2 |
|
83 | 3 | on: |
84 | 4 | push: |
85 | | - branches: [ master ] |
| 5 | + branches: |
| 6 | + - master |
86 | 7 | pull_request: |
87 | | - branches: [ master ] |
| 8 | + branches: |
| 9 | + - master |
88 | 10 |
|
89 | 11 | jobs: |
90 | 12 | build-and-test: |
91 | 13 | runs-on: ubuntu-latest |
92 | | - env: |
93 | | - NODE_ENV: test |
94 | | - NODE_OPTIONS: --max-old-space-size=256 |
| 14 | + |
95 | 15 | steps: |
96 | | - - name: Checkout |
97 | | - uses: actions/checkout@v4 |
| 16 | + # Checkout code from the repository |
| 17 | + - name: Checkout code |
| 18 | + uses: actions/checkout@v3 |
98 | 19 |
|
99 | | - - name: Use Node.js 20.x |
100 | | - uses: actions/setup-node@v4 |
| 20 | + # Set up Node.js environment |
| 21 | + - name: Set up Node.js |
| 22 | + uses: actions/setup-node@v3 |
101 | 23 | with: |
102 | 24 | 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 |
125 | 25 |
|
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% |
| 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 |
131 | 48 |
|
132 | 49 | dockerize: |
133 | 50 | runs-on: ubuntu-latest |
134 | 51 | needs: build-and-test |
135 | | - permissions: |
136 | | - contents: read |
137 | | - packages: write |
| 52 | + |
138 | 53 | steps: |
139 | | - - name: Checkout |
140 | | - uses: actions/checkout@v4 |
| 54 | + # Checkout code from the repository |
| 55 | + - name: Checkout code |
| 56 | + uses: actions/checkout@v3 |
141 | 57 |
|
| 58 | + # Set up Docker Buildx |
142 | 59 | - name: Set up Docker Buildx |
143 | | - uses: docker/setup-buildx-action@v3 |
| 60 | + uses: docker/setup-buildx-action@v2 |
144 | 61 |
|
| 62 | + # Log in to Docker Hub |
145 | 63 | - name: Log in to Docker Hub |
146 | | - uses: docker/login-action@v3 |
| 64 | + uses: docker/login-action@v2 |
147 | 65 | with: |
148 | 66 | username: ${{ secrets.DOCKER_USERNAME }} |
149 | 67 | password: ${{ secrets.DOCKER_PASSWORD }} |
150 | 68 |
|
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 |
| 69 | + # Build And Push Docker image |
| 70 | + - name: Build and push Docker image |
| 71 | + uses: docker/build-push-action@v4 |
163 | 72 | with: |
164 | 73 | context: . |
165 | 74 | push: true |
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 |
| 75 | + tags: ${{ secrets.DOCKER_USERNAME }}/eventify-api:latest |
172 | 76 |
|
173 | | - - name: Logout Docker Hub |
| 77 | + # Log out from Docker Hub |
| 78 | + - name: Log out from Docker Hub |
174 | 79 | run: docker logout |
| 80 | + |
0 commit comments