Fix/consumer issue #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: CI/CD Pipeline | |
| on: | |
| push: | |
| tags: | |
| - 'v*' | |
| pull_request: | |
| branches: | |
| - main | |
| - develop | |
| env: | |
| REGISTRY: ghcr.io | |
| IMAGE_NAME: ${{ github.repository }} | |
| jobs: | |
| test: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Go | |
| uses: actions/setup-go@v4 | |
| with: | |
| go-version: '1.24' | |
| - name: Cache Go modules | |
| uses: actions/cache@v3 | |
| with: | |
| path: | | |
| ~/.cache/go-build | |
| ~/go/pkg/mod | |
| key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} | |
| restore-keys: | | |
| ${{ runner.os }}-go- | |
| - name: Install dependencies | |
| run: | | |
| go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest | |
| go install github.com/pressly/goose/v3/cmd/goose@latest | |
| - name: Run tests for photo-svc | |
| run: | | |
| cd photo-svc | |
| go mod download | |
| go test ./... -v | |
| - name: Run tests for user-svc | |
| run: | | |
| cd user-svc | |
| go mod download | |
| go test ./... -v | |
| - name: Run tests for transaction-svc | |
| run: | | |
| cd transaction-svc | |
| go mod download | |
| go test ./... -v | |
| - name: Run tests for upload-svc | |
| run: | | |
| cd upload-svc | |
| go mod download | |
| go test ./... -v | |
| - name: Run tests for notification-svc | |
| run: | | |
| cd notification-svc | |
| go mod download | |
| go test ./... -v | |
| - name: Run linter | |
| run: | | |
| golangci-lint run --timeout=5m | |
| build-and-push: | |
| needs: test | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') | |
| strategy: | |
| matrix: | |
| service: [photo-svc, user-svc, transaction-svc, upload-svc, notification-svc] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Set up Docker Buildx | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Log in to Container Registry | |
| uses: docker/login-action@v3 | |
| with: | |
| registry: ${{ env.REGISTRY }} | |
| username: ${{ github.actor }} | |
| password: ${{ secrets.GITHUB_TOKEN }} | |
| - name: Extract metadata | |
| id: meta | |
| uses: docker/metadata-action@v5 | |
| with: | |
| images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-${{ matrix.service }} | |
| tags: | | |
| type=ref,event=tag | |
| type=sha,prefix={{branch}}- | |
| type=raw,value=latest,enable={{is_default_branch}} | |
| - name: Build and push Docker image | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: . | |
| file: ${{ matrix.service }}/Dockerfile | |
| push: true | |
| tags: ${{ steps.meta.outputs.tags }} | |
| labels: ${{ steps.meta.outputs.labels }} | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |
| deploy: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| if: github.event_name == 'push' && startsWith(github.ref, 'refs/tags/') | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Setup SSH | |
| uses: webfactory/ssh-agent@v0.7.0 | |
| with: | |
| ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| - name: Deploy to K3s | |
| run: | | |
| # Create deployment directory | |
| mkdir -p deployment | |
| # Copy deployment scripts | |
| cp scripts/deploy.sh deployment/ | |
| cp scripts/k8s/*.yaml deployment/ | |
| # Set executable permissions | |
| chmod +x deployment/deploy.sh | |
| # Deploy to VPS | |
| scp -o StrictHostKeyChecking=no -r deployment/* ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }}:/tmp/deployment/ | |
| # Execute deployment on VPS | |
| ssh -o StrictHostKeyChecking=no ${{ secrets.VPS_USER }}@${{ secrets.VPS_HOST }} << 'EOF' | |
| cd /tmp/deployment | |
| chmod +x deploy.sh | |
| ./deploy.sh ${{ github.ref_name }} | |
| EOF |