-
Notifications
You must be signed in to change notification settings - Fork 0
152 lines (147 loc) · 5.8 KB
/
deployment.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
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
# Copyright 2024 Zaphiro Technologies
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Container Deployment
concurrency:
group: ${{inputs.environment}}-container-deployment
cancel-in-progress: false
# Permissions needed
# permissions:
# packages: read
# pull-requests: read
on:
workflow_call:
inputs:
app-name:
required: true
type: string
description: Application name used in k8s-deployments
environment:
required: true
type: string
description: Environment name for the deployment
jobs:
split-environment:
name: Split environment
runs-on: ubuntu-latest
steps:
- name: Split environment to cluster and platform
id: split-environment
run: |
echo "cluster=$(echo ${{inputs.environment}} | cut -d'|' -f1)" >> $GITHUB_OUTPUT
echo "platform=$(echo ${{inputs.environment}} | cut -d'|' -f2)" >> $GITHUB_OUTPUT
outputs:
cluster: ${{steps.split-environment.outputs.cluster}}
platform: ${{steps.split-environment.outputs.platform}}
deploy:
name: Deploy
runs-on: ubuntu-latest
needs: split-environment
env:
CLUSTER: ${{needs.split-environment.outputs.cluster}}
PLATFORM: ${{needs.split-environment.outputs.platform}}
environment:
name: ${{inputs.environment}}
url: https://argocd.${{env.CLUSTER}}.zaphiro.ch/applications/argocd/platforms?view=tree&resource=kind%3AApplication
steps:
- name: Get image tag from git tag
if: github.ref_type == 'tag'
run: |
tag_version="$(echo ${{github.ref}} | cut -d'/' -f3)"
echo "IMAGE_TAG=${tag_version#v}" >> $GITHUB_ENV
- name: Get image tag from main branch
if: github.ref_type == 'branch' && github.ref == 'refs/heads/main'
run: |
echo "IMAGE_TAG=main" >> $GITHUB_ENV
- name: Search PR of branch not main
if: github.ref_type == 'branch' && github.ref != 'refs/heads/main'
id: search-pr
uses: actions/github-script@v7
env:
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
with:
script: |
const {BRANCH_NAME} = process.env
const pr = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
head: `${context.repo.owner}:${BRANCH_NAME}`,
state: 'open'
});
if (pr.data.length == 1) {
return core.setOutput('pr', pr.data[0].number);
}
core.setFailed("PR not found");
- name: Get image tag from PR number
if: steps.search-pr.outputs.pr != ''
run: |
echo "IMAGE_TAG=pr-${{steps.search-pr.outputs.pr}}" >> $GITHUB_ENV
- name: Checkout of k8s-deployments
uses: actions/checkout@v4
with:
token: ${{ secrets.REPO_PAT }}
repository: ${{github.repository_owner}}/k8s-deployments
ref: main
- name: Get repository name from values-platforms.yaml
if: github.ref_type != 'tag'
id: repository-name
run: echo "repository=$(yq '.platforms.${{env.PLATFORM}}.${{inputs.app-name}}.image.repository' kubernetes/${{env.CLUSTER}}/values-platforms.yaml)" >> $GITHUB_OUTPUT
- name: Get digest for image tag if not git tag
if: github.ref_type != 'tag'
id: digest
# Use github APIs to get the digest of the image tag
uses: actions/github-script@v7
with:
script: |
const {IMAGE_TAG} = process.env
const repository_name = "${{steps.repository-name.outputs.repository}}"
// Check IMAGE_TAG is not empty
if (!IMAGE_TAG) {
return core.setFailed("No valid tag found");
}
for await (const versions of github.paginate.iterator(
github.rest.packages.getAllPackageVersionsForPackageOwnedByOrg, {
package_type: "container",
package_name: repository_name.split("/").pop(),
org: "${{github.repository_owner}}",
state: "active"
})) {
// Check if tag is in tags list
for (const version of versions.data) {
console.log(version)
if (version.metadata.container.tags.includes(IMAGE_TAG)) {
return core.exportVariable('DIGEST', version.name);
}
}
}
core.setFailed("Image tag not found");
- name: Concat digest to tag
if: env.DIGEST != ''
run: |
echo "IMAGE_TAG=$IMAGE_TAG@$DIGEST" >> $GITHUB_ENV
- name: Update values-platforms.yaml
uses: fjogeleit/[email protected]
with:
valueFile: kubernetes/${{env.CLUSTER}}/values-platforms.yaml
propertyPath: platforms.${{env.PLATFORM}}.${{inputs.app-name}}.image.tag
value: ${{env.IMAGE_TAG}}
commitChange: false
createPR: false
- name: Commit & Push changed tag
uses: actions-js/[email protected]
with:
github_token: ${{ secrets.REPO_PAT }}
branch: main
message: "Deploy tag ${{env.IMAGE_TAG}} to ${{github.repository}} on ${{inputs.environment}}"
directory: kubernetes/${{env.CLUSTER}}
repository: ${{github.repository_owner}}/k8s-deployments