-
Notifications
You must be signed in to change notification settings - Fork 2
155 lines (136 loc) · 5.39 KB
/
pr-close.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
155
name: Pull Request Close
on:
pull_request:
types:
- closed
env:
REGISTRY: ghcr.io
NAME: ${{ github.event.repository.name }}
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
# Clean up OpenShift when PR closed, no conditions
cleanup-openshift:
name: Cleanup OpenShift
runs-on: ubuntu-22.04
environment:
name: dev
steps:
- uses: actions/checkout@v3
- name: Remove OpenShift artifacts
run: |
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }}
# Remove old build runs, build pods and deployment pods
oc delete all,pvc,secret -l app=${{ env.NAME }}-${{ github.event.number }}
oc delete configmap ${{ env.NAME }}-${{ github.event.number }}-frontend
# If merged, then handle any image promotion
image-backend:
name: Backend Image Promotion
outputs:
build: ${{ steps.check.outputs.build }}
env:
COMPONENT: backend
PREV: ${{ github.event.number }}
ZONE: test
environment:
name: test
runs-on: ubuntu-22.04
if: github.event.pull_request.merged == true && (github.event.pull_request.base.ref == 'main' || github.event.pull_request.base.ref == 'dev')
steps:
- name: Check for image changes
id: check
run: |
# Vars
IMG_PREV="${{ env.REGISTRY }}/${{ github.repository }}:${{ env.PREV }}-${{ env.COMPONENT }}"
IMG_ZONE="${{ env.REGISTRY }}/${{ github.repository }}:${{ env.ZONE }}-${{ env.COMPONENT }}"
# Make sure an image exists to promote; grab SHA
if [[ ! $(docker pull "${IMG_PREV}") ]]; then
echo -e "\n No images to promote"
exit 0
fi
SHA_PREV=$(docker inspect -f '{{.Id}}' "${IMG_PREV}")
# Use blank SHA for promoted image, unless a real one exists instead
SHA_ZONE=""
if [[ $(docker pull "${IMG_ZONE}") ]]; then
SHA_ZONE=$(docker inspect -f '{{.Id}}' "${IMG_ZONE}")
fi
# Output SHAs
echo -e "\n${IMG_PREV}: ${SHA_PREV}"
echo -e "${IMG_ZONE}: ${SHA_ZONE}\n"
# If different, then trigger updates
if [[ "${SHA_PREV}" != "${SHA_ZONE}" ]]; then
echo "::set-output name=build::true"
echo "Image has changed"
# Login to OpenShift and select project
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }}
oc delete is/${{ env.NAME }}-${{ env.ZONE}}-${{ env.COMPONENT }} || true
exit 0
fi
echo "Image promotion not required"
- name: Promote Backend Image
if: steps.check.outputs.build == 'true'
uses: shrink/actions-docker-registry-tag@v3
with:
registry: ${{ env.REGISTRY }}
repository: ${{ github.repository }}
token: ${{ secrets.GHCR_TOKEN }}
target: ${{ env.PREV }}-${{ env.COMPONENT }}
tags: |
${{ env.ZONE }}-${{ env.COMPONENT }}
# If merged, then handle any image promotion
image-frontend:
name: Frontend Image Promotion
outputs:
build: ${{ steps.check.outputs.build }}
env:
COMPONENT: frontend
PREV: ${{ github.event.number }}
ZONE: test
environment:
name: test
runs-on: ubuntu-22.04
if: github.event.pull_request.merged == true && (github.event.pull_request.base.ref == 'main' || github.event.pull_request.base.ref == 'dev')
steps:
- name: Check for image changes
id: check
run: |
# Vars
IMG_PREV="${{ env.REGISTRY }}/${{ github.repository }}:${{ env.PREV }}-${{ env.COMPONENT }}"
IMG_ZONE="${{ env.REGISTRY }}/${{ github.repository }}:${{ env.ZONE }}-${{ env.COMPONENT }}"
# Make sure an image exists to promote; grab SHA
if [[ ! $(docker pull "${IMG_PREV}") ]]; then
echo -e "\n No images to promote"
exit 0
fi
SHA_PREV=$(docker inspect -f '{{.Id}}' "${IMG_PREV}")
# Use blank SHA for promoted image, unless a real one exists instead
SHA_ZONE=""
if [[ $(docker pull "${IMG_ZONE}") ]]; then
SHA_ZONE=$(docker inspect -f '{{.Id}}' "${IMG_ZONE}")
fi
# Output SHAs
echo -e "\n${IMG_PREV}: ${SHA_PREV}"
echo -e "${IMG_ZONE}: ${SHA_ZONE}\n"
# If different, then trigger updates
if [[ "${SHA_PREV}" != "${SHA_ZONE}" ]]; then
echo "::set-output name=build::true"
echo "Image has changed"
# Login to OpenShift and select project
oc login --token=${{ secrets.OC_TOKEN }} --server=${{ secrets.OC_SERVER }}
oc project ${{ secrets.OC_NAMESPACE }}
oc delete is/${{ env.NAME }}-${{ env.ZONE}}-${{ env.COMPONENT }} || true
exit 0
fi
echo "Image promotion not required"
- name: Promote Backend Image
if: steps.check.outputs.build == 'true'
uses: shrink/actions-docker-registry-tag@v3
with:
registry: ${{ env.REGISTRY }}
repository: ${{ github.repository }}
target: ${{ env.PREV }}-${{ env.COMPONENT }}
tags: |
${{ env.ZONE }}-${{ env.COMPONENT }}