|
1 | | -name: CI/CD Pipeline |
2 | | - |
3 | | -on: |
4 | | - push: |
5 | | - branches: [main, develop] |
6 | | - pull_request: |
7 | | - branches: [main] |
8 | | - |
9 | | -jobs: |
10 | | - test: |
11 | | - name: Test |
12 | | - runs-on: ubuntu-latest |
13 | | - |
14 | | - services: |
15 | | - postgres: |
16 | | - image: postgres:15 |
17 | | - env: |
18 | | - POSTGRES_PASSWORD: postgres |
19 | | - POSTGRES_DB: auth_test |
20 | | - options: >- |
21 | | - --health-cmd pg_isready |
22 | | - --health-interval 10s |
23 | | - --health-timeout 5s |
24 | | - --health-retries 5 |
25 | | - ports: |
26 | | - - 5432:5432 |
27 | | - |
28 | | - redis: |
29 | | - image: redis:7 |
30 | | - options: >- |
31 | | - --health-cmd "redis-cli ping" |
32 | | - --health-interval 10s |
33 | | - --health-timeout 5s |
34 | | - --health-retries 5 |
35 | | - ports: |
36 | | - - 6379:6379 |
37 | | - |
38 | | - steps: |
39 | | - - name: Checkout code |
40 | | - uses: actions/checkout@v4 |
41 | | - |
42 | | - - name: Set up Go |
43 | | - uses: actions/setup-go@v4 |
44 | | - with: |
45 | | - go-version: "1.23" |
46 | | - |
47 | | - - name: Cache Go modules |
48 | | - uses: actions/cache@v3 |
49 | | - with: |
50 | | - path: ~/go/pkg/mod |
51 | | - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |
52 | | - restore-keys: | |
53 | | - ${{ runner.os }}-go- |
54 | | -
|
55 | | - - name: Download dependencies |
56 | | - run: go mod download |
57 | | - |
58 | | - - name: Run unit tests |
59 | | - env: |
60 | | - JWT_SECRET: testsecret |
61 | | - ACCESS_TOKEN_EXPIRATION_MINUTES: 15 |
62 | | - REFRESH_TOKEN_EXPIRATION_HOURS: 720 |
63 | | - DB_HOST: localhost |
64 | | - DB_PORT: 5432 |
65 | | - DB_USER: postgres |
66 | | - DB_PASSWORD: postgres |
67 | | - DB_NAME: auth_test |
68 | | - REDIS_ADDR: localhost:6379 |
69 | | - REDIS_PASSWORD: "" |
70 | | - REDIS_DB: 0 |
71 | | - run: go test -v -race -coverprofile=coverage.out ./... |
72 | | - |
73 | | - - name: Upload coverage to Codecov |
74 | | - uses: codecov/codecov-action@v3 |
75 | | - with: |
76 | | - file: ./coverage.out |
77 | | - flags: unittests |
78 | | - name: codecov-umbrella |
79 | | - |
80 | | - build: |
81 | | - name: Build |
82 | | - runs-on: ubuntu-latest |
83 | | - needs: test |
84 | | - |
85 | | - steps: |
86 | | - - name: Checkout code |
87 | | - uses: actions/checkout@v4 |
88 | | - |
89 | | - - name: Set up Go |
90 | | - uses: actions/setup-go@v4 |
91 | | - with: |
92 | | - go-version: "1.23" |
93 | | - |
94 | | - - name: Build application |
95 | | - run: go build -v ./cmd/api |
96 | | - |
97 | | - - name: Build Docker image |
98 | | - run: docker build -t auth-api:${{ github.sha }} . |
99 | | - |
100 | | - - name: Save Docker image |
101 | | - run: docker save auth-api:${{ github.sha }} | gzip > auth-api.tar.gz |
102 | | - |
103 | | - - name: Upload Docker image artifact |
104 | | - uses: actions/upload-artifact@v4 |
105 | | - with: |
106 | | - name: docker-image |
107 | | - path: auth-api.tar.gz |
108 | | - |
109 | | - security-scan: |
110 | | - name: Security Scan |
111 | | - runs-on: ubuntu-latest |
112 | | - needs: test |
113 | | - |
114 | | - steps: |
115 | | - - name: Checkout code |
116 | | - uses: actions/checkout@v4 |
117 | | - |
118 | | - - name: Set up Go |
119 | | - uses: actions/setup-go@v4 |
120 | | - with: |
121 | | - go-version: "1.23" |
122 | | - |
123 | | - - name: Run Gosec Security Scanner |
124 | | - run: | |
125 | | - go install github.com/securego/gosec/v2/cmd/gosec@latest |
126 | | - gosec ./... |
127 | | -
|
128 | | - - name: Run Nancy vulnerability scanner |
129 | | - run: | |
130 | | - go install github.com/sonatype-nexus-community/nancy@latest |
131 | | - go list -json -deps ./... | nancy sleuth |
132 | | -
|
133 | | - deploy-staging: |
134 | | - name: Deploy to Staging |
135 | | - runs-on: ubuntu-latest |
136 | | - needs: [test, build, security-scan] |
137 | | - if: github.ref == 'refs/heads/develop' |
138 | | - environment: staging |
139 | | - |
140 | | - steps: |
141 | | - - name: Checkout code |
142 | | - uses: actions/checkout@v4 |
143 | | - |
144 | | - - name: Download Docker image artifact |
145 | | - uses: actions/download-artifact@v4 |
146 | | - with: |
147 | | - name: docker-image |
148 | | - |
149 | | - - name: Load Docker image |
150 | | - run: docker load < auth-api.tar.gz |
151 | | - |
152 | | - - name: Deploy to staging |
153 | | - run: | |
154 | | - echo "Deploying to staging environment..." |
155 | | - # Here you would add your actual deployment commands |
156 | | - # For example, using kubectl, docker-compose, or cloud provider CLI |
157 | | - echo "Deployment completed successfully" |
158 | | -
|
159 | | - deploy-production: |
160 | | - name: Deploy to Production |
161 | | - runs-on: ubuntu-latest |
162 | | - needs: [test, build, security-scan] |
163 | | - if: github.ref == 'refs/heads/main' |
164 | | - environment: production |
165 | | - |
166 | | - steps: |
167 | | - - name: Checkout code |
168 | | - uses: actions/checkout@v4 |
169 | | - |
170 | | - - name: Download Docker image artifact |
171 | | - uses: actions/download-artifact@v4 |
172 | | - with: |
173 | | - name: docker-image |
174 | | - |
175 | | - - name: Load Docker image |
176 | | - run: docker load < auth-api.tar.gz |
177 | | - |
178 | | - - name: Deploy to production |
179 | | - run: | |
180 | | - echo "Deploying to production environment..." |
181 | | - # Here you would add your actual deployment commands |
182 | | - # For example, using kubectl, docker-compose, or cloud provider CLI |
183 | | - echo "Production deployment completed successfully" |
| 1 | +name: CI/CD Pipeline |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop] |
| 6 | + pull_request: |
| 7 | + branches: [main] |
| 8 | + |
| 9 | +jobs: |
| 10 | + test: |
| 11 | + name: Test |
| 12 | + runs-on: ubuntu-latest |
| 13 | + |
| 14 | + services: |
| 15 | + postgres: |
| 16 | + image: postgres:15 |
| 17 | + env: |
| 18 | + POSTGRES_PASSWORD: postgres |
| 19 | + POSTGRES_DB: auth_test |
| 20 | + options: >- |
| 21 | + --health-cmd pg_isready |
| 22 | + --health-interval 10s |
| 23 | + --health-timeout 5s |
| 24 | + --health-retries 5 |
| 25 | + ports: |
| 26 | + - 5435:5432 |
| 27 | + |
| 28 | + redis: |
| 29 | + image: redis:7 |
| 30 | + options: >- |
| 31 | + --health-cmd "redis-cli ping" |
| 32 | + --health-interval 10s |
| 33 | + --health-timeout 5s |
| 34 | + --health-retries 5 |
| 35 | + ports: |
| 36 | + - 6381:6379 |
| 37 | + |
| 38 | + steps: |
| 39 | + - name: Checkout code |
| 40 | + uses: actions/checkout@v4 |
| 41 | + |
| 42 | + - name: Set up Go |
| 43 | + uses: actions/setup-go@v4 |
| 44 | + with: |
| 45 | + go-version: "1.23" |
| 46 | + |
| 47 | + - name: Cache Go modules |
| 48 | + uses: actions/cache@v3 |
| 49 | + with: |
| 50 | + path: ~/go/pkg/mod |
| 51 | + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} |
| 52 | + restore-keys: | |
| 53 | + ${{ runner.os }}-go- |
| 54 | +
|
| 55 | + - name: Download dependencies |
| 56 | + run: go mod download |
| 57 | + |
| 58 | + - name: Run unit tests |
| 59 | + env: |
| 60 | + JWT_SECRET: testsecret |
| 61 | + ACCESS_TOKEN_EXPIRATION_MINUTES: 15 |
| 62 | + REFRESH_TOKEN_EXPIRATION_HOURS: 720 |
| 63 | + DB_HOST: localhost |
| 64 | + DB_PORT: 5435 |
| 65 | + DB_USER: postgres |
| 66 | + DB_PASSWORD: postgres |
| 67 | + DB_NAME: auth_test |
| 68 | + REDIS_ADDR: localhost:6381 |
| 69 | + REDIS_PASSWORD: "" |
| 70 | + REDIS_DB: 0 |
| 71 | + run: go test -v -race -coverprofile=coverage.out ./... |
| 72 | + |
| 73 | + - name: Upload coverage to Codecov |
| 74 | + uses: codecov/codecov-action@v3 |
| 75 | + with: |
| 76 | + file: ./coverage.out |
| 77 | + flags: unittests |
| 78 | + name: codecov-umbrella |
| 79 | + |
| 80 | + build: |
| 81 | + name: Build |
| 82 | + runs-on: ubuntu-latest |
| 83 | + needs: test |
| 84 | + |
| 85 | + steps: |
| 86 | + - name: Checkout code |
| 87 | + uses: actions/checkout@v4 |
| 88 | + |
| 89 | + - name: Set up Go |
| 90 | + uses: actions/setup-go@v4 |
| 91 | + with: |
| 92 | + go-version: "1.23" |
| 93 | + |
| 94 | + - name: Build application |
| 95 | + run: go build -v ./cmd/api |
| 96 | + |
| 97 | + - name: Build Docker image |
| 98 | + run: docker build -t auth-api:${{ github.sha }} . |
| 99 | + |
| 100 | + - name: Save Docker image |
| 101 | + run: docker save auth-api:${{ github.sha }} | gzip > auth-api.tar.gz |
| 102 | + |
| 103 | + - name: Upload Docker image artifact |
| 104 | + if: ${{ !env.ACT }} |
| 105 | + uses: actions/upload-artifact@v4 |
| 106 | + with: |
| 107 | + name: docker-image |
| 108 | + path: auth-api.tar.gz |
| 109 | + |
| 110 | + security-scan: |
| 111 | + name: Security Scan |
| 112 | + runs-on: ubuntu-latest |
| 113 | + needs: test |
| 114 | + |
| 115 | + steps: |
| 116 | + - name: Checkout code |
| 117 | + uses: actions/checkout@v4 |
| 118 | + |
| 119 | + - name: Set up Go |
| 120 | + uses: actions/setup-go@v4 |
| 121 | + with: |
| 122 | + go-version: "1.23" |
| 123 | + |
| 124 | + - name: Run Gosec Security Scanner |
| 125 | + run: | |
| 126 | + go install github.com/securego/gosec/v2/cmd/gosec@latest |
| 127 | + gosec ./... |
| 128 | +
|
| 129 | + - name: Run Nancy vulnerability scanner |
| 130 | + if: ${{ !env.ACT }} |
| 131 | + run: | |
| 132 | + go install github.com/sonatype-nexus-community/nancy@latest |
| 133 | + go list -json -deps ./... | nancy sleuth |
| 134 | +
|
| 135 | + deploy-staging: |
| 136 | + name: Deploy to Staging |
| 137 | + runs-on: ubuntu-latest |
| 138 | + needs: [test, build, security-scan] |
| 139 | + if: github.ref == 'refs/heads/develop' |
| 140 | + environment: staging |
| 141 | + |
| 142 | + steps: |
| 143 | + - name: Checkout code |
| 144 | + uses: actions/checkout@v4 |
| 145 | + |
| 146 | + - name: Download Docker image artifact |
| 147 | + if: ${{ !env.ACT }} |
| 148 | + uses: actions/download-artifact@v4 |
| 149 | + with: |
| 150 | + name: docker-image |
| 151 | + |
| 152 | + - name: Load Docker image |
| 153 | + if: ${{ !env.ACT }} |
| 154 | + run: docker load < auth-api.tar.gz |
| 155 | + |
| 156 | + - name: Deploy to staging |
| 157 | + run: | |
| 158 | + echo "Deploying to staging environment..." |
| 159 | + # Here you would add your actual deployment commands |
| 160 | + # For example, using kubectl, docker-compose, or cloud provider CLI |
| 161 | + echo "Deployment completed successfully" |
| 162 | +
|
| 163 | + deploy-production: |
| 164 | + name: Deploy to Production |
| 165 | + runs-on: ubuntu-latest |
| 166 | + needs: [test, build, security-scan] |
| 167 | + if: github.ref == 'refs/heads/main' |
| 168 | + environment: production |
| 169 | + |
| 170 | + steps: |
| 171 | + - name: Checkout code |
| 172 | + uses: actions/checkout@v4 |
| 173 | + |
| 174 | + - name: Download Docker image artifact |
| 175 | + if: ${{ !env.ACT }} |
| 176 | + uses: actions/download-artifact@v4 |
| 177 | + with: |
| 178 | + name: docker-image |
| 179 | + |
| 180 | + - name: Load Docker image |
| 181 | + if: ${{ !env.ACT }} |
| 182 | + run: docker load < auth-api.tar.gz |
| 183 | + |
| 184 | + - name: Deploy to production |
| 185 | + run: | |
| 186 | + echo "Deploying to production environment..." |
| 187 | + # Here you would add your actual deployment commands |
| 188 | + # For example, using kubectl, docker-compose, or cloud provider CLI |
| 189 | + echo "Production deployment completed successfully" |
0 commit comments