-
Notifications
You must be signed in to change notification settings - Fork 7
121 lines (102 loc) · 4.81 KB
/
cicd.yaml
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
name: Python CI/CD Pipeline with k8s
on:
push:
branches:
- '*'
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
env:
KUBE_CONFIG : ${{ secrets.KUBE_CONFIG }}
env_name: ${{startsWith(github.ref, 'refs/heads/release-') && 'prod' || 'dev'}}
api_host : ${{startsWith(github.ref, 'refs/heads/release-') && 'provisioner.zerofiltre.tech' || 'provisioner-dev.zerofiltre.tech'}}
DOCKER_TAG_SUFFIX: ${{startsWith(github.ref, 'refs/heads/release-') && 'stable' || 'unstable'}}
replicas : ${{startsWith(github.ref, 'refs/heads/release-') && 1 || 1 }}
requests_cpu : ${{startsWith(github.ref, 'refs/heads/release-') && 0.1 || 0.1 }}
requests_memory : ${{startsWith(github.ref, 'refs/heads/release-') && '0.5Gi' || '0.5Gi' }}
limits_cpu : ${{startsWith(github.ref, 'refs/heads/release-') && 0.5 || 0.5 }}
limits_memory : ${{startsWith(github.ref, 'refs/heads/release-') && '1.5Gi' || '1Gi' }}
domain_name : ${{startsWith(github.ref, 'refs/heads/release-') && 'provisioner.zerofiltre.tech' || 'provisioner-dev.zerofiltre.tech'}}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Docker Login
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Docker Login
uses: docker/[email protected]
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_PASSWORD }}
- name: Build and push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: imzerofiltre/zerofiltretech-provisioner:${{ github.run_number }}_${{ env.DOCKER_TAG_SUFFIX }}
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new
build-args: PROFILE=${{ env.env_name }}
- name: Set up kubectl
uses: tale/kubectl-action@v1
with:
base64-kube-config: ${{ secrets.KUBE_CONFIG }}
- name: Set up k8s manifest
run: |
envsubst < microservice.yaml > k8s.yml
cat k8s.yml
- name: Deploy to k8s
run: |
kubectl apply -f k8s.yml
kubectl set image deployment/zerofiltretech-provisioner-${{ env.env_name }} zerofiltretech-provisioner-${{ env.env_name }}=imzerofiltre/zerofiltretech-provisioner:${{ github.run_number }}_${{ env.DOCKER_TAG_SUFFIX }} -n zerofiltretech-${{ env.env_name }}
kubectl annotate deployment zerofiltretech-provisioner-${{ env.env_name }} kubernetes.io/change-cause="version change to: ${{ github.run_number }}_${{ env.DOCKER_TAG_SUFFIX }} " -n zerofiltretech-${{ env.env_name }} --overwrite=true
if ! kubectl rollout status -w deployment/zerofiltretech-provisioner-${{ env.env_name }} -n zerofiltretech-${{ env.env_name }}; then
kubectl rollout undo deployment.v1.apps/zerofiltretech-provisioner-${{ env.env_name }} -n zerofiltretech-${{ env.env_name }}
kubectl rollout status deployment/zerofiltretech-provisioner-${{ env.env_name }} -n zerofiltretech-${{ env.env_name }}
exit 1
fi
- name: Get commit author email
if: ${{ failure() }}
run: |
COMMIT_AUTHOR_EMAIL=$(git log -1 --pretty=format:'%ae')
echo "COMMIT_AUTHOR_EMAIL=$COMMIT_AUTHOR_EMAIL" >> $GITHUB_ENV
- name: Send email on failure
uses: dawidd6/[email protected]
with:
server_address: smtp.gmail.com
server_port: 587
username: ${{ secrets.EMAIL_USERNAME }}
password: ${{ secrets.EMAIL_PASSWORD }}
subject: "Workflow failed on ${{ github.repository }}/${{ github.head_ref }} (commit ${{ github.sha }})"
body: |
Workflow failed on ${{ github.repository }}/${{ github.head_ref }} (commit ${{ github.sha }}). \nSee the workflow run log for more details: https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}
from: "GitHub Actions <${{ vars.EMAIL_FROM }}>"
to: ${{ env.COMMIT_AUTHOR_EMAIL }}
if: ${{ failure() }}
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache