diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..16a6b7c --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,61 @@ +name: Continuous Deployment to Cloud Run + +on: + push: + branches: + - main + +permissions: + contents: read + id-token: write + +env: + PROJECT_ID: project-452e74c0-919a-47bd-903 + REGION: europe-north1 + REPOSITORY: devops-repo + SERVICE_NAME: devops-app + WIF_PROVIDER: projects/330724336363/locations/global/workloadIdentityPools/github-pool/providers/github-provider + WIF_SERVICE_ACCOUNT: github-cd-deployer@project-452e74c0-919a-47bd-903.iam.gserviceaccount.com + +jobs: + deploy: + name: Build, Push, and Deploy + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Authenticate to Google Cloud (Workload Identity) + id: auth + uses: google-github-actions/auth@v2 + with: + workload_identity_provider: ${{ env.WIF_PROVIDER }} + service_account: ${{ env.WIF_SERVICE_ACCOUNT }} + + - name: Set up Cloud SDK + uses: google-github-actions/setup-gcloud@v2 + + - name: Configure Docker authentication for Artifact Registry + run: | + gcloud auth configure-docker ${{ env.REGION }}-docker.pkg.dev --quiet + + - name: Build and Push Docker image + run: | + IMAGE_URI="${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/app:${{ github.sha }}" + docker build --platform linux/amd64 -t "$IMAGE_URI" -t "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/app:latest" . + docker push "$IMAGE_URI" + docker push "${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/app:latest" + + - name: Deploy to Cloud Run + id: deploy + uses: google-github-actions/deploy-cloudrun@v2 + with: + service: ${{ env.SERVICE_NAME }} + region: ${{ env.REGION }} + image: ${{ env.REGION }}-docker.pkg.dev/${{ env.PROJECT_ID }}/${{ env.REPOSITORY }}/app:${{ github.sha }} + flags: '--port=8000' + + - name: Output Service URL + run: | + echo "Deployment successful! Service URL: ${{ steps.deploy.outputs.url }}" \ No newline at end of file