-
Notifications
You must be signed in to change notification settings - Fork 0
154 lines (127 loc) · 5.19 KB
/
ci-cd-pipeline.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
144
145
146
147
148
149
150
151
152
153
154
name: Go CI/CD
on:
pull_request:
types: [opened, synchronize, reopened]
branches:
- main
push:
branches:
- main
permissions:
contents: write
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
jobs:
ci:
name: CI Pipeline
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: ^1.22.1
- name: Install dependencies
run: go mod download
- name: Build
run: go build -o ./app .
- name: Format code with gofumpt
run: go install mvdan.cc/gofumpt@latest && gofumpt -w .
- name: Install golangci-lint
run: curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest
- name: Run golangci-lint
run: |
OUTPUT=$(golangci-lint run ./... 2>&1) || true
if [[ -n "$OUTPUT" ]]; then
echo "golangci-lint found issues:"
echo "$OUTPUT"
fi
- name: Install go-staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
- name: Run go-staticcheck
run: |
OUTPUT=$(staticcheck ./... 2>&1) || true
if [[ -n "$OUTPUT" ]]; then
echo "golangci-lint found issues:"
echo "$OUTPUT"
fi
- name: Install gosec
run: go install github.com/securego/gosec/cmd/gosec@latest
- name: Run gosec
run: |
OUTPUT=$(gosec -exclude=G104 ./... 2>&1) || true
if [[ -n "$OUTPUT" ]]; then
echo "golangci-lint found issues:"
echo "$OUTPUT"
fi
- name: Test
run: go test ./...
build-and-deploy:
name: CD Pipeline - Continuous Delivery Pipeline
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- name: Set short git commit SHA
id: commit
uses: prompt/actions-commit-hash@v2
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Set up AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ vars.AWS_REGION }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Build, tag, and push Docker image to Amazon ECR
env:
ECR_REPOSITORY: ${{ vars.SERVICE_NAME }}
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE_TAG: ${{ steps.commit.outputs.short }}
run: |
IMAGE_URI="$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
docker build -t $IMAGE_URI .
docker push $IMAGE_URI
echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_ENV
- name: Update Kubernetes configuration
env:
SERVICE_NAME: ${{ vars.SERVICE_NAME }}
JWT_SECRET: ${{ secrets.JWT_SECRET }}
JWT_ISSUER: ${{ secrets.JWT_ISSUER }}
run: |
DB_NAME=$(aws ssm get-parameter --name "/$SERVICE_NAME/db_name" --with-decryption --output json | jq '.Parameter | .Value')
DB_HOST=$(aws ssm get-parameter --name "/$SERVICE_NAME/db_host" --with-decryption --output json | jq '.Parameter | .Value')
DB_USERNAME=$(aws ssm get-parameter --name "/$SERVICE_NAME/db_username" --with-decryption --output json | jq '.Parameter | .Value')
DB_PASSWORD=$(aws ssm get-parameter --name "/$SERVICE_NAME/db_password" --with-decryption --output json | jq '.Parameter | .Value')
sed -i 's|placeholder_repository_name|'"$IMAGE_URI"'|' ./infra/golang-app-deployment.yaml
sed -i 's|aws_ssm_db_name|'"$DB_NAME"'|' ./infra/configmap.yaml
sed -i 's|aws_ssm_db_host|'"$DB_HOST"'|' ./infra/configmap.yaml
sed -i 's|aws_ssm_db_username|'"$DB_USERNAME"'|' ./infra/secrets.yaml
sed -i 's|aws_ssm_db_password|'"$DB_PASSWORD"'|' ./infra/secrets.yaml
sed -i 's|git_hub_secrets_jwt_secret|'"$JWT_SECRET"'|' ./infra/secrets.yaml
sed -i 's|git_hub_secrets_jwt_issuer|'"$JWT_ISSUER"'|' ./infra/secrets.yaml
- name: Install kubectl
run: |
curl -LO "https://dl.k8s.io/release/$(curl -sSL https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x kubectl
sudo mv kubectl /usr/local/bin/
- name: Update kube config
env:
AWS_EKS_CLUSTER_NAME: ${{ vars.AWS_EKS_CLUSTER_NAME }}
AWS_REGION: ${{ vars.AWS_REGION }}
run: aws eks update-kubeconfig --name $AWS_EKS_CLUSTER_NAME --region $AWS_REGION
- name: Deploy to Kubernetes
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
run: |
kubectl config get-contexts
kubectl apply -f ./infra --validate=false
kubectl rollout status deployment/customer-service