This repository has been archived by the owner on May 3, 2023. It is now read-only.
generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 0
106 lines (97 loc) · 3.57 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
name: PR Close
on:
pull_request:
types:
- closed
paths-ignore:
- "**.md"
env:
REGISTRY: ghcr.io
NAME: nr-spar-oracle-api
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-latest
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 }}
# If merged, then handle any image promotion
image-backend:
name: Image Promotion
outputs:
build: ${{ steps.check.outputs.build }}
env:
COMPONENT: service-api
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'
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 Service API 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 }}
# Notify when PR merged and branch = main
merge-notification:
name: Merge Notification
runs-on: ubuntu-latest
if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'main'
steps:
- name: Pre-merge update
uses: mshick/add-pr-comment@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
allow-repeats: false
message: |
Your Pull Request code is being promoted! Please follow the link below.
[Main Merge Workflow](https://github.com/${{ github.repository }}/actions/workflows/merge-main.yml)