-
Notifications
You must be signed in to change notification settings - Fork 1
129 lines (93 loc) · 4.08 KB
/
build-push-webapi.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
name: diam-webapi
on:
push:
branches: [develop,test,main]
paths:
- "backend/webapi/**"
- "backend/common/**"
- ".github/workflows/build-push-webapi.yml"
workflow_dispatch:
env:
IMAGE_NAME: diam-webapi
WORKING_DIRECTORY: ./backend
BRANCH_NAME: develop
VALUES_FILE: dev
jobs:
build:
runs-on: ubuntu-latest
if: github.event_name == 'push'
steps:
- uses: actions/checkout@v4
- name: Set environment for branch
run: |
if [[ ${{ github.ref_name }} == 'main' ]]; then
echo "BRANCH_NAME=main" >> "$GITHUB_ENV"
echo "VALUES_FILE=prod" >> "$GITHUB_ENV"
fi
if [[ ${{ github.ref_name }} == 'test' ]]; then
echo "BRANCH_NAME=test" >> "$GITHUB_ENV"
echo "VALUES_FILE=test" >> "$GITHUB_ENV"
fi
if [[ ${{ github.ref_name }} == 'develop' ]]; then
echo "BRANCH_NAME=develop" >> "$GITHUB_ENV"
echo "VALUES_FILE=dev" >> "$GITHUB_ENV"
fi
- name: Extract branch name
shell: bash
run: echo "branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}" >> $GITHUB_OUTPUT
id: extract_branch
- name: Branch name
run: echo running on branch ${GITHUB_REF##*/}
- name: Login to Artifactory
uses: docker/login-action@v1
with:
registry: artifacts.developer.gov.bc.ca
username: ${{ secrets.ARTIFACTORY_USERNAME }}
password: ${{ secrets.ARTIFACTORY_PASSWORD }}
- name: Build Image
working-directory: ${{env.WORKING_DIRECTORY}}
run: |
docker build -t artifacts.developer.gov.bc.ca/de27-general-docker/${{env.IMAGE_NAME}}:${GITHUB_REF##*/} -f Dockerfile.WebApi .
- name: Docker Push to Artifactory
id: publish
run: |
docker push artifacts.developer.gov.bc.ca/de27-general-docker/${{env.IMAGE_NAME}}:${GITHUB_REF##*/}
- name: Checkout ArgoCD Repo
id: gitops
if: steps.publish.outcome == 'success'
uses: actions/checkout@v4
with:
repository: bcgov-c/tenant-gitops-e27db1
ref: ${{ env.BRANCH_NAME }}
token: ${{ secrets.ARGO_PAT }} # access token
path: gitops
- name: Get short SHA
id: short_sha
run: |
echo "SHORT_SHA=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "Short SHA: $SHORT_SHA"
- name: Update Helm Values and Commit
id: helm
if: steps.gitops.outcome == 'success' # Only run if the previous step (publish) was successful
run: |
echo "Updating ${{ env.BRANCH_NAME }} helm values to trigger ArgoCD deployment "
# Navigate to the directory containing your Helm values file for the environment develop -> DEV, test -> test
cd gitops/charts
# Update the Helm values file with the new image tag and version
DATETIME=$(date +'%Y-%m-%d %H:%M:%S') # Get current date and time
echo "Updating tag apitag: to ${{ steps.short_sha.outputs.SHORT_SHA }}"
sed -i "s/apitag: .*/apitag: ${{ env.VALUES_FILE }}-${{ steps.short_sha.outputs.SHORT_SHA }} # Image Updated on $DATETIME/" ../deploy/${{ env.VALUES_FILE }}_values.yaml
sed -i "s/apitag: .*/apitag: ${{ env.VALUES_FILE }}-${{ steps.short_sha.outputs.SHORT_SHA }} # Image Updated on $DATETIME/" webapi/values.yaml
# Commit and push the changes
git config --global user.email "[email protected]"
git config --global user.name "GitHub Actions"
git add .
git add ../deploy/${{ env.VALUES_FILE }}_values.yaml
# Repackage Helm Chart
cd webapi
helm dependency build
git add .
git commit -m "Update ${{ env.BRANCH_NAME }} API image tag"
# pull any changes
git pull origin ${{ env.BRANCH_NAME }}
git push origin ${{ env.BRANCH_NAME }} # Update the branch name as needed