-
-
Notifications
You must be signed in to change notification settings - Fork 3
110 lines (95 loc) · 3.94 KB
/
deploy.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
name: Deploy the image to Google Kubernetes Engine
on:
workflow_call:
secrets:
GCP_SA_JSON:
required: true
inputs:
BRANCH:
description: 'The branch to build for staging deployments'
default: main
type: string
ENABLE_PRODUCTION:
description: 'Deploy production builds based on tags'
default: true
type: boolean
env:
STG_PROJECT_ID: stg-pro-397609
# TODO: Replace with the prodcution project ID when that is available
PRD_PROJECT_ID: stg-pro-397609
ARTIFACT_REGISTRY_LOCATION: europe-west4-docker.pkg.dev
ARTIFACT_REGISTRY_NAME: pro-containers
jobs:
docker:
runs-on: ubuntu-latest
if: github.event_name == 'push' && (github.ref == 'refs/heads/${{ github.event.inputs.BRANCH }}' || (${{ github.event.inputs.ENABLE_PRODUCTION }} && startsWith(github.ref, 'refs/tags/')))
steps:
- name: Set environment (Staging)
if: github.ref == 'refs/heads/${{ github.event.inputs.BRANCH }}'
run: |
echo "ENVIRONMENT=staging" >> $GITHUB_ENV
- name: Set environment (Production)
if: startsWith(github.ref, 'refs/tags/')
run: |
echo "ENVIRONMENT=production" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v3
- name: Check for folder existence
id: checkfolder
run: |
if [ -d "./k8s/overlays/${{ env.ENVIRONMENT }}" ]; then
echo "::set-output name=manifests_exist::true"
else
echo "::set-output name=manifests_exist::false"
fi
- name: Set project id
if: steps.checkfolder.outputs.manifests_exist == 'true'
run: |
if [[ "${{ env.ENVIRONMENT }}" == "production" ]]; then
echo "PROJECT_ID=${{ env.PRD_PROJECT_ID }}" >> $GITHUB_ENV
elif [[ "${{ env.ENVIRONMENT }}" == "staging" ]]; then
echo "PROJECT_ID=${{ env.STG_PROJECT_ID }}" >> $GITHUB_ENV
fi
- name: 'Authenticate to Google Cloud'
if: steps.checkfolder.outputs.manifests_exist == 'true'
uses: 'google-github-actions/auth@v1'
with:
# TODO: Use different secret for production and staging
credentials_json: ${{ secrets.GCP_SA_JSON }}
- name: Set up Google Cloud SDK
if: steps.checkfolder.outputs.manifests_exist == 'true'
uses: google-github-actions/[email protected]
with:
project_id: ${{ env.PROJECT_ID }}
- name: Authenticate with GKE
if: steps.checkfolder.outputs.manifests_exist == 'true'
uses: 'google-github-actions/get-gke-credentials@v1'
with:
cluster_name: 'ghost-pro'
location: 'europe-west4-a'
- name: Build final manifest
if: steps.checkfolder.outputs.manifests_exist == 'true'
run: |
kubectl kustomize ./k8s/overlays/${{ env.ENVIRONMENT }} > /tmp/built-manifest.yaml
- name: Setup yq
if: steps.checkfolder.outputs.manifests_exist == 'true'
uses: supplypike/setup-bin@v3
with:
uri: 'https://github.com/mikefarah/yq/releases/download/v4.35.1/yq_linux_amd64'
name: 'yq'
version: '4.35.1'
- name: Extract Deployment Name and Namespace
if: steps.checkfolder.outputs.manifests_exist == 'true'
run: |
deployment_name=$(yq e 'select(.kind == "Deployment") | .metadata.name' /tmp/built-manifest.yaml)
namespace=$(yq e 'select(.kind == "Deployment") | .metadata.namespace' /tmp/built-manifest.yaml | sed 's/^null$/default/')
namespace=${namespace:-default}
echo "Deployment Name: $deployment_name"
echo "Namespace: $namespace"
echo "DEPLOYMENT_NAME=$deployment_name" >> $GITHUB_ENV
echo "NAMESPACE=$namespace" >> $GITHUB_ENV
- name: Apply the manifests to the Kubernetes cluster
if: steps.checkfolder.outputs.manifests_exist == 'true'
run: |
kubectl apply -k k8s/overlays/${{ env.ENVIRONMENT }}
kubectl rollout restart deployment ${{ env.DEPLOYMENT_NAME }} --namespace ${{ env.NAMESPACE }}