Skip to content

Commit feaa57e

Browse files
Add reusable .NET CI/CD workflow and onboard content-service (#2572)
* Add reusable .NET CI/CD workflow and onboard content-service * fix: passing OPENSHIFT_TOKEN explicitly in reusable workflow callers * Revert: restore folder-collection and api-services workflows from folder-collection branch * fix: add continue-on-error to deployment report and remove redundant || true in reusable workflow --------- Co-authored-by: Veerendra96-k <kveerendra6896@gmail.com>
1 parent 08592fc commit feaa57e

File tree

2 files changed

+298
-0
lines changed

2 files changed

+298
-0
lines changed
Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
name: Reusable .NET CI/CD
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
image_name:
7+
required: true
8+
type: string
9+
csproj_path:
10+
required: true
11+
type: string
12+
dockerfile:
13+
required: true
14+
type: string
15+
component:
16+
required: true
17+
type: string
18+
deployment_name:
19+
required: true
20+
type: string
21+
kustomize_dev_path:
22+
required: true
23+
type: string
24+
kustomize_test_path:
25+
required: true
26+
type: string
27+
dev_branch:
28+
required: false
29+
type: string
30+
default: ''
31+
rollout_timeout:
32+
required: false
33+
type: string
34+
default: '180s'
35+
continue_on_error_verify:
36+
required: false
37+
type: boolean
38+
default: false
39+
health_check_method:
40+
required: false
41+
type: string
42+
default: 'exec_curl'
43+
secrets:
44+
OPENSHIFT_TOKEN:
45+
required: true
46+
47+
permissions:
48+
contents: read
49+
50+
env:
51+
OPENSHIFT_SERVER: https://api.silver.devops.gov.bc.ca:6443
52+
OPENSHIFT_TOOLS_NAMESPACE: 9b301c-tools
53+
54+
jobs:
55+
ci:
56+
name: CI — Validate (.NET 9)
57+
runs-on: ubuntu-latest
58+
steps:
59+
- uses: actions/checkout@v4
60+
61+
- name: Setup .NET 9
62+
uses: actions/setup-dotnet@v5
63+
with:
64+
dotnet-version: 9.x
65+
66+
- name: Restore
67+
run: dotnet restore "${{ inputs.csproj_path }}"
68+
69+
- name: Check vulnerable packages
70+
run: dotnet list "${{ inputs.csproj_path }}" package --vulnerable --include-transitive
71+
72+
- name: Lint
73+
run: dotnet format "${{ inputs.csproj_path }}" --verify-no-changes --verbosity diagnostic
74+
75+
- name: Build
76+
run: dotnet build "${{ inputs.csproj_path }}" --configuration Release --no-restore
77+
78+
cd-dev:
79+
name: CD — Deploy to Dev
80+
runs-on: ubuntu-latest
81+
needs: ci
82+
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && (github.ref == 'refs/heads/dev' || (inputs.dev_branch != '' && github.ref == format('refs/heads/{0}', inputs.dev_branch))) && github.repository == 'bcgov/tno'
83+
environment: dev
84+
env:
85+
IMAGE_TAG: dev
86+
OPENSHIFT_NAMESPACE: 9b301c-dev
87+
steps:
88+
- uses: actions/checkout@v4
89+
90+
- name: Build Docker Image
91+
run: |
92+
docker build \
93+
-f ${{ inputs.dockerfile }} \
94+
-t ${{ inputs.image_name }}:${{ github.sha }} \
95+
.
96+
97+
- name: Login to OpenShift Image Registry
98+
run: |
99+
echo "${{ secrets.OPENSHIFT_TOKEN }}" | docker login \
100+
image-registry.apps.silver.devops.gov.bc.ca \
101+
-u serviceaccount \
102+
--password-stdin
103+
104+
- name: Push Image to OpenShift Registry
105+
run: |
106+
docker tag \
107+
${{ inputs.image_name }}:${{ github.sha }} \
108+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ env.IMAGE_TAG }}
109+
docker push \
110+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ env.IMAGE_TAG }}
111+
docker tag \
112+
${{ inputs.image_name }}:${{ github.sha }} \
113+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ github.sha }}
114+
docker push \
115+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ github.sha }}
116+
117+
- name: Install oc CLI
118+
uses: redhat-actions/openshift-tools-installer@v1
119+
with:
120+
oc: latest
121+
122+
- name: Login to OpenShift
123+
run: |
124+
oc login \
125+
--token=${{ secrets.OPENSHIFT_TOKEN }} \
126+
--server=${{ env.OPENSHIFT_SERVER }}
127+
128+
- name: Deploy via Kustomize
129+
run: oc apply -k ${{ inputs.kustomize_dev_path }} -n ${{ env.OPENSHIFT_NAMESPACE }}
130+
131+
- name: Rollout Restart
132+
run: |
133+
oc rollout restart deployment/${{ inputs.deployment_name }} \
134+
-n ${{ env.OPENSHIFT_NAMESPACE }}
135+
136+
- name: Verify Deployment
137+
continue-on-error: ${{ inputs.continue_on_error_verify }}
138+
run: |
139+
oc rollout status deployment/${{ inputs.deployment_name }} \
140+
-n ${{ env.OPENSHIFT_NAMESPACE }} \
141+
--timeout=${{ inputs.rollout_timeout }}
142+
143+
- name: Deployment Report
144+
if: always()
145+
continue-on-error: true
146+
env:
147+
HEALTH_CHECK_METHOD: ${{ inputs.health_check_method }}
148+
run: |
149+
echo "--- pod status ---"
150+
oc get pods -l component=${{ inputs.component }} -n ${{ env.OPENSHIFT_NAMESPACE }}
151+
echo "--- health check ---"
152+
POD=$(oc get pod -l component=${{ inputs.component }} \
153+
-n ${{ env.OPENSHIFT_NAMESPACE }} \
154+
--sort-by=.metadata.creationTimestamp \
155+
-o jsonpath='{.items[-1].metadata.name}')
156+
if [ "$HEALTH_CHECK_METHOD" = "port_forward" ]; then
157+
oc port-forward $POD 8080:8080 -n ${{ env.OPENSHIFT_NAMESPACE }} &
158+
sleep 10
159+
curl -sf http://localhost:8080/health
160+
kill %1
161+
else
162+
oc exec $POD -n ${{ env.OPENSHIFT_NAMESPACE }} -- \
163+
curl -sf http://localhost:8080/health
164+
fi
165+
echo "--- recent logs ---"
166+
sleep 60
167+
oc logs $POD -n ${{ env.OPENSHIFT_NAMESPACE }} --tail=20
168+
169+
cd-test:
170+
name: CD — Deploy to Test
171+
runs-on: ubuntu-latest
172+
needs: ci
173+
if: (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && github.ref == 'refs/heads/master' && github.repository == 'bcgov/tno'
174+
environment: test
175+
env:
176+
IMAGE_TAG: test
177+
OPENSHIFT_NAMESPACE: 9b301c-test
178+
steps:
179+
- uses: actions/checkout@v4
180+
181+
- name: Build Docker Image
182+
run: |
183+
docker build \
184+
-f ${{ inputs.dockerfile }} \
185+
-t ${{ inputs.image_name }}:${{ github.sha }} \
186+
.
187+
188+
- name: Login to OpenShift Image Registry
189+
run: |
190+
echo "${{ secrets.OPENSHIFT_TOKEN }}" | docker login \
191+
image-registry.apps.silver.devops.gov.bc.ca \
192+
-u serviceaccount \
193+
--password-stdin
194+
195+
- name: Push Image to OpenShift Registry
196+
run: |
197+
docker tag \
198+
${{ inputs.image_name }}:${{ github.sha }} \
199+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ env.IMAGE_TAG }}
200+
docker push \
201+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ env.IMAGE_TAG }}
202+
docker tag \
203+
${{ inputs.image_name }}:${{ github.sha }} \
204+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ github.sha }}
205+
docker push \
206+
image-registry.apps.silver.devops.gov.bc.ca/${{ env.OPENSHIFT_TOOLS_NAMESPACE }}/${{ inputs.image_name }}:${{ github.sha }}
207+
208+
- name: Install oc CLI
209+
uses: redhat-actions/openshift-tools-installer@v1
210+
with:
211+
oc: latest
212+
213+
- name: Login to OpenShift
214+
run: |
215+
oc login \
216+
--token=${{ secrets.OPENSHIFT_TOKEN }} \
217+
--server=${{ env.OPENSHIFT_SERVER }}
218+
219+
- name: Deploy via Kustomize
220+
run: oc apply -k ${{ inputs.kustomize_test_path }} -n ${{ env.OPENSHIFT_NAMESPACE }}
221+
222+
- name: Rollout Restart
223+
run: |
224+
oc rollout restart deployment/${{ inputs.deployment_name }} \
225+
-n ${{ env.OPENSHIFT_NAMESPACE }}
226+
227+
- name: Verify Deployment
228+
continue-on-error: ${{ inputs.continue_on_error_verify }}
229+
run: |
230+
oc rollout status deployment/${{ inputs.deployment_name }} \
231+
-n ${{ env.OPENSHIFT_NAMESPACE }} \
232+
--timeout=${{ inputs.rollout_timeout }}
233+
234+
- name: Deployment Report
235+
if: always()
236+
continue-on-error: true
237+
env:
238+
HEALTH_CHECK_METHOD: ${{ inputs.health_check_method }}
239+
run: |
240+
echo "--- pod status ---"
241+
oc get pods -l component=${{ inputs.component }} -n ${{ env.OPENSHIFT_NAMESPACE }}
242+
echo "--- health check ---"
243+
POD=$(oc get pod -l component=${{ inputs.component }} \
244+
-n ${{ env.OPENSHIFT_NAMESPACE }} \
245+
--sort-by=.metadata.creationTimestamp \
246+
-o jsonpath='{.items[-1].metadata.name}')
247+
if [ "$HEALTH_CHECK_METHOD" = "port_forward" ]; then
248+
oc port-forward $POD 8080:8080 -n ${{ env.OPENSHIFT_NAMESPACE }} &
249+
sleep 10
250+
curl -sf http://localhost:8080/health
251+
kill %1
252+
else
253+
oc exec $POD -n ${{ env.OPENSHIFT_NAMESPACE }} -- \
254+
curl -sf http://localhost:8080/health
255+
fi
256+
echo "--- recent logs ---"
257+
sleep 60
258+
oc logs $POD -n ${{ env.OPENSHIFT_NAMESPACE }} --tail=20
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: Content Service CI/CD
2+
3+
on:
4+
workflow_dispatch:
5+
pull_request:
6+
branches: [dev, master]
7+
paths:
8+
- services/net/content/**
9+
- libs/net/**
10+
- openshift/kustomize/services/content/**
11+
- .github/workflows/content-service-cicd.yml
12+
push:
13+
branches: [dev, master, content-service]
14+
paths:
15+
- services/net/content/**
16+
- libs/net/**
17+
- openshift/kustomize/services/content/**
18+
- .github/workflows/content-service-cicd.yml
19+
20+
concurrency:
21+
group: content-service-${{ github.workflow }}-${{ github.ref }}
22+
cancel-in-progress: true
23+
24+
jobs:
25+
pipeline:
26+
uses: ./.github/workflows/_reusable-dotnet-cicd.yml
27+
with:
28+
image_name: content-service
29+
csproj_path: services/net/content/TNO.Services.Content.csproj
30+
dockerfile: services/net/content/Dockerfile
31+
component: content-service
32+
deployment_name: content-service
33+
kustomize_dev_path: openshift/kustomize/services/content/overlays/dev
34+
kustomize_test_path: openshift/kustomize/services/content/overlays/test
35+
dev_branch: content-service
36+
rollout_timeout: '180s'
37+
continue_on_error_verify: false
38+
health_check_method: exec_curl
39+
secrets:
40+
OPENSHIFT_TOKEN: ${{ secrets.OPENSHIFT_TOKEN }}

0 commit comments

Comments
 (0)