-
Notifications
You must be signed in to change notification settings - Fork 1
144 lines (123 loc) · 3.86 KB
/
build-deploy-hello-app-image.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
139
140
141
142
143
on:
push:
branches:
- gke-terraform-deploy
paths:
- 'apps/hello-app/**'
- 'infra/**'
- .github/**
env:
PROJECT_ID: "deployment-demo-311509"
PROJECT_NAME: "deployment-demo"
IMAGE: "hello-app"
TAG: "v1"
ENVIRONMENT: "dev"
APP_LINK: ""
VALIDATION_REPO: "ksiedlarek/gcp-deployment-validation"
TRIGGER_VALIDATION: true
jobs:
show-run-id:
name: Display run id
runs-on: ubuntu-latest
steps:
- name: Display run id
run: echo "${{ github.run_id }}"
setup-build-publish:
name: Container image - setup, build and publish
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
# Setup gcloud CLI
- uses: google-github-actions/[email protected]
with:
service_account_key: ${{ secrets.GCLOUD_SERVICE_KEY }}
project_id: ${{ env.PROJECT_ID }}
# Configure docker to use the gcloud command-line tool as a credential helper
- run: |-
gcloud --quiet auth configure-docker
# Build the Docker image
- name: Build
run: |-
docker build \
--tag "gcr.io/$PROJECT_ID/$IMAGE:$TAG" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" \
--file apps/hello-app/Dockerfile .
# Push the Docker image to Google Container Registry
- name: Publish
run: |-
docker push "gcr.io/$PROJECT_ID/$IMAGE:$TAG"
terraform-plan:
name: Terraform Plan
runs-on: ubuntu-latest
needs: setup-build-publish
defaults:
run:
working-directory: infra
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.15.0
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
- name: Terraform Init
id: init
run: terraform init -backend-config="config/$ENVIRONMENT/backend.tfvars"
- name: Terraform Plan
id: plan
run: terraform plan
continue-on-error: true
- name: Terraform Plan Status
if: steps.plan.outcome == 'failure'
run: exit 1
# - name: Terraform Apply
# if: github.ref == 'refs/heads/gke-terraform-deploy' && github.event_name == 'push'
# run: terraform apply -auto-approve
terraform-apply:
name: Terraform Apply
runs-on: ubuntu-latest
needs: terraform-plan
environment:
name: dev
defaults:
run:
working-directory: infra
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Terraform
uses: hashicorp/setup-terraform@v1
with:
terraform_version: 0.15.0
cli_config_credentials_token: ${{ secrets.TF_API_TOKEN }}
terraform_wrapper: false
- name: Terraform Init
id: init
run: terraform init -backend-config="config/$ENVIRONMENT/backend.tfvars"
- name: Terraform Apply
if: github.ref == 'refs/heads/gke-terraform-deploy' && github.event_name == 'push'
run: terraform apply -auto-approve
- name: Save output link
run: |
output=$(terraform output -raw lb_ip)
echo "$output"
echo "APP_LINK=$output" >> $GITHUB_ENV
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v1
if: ${{ env.TRIGGER_VALIDATION }}
with:
token: ${{ secrets.REPO_ACCESS_TOKEN }}
repository: ${{ env.VALIDATION_REPO }}
event-type: deployment-validation
client-payload: |
{
"ref": "${{ github.ref }}",
"sha": "${{ github.sha }}",
"project_name": "${{ env.PROJECT_NAME }}",
"app_link": "${{ env.APP_LINK }}",
"run_id": "${{ github.run_id }}",
"repo": "${{ github.repository }}"
}