Merge branch 'main' of github.com:visheshgubrani/url-shortner #12
This file contains 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: | |
branches: main | |
paths-ignore: | |
- 'helm/**' | |
- 'README.md' | |
- '**.md' | |
- '.gitignore' | |
env: | |
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }} | |
REGISTRY: asia-south1-docker.pkg.dev | |
IMAGE_TAG: ${{github.sha}} | |
DOCKER_BUILDKIT: 1 # Enable BuildKit for faster builds | |
NEXT_PUBLIC_API_URL: https://shrtnn.xyz/api/shorten | |
jobs: | |
build-and-push: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: 'read' | |
id-token: 'write' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
token: ${{secrets.GH_TOKEN}} | |
fetch-depth: 0 | |
- name: SonarQube Scan | |
uses: sonarsource/sonarqube-scan-action@v4 | |
env: | |
SONAR_TOKEN: ${{secrets.SONAR_TOKEN}} | |
SONAR_HOST_URL: ${{secrets.SONAR_HOST_URL}} | |
with: | |
args: > | |
-Dsonar.projectKey=url-shortener | |
- name: Run Trivy filesystem scan | |
uses: aquasecurity/trivy-action@master | |
with: | |
scan-type: 'fs' | |
scan-ref: '.' | |
skip-dirs: 'node_modules' | |
format: 'table' | |
exit-code: 0 | |
severity: 'CRITICAL,HIGH' | |
- name: Setup Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- id: 'auth' | |
uses: 'google-github-actions/auth@v2' | |
with: | |
create_credentials_file: true | |
workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER }} | |
service_account: ${{ secrets.GCP_SA_EMAIL }} | |
- name: 'Set up Cloud SDK' | |
uses: 'google-github-actions/setup-gcloud@v2' | |
with: | |
version: '>= 363.0.0' | |
project_id: ${{ env.PROJECT_ID }} | |
- name: Configure Docker for Artificat Registry | |
run: | | |
gcloud auth configure-docker ${{ env.REGISTRY }} --quiet | |
# Cache Docker layers for faster builds | |
- name: Cache Docker layers | |
uses: actions/cache@v3 | |
with: | |
path: /tmp/.buildx-cache | |
key: ${{runner.os}}-buildx-${{github.ref_name}}-${{hashFiles('**/package.json', '**/package-lock.json', '**/Dockerfile')}} | |
restore-keys: | | |
${{runner.os}}-buildx-${{github.ref_name}}- | |
${{runner.os}}-buildx- | |
- name: Build and push shortner image | |
uses: docker/build-push-action@v6 | |
with: | |
context: ./shortner-service | |
push: true | |
tags: ${{env.REGISTRY}}/${{env.PROJECT_ID}}/shortner-service/shortner-service:${{env.IMAGE_TAG}} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache-new | |
builder: ${{steps.buildx.outputs.name}} | |
- name: Run Trivy vulnerability scanner | |
uses: aquasecurity/[email protected] | |
with: | |
image-ref: '${{env.REGISTRY}}/${{env.PROJECT_ID}}/shortner-service/shortner-service:${{env.IMAGE_TAG}}' | |
format: 'table' | |
exit-code: '0' | |
ignore-unfixed: true | |
vuln-type: 'os,library' | |
severity: 'HIGH' | |
- name: Build and push analytics image | |
uses: docker/build-push-action@v6 | |
with: | |
context: ./analytics-service | |
push: true | |
tags: ${{env.REGISTRY}}/${{env.PROJECT_ID}}/analytics-service/analytics-service:${{env.IMAGE_TAG}} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache-new | |
builder: ${{ steps.buildx.outputs.name }} | |
- name: Run Trivy vulnerability scanner | |
uses: aquasecurity/[email protected] | |
with: | |
image-ref: '${{env.REGISTRY}}/${{env.PROJECT_ID}}/analytics-service/analytics-service:${{env.IMAGE_TAG}}' | |
format: 'table' | |
exit-code: '0' | |
ignore-unfixed: true | |
vuln-type: 'os,library' | |
severity: 'HIGH' | |
- name: Build and push frontend image | |
uses: docker/build-push-action@v6 | |
with: | |
context: ./frontend-service | |
push: true | |
build-args: | | |
NEXT_PUBLIC_API_URL=${{ env.NEXT_PUBLIC_API_URL }} | |
tags: ${{env.REGISTRY}}/${{env.PROJECT_ID}}/frontend-service/frontend-service:${{env.IMAGE_TAG}} | |
cache-from: type=local,src=/tmp/.buildx-cache | |
cache-to: type=local,dest=/tmp/.buildx-cache-new | |
builder: ${{ steps.buildx.outputs.name }} | |
- name: Run Trivy vulnerability scanner | |
uses: aquasecurity/[email protected] | |
with: | |
image-ref: '${{env.REGISTRY}}/${{env.PROJECT_ID}}/frontend-service/frontend-service:${{env.IMAGE_TAG}}' | |
format: 'table' | |
exit-code: '0' | |
ignore-unfixed: true | |
vuln-type: 'os,library' | |
severity: 'HIGH' | |
- name: Move cache | |
run: | | |
rm -rf /tmp/.buildx-cache | |
mv /tmp/.buildx-cache-new /tmp/.buildx-cache | |
- name: Update Helm Values | |
run: | | |
yq e '.frontend.image.tag = "${{ env.IMAGE_TAG }}"' -i ./helm/values.yaml | |
yq e '.shortner.image.tag = "${{ env.IMAGE_TAG }}"' -i ./helm/values.yaml | |
yq e '.analytics.image.tag = "${{ env.IMAGE_TAG }}"' -i ./helm/values.yaml | |
- name: Commit and push updated Helm values | |
run: | | |
git config --global user.name 'GitHub Actions' | |
git config --global user.email '[email protected]' | |
git add ./helm/values.yaml | |
git commit -m "Update image tags to ${{ env.IMAGE_TAG }}" | |
git push | |
env: | |
GITHUB_TOKEN: ${{secrets.GH_TOKEN}} |