-
Notifications
You must be signed in to change notification settings - Fork 0
138 lines (121 loc) · 5.56 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
name: Deploy to Cloud Run
on:
workflow_dispatch:
inputs:
environment:
description: "Deployment environment (e.g., staging, production)"
required: true
default: "staging"
branch:
description: "Branch to deploy (e.g., main, develop)"
required: true
default: "main"
env:
REGION: ${{ secrets.REGION }}
PROJECT_ID: ${{ secrets.GCP_PROJECT_ID }}
jobs:
deploy:
runs-on: ubuntu-latest
steps:
# Step 1: Checkout the code from the target branch
- name: Checkout repository
uses: actions/checkout@v3
with:
ref: ${{ github.event.inputs.branch }}
# Step 2: Set SERVICE_NAME based on environment
- name: Set SERVICE_NAME
run: |
if [ "${{ github.event.inputs.environment }}" = "production" ]; then
echo "SERVICE_NAME=${{ secrets.PRODUCTION_SERVICE }}" >> $GITHUB_ENV
elif [ "${{ github.event.inputs.environment }}" = "staging" ]; then
echo "SERVICE_NAME=${{ secrets.STAGING_SERVICE }}" >> $GITHUB_ENV
else
echo "Invalid environment: ${{ github.event.inputs.environment }}. Valid values are 'production' or 'staging'." >&2
exit 1
fi
- name: Debug SERVICE_NAME
run: echo "SERVICE_NAME=${{ env.SERVICE_NAME }}"
# Step 3: Authenticate with Google Cloud
- name: Authenticate with Google Cloud
uses: google-github-actions/auth@v1
with:
project_id: ${{ env.PROJECT_ID }}
credentials_json: ${{secrets.GCP_SERVICE_ACCOUNT_KEY}}
# Step 4: Configure gcloud CLI
- name: Set up gcloud CLI
run: |
gcloud config set project ${{ secrets.GCP_PROJECT_ID }}
gcloud auth configure-docker
# Step 5: Get Git commit hash for versioning
- name: Get short Git commit hash
id: commit
run: |
echo "GIT_COMMIT_HASH=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
# Step 6: Build Docker image with versioning
- name: Build Docker image
run: |
IMAGE_NAME=gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
docker build -t $IMAGE_NAME:$IMAGE_TAG .
# Step 7: Push Docker image with tag to GCR
- name: Push Docker image to GCR
run: |
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
IMAGE_NAME=gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}
docker push $IMAGE_NAME:$IMAGE_TAG
# Step 8: Deploy to Cloud Run
- name: Deploy to Cloud Run
run: |
IMAGE_TAG="${{ github.event.inputs.branch }}-${{ github.sha }}"
gcloud run deploy ${{env.SERVICE_NAME}} \
--image gcr.io/${{ secrets.GCP_PROJECT_ID }}/${{env.SERVICE_NAME}}-${{ github.event.inputs.environment }}:$IMAGE_TAG \
--region ${{ env.REGION }} \
--platform managed \
--allow-unauthenticated \
--min-instances=1 \
--max-instances=100 \
--memory=512Mi \
--cpu=1000m \
--port=8080 \
--concurrency=80 \
--timeout=300s \
--service-account=780701254989-compute@developer.gserviceaccount.com \
--set-startup-cpu-boost \
--set-env-vars SPRING_DATA_MONGODB_URI=mongodb+srv://droiddumbledore:[email protected]/?retryWrites=true&w=majority&appName=QuashMagnusStage,SPRING_DATA_MONGODB_DATABASE=QuashMagnusStage,SPRING_PROFILES_ACTIVE=${{ github.event.inputs.environment }}
# Step 9: Verify the deployment
- name: Verify deployment
run: |
echo "Deployment complete. Access your service at:"
gcloud run services describe ${{env.SERVICE_NAME}} --region=${{ env.REGION }} --format='value(status.url)'
# Step 10: Clean Older Revisions
- name: Cleanup older revisions
run: |
SERVICE_NAME=${{env.SERVICE_NAME}}
REGION=${{ env.REGION }}
# List all revisions, sorted by creation timestamp in descending order
gcloud run revisions list \
--service=$SERVICE_NAME \
--region=$REGION \
--format="value(METADATA.name)" \
--sort-by=~CREATED_AT \
| tail -n +6 \
| xargs -I {} gcloud run revisions delete {} \
--region=$REGION --quiet
# Notify Success (Send Slack Message)
- name: Notify Slack - Success
if: success()
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{
"text": "✅ *Deployment Successful* \nService: `${{env.SERVICE_NAME}}` \nBranch: `${{ github.event.inputs.branch }}` \nEnvironment: `${{ github.event.inputs.environment }}` \n<https://console.cloud.google.com/run|View Cloud Run>"
}' \
${{ secrets.SLACK_WEBHOOK_URL }}
# Notify Failure (Send Slack Message)
- name: Notify Slack - Failure
if: failure()
run: |
curl -X POST -H 'Content-type: application/json' \
--data '{
"text": "❌ *Deployment Failed* \nService: `${{env.SERVICE_NAME}}` \nBranch: `${{ github.event.inputs.branch }}` \nEnvironment: `${{ github.event.inputs.environment }}` \nCheck the <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|GitHub Actions logs> for details."
}' \
${{ secrets.SLACK_WEBHOOK_URL }}