-
Notifications
You must be signed in to change notification settings - Fork 0
156 lines (127 loc) · 5.47 KB
/
build-and-publish-container-image.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
156
name: Build and Push Container Image
on:
pull_request:
paths:
- 'versions.json'
push:
branches:
- main
paths:
- 'versions.json'
env:
ArtifactoryImagePath: "${{ secrets.ARTIFACTORY_DOCKER_REPO_HOSTNAME }}/octopusdeploy/kubernetes-agent-tools-base"
DockerHubImagePath: "octopusdeploy/kubernetes-agent-tools-base"
jobs:
versions:
runs-on: ubuntu-latest
outputs:
toolsVersions: ${{ steps.versions.outputs.tools }}
latestVersion: ${{ steps.versions.outputs.latest }}
revision: ${{ steps.versions.outputs.revision}}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: "Parse versions.json"
id: versions
run: |
toolsVersions=$(jq -c .tools versions.json)
latestVersion=$(jq -r -c .latest versions.json)
revision=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 4)
echo "tools=$toolsVersions" >> $GITHUB_OUTPUT
echo "tools=$toolsVersions"
echo "latest=$latestVersion" >> $GITHUB_OUTPUT
echo "latest=$latestVersion"
echo "revision=$revision" >> $GITHUB_OUTPUT
echo "revision=$revision"
build:
runs-on: ubuntu-latest
needs: versions
strategy:
matrix: ${{ fromJSON(needs.versions.outputs.toolsVersions) }}
steps:
- name: Log Inputs
run: |
echo "Kubectl Version: ${{ matrix.kubectl }}"
echo "Helm Version: ${{ matrix.helm }}"
echo "Powershell Version: ${{ matrix.powershell }}"
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get branch names
id: branch_names
uses: OctopusDeploy/util-actions/[email protected]
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Artifactory
uses: docker/login-action@v3
with:
registry: ${{ secrets.ARTIFACTORY_DOCKER_REPO_HOSTNAME }}
username: ${{ secrets.ARTIFACTORY_USERNAME }}
password: ${{ secrets.ARTIFACTORY_PASSWORD }}
- name: Login to DockerHub
uses: docker/login-action@v3
# Only log into Dockerhub on when using main branch
if: ${{ github.ref == 'refs/heads/main' }}
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Get Kubernetes Version
id: kubernetes-version
run: |
kubectlVersion="${{ matrix.kubectl }}"
kubeVersion="${kubectlVersion%'.'*}"
echo "kubernetesVersion=$kubeVersion" >> $GITHUB_OUTPUT
echo "kubeVersion=$kubeVersion"
- name: Create Tag Version
run: |
kubernetesVersion="${{ steps.kubernetes-version.outputs.kubernetesVersion }}"
if [[ "${{steps.branch_names.outputs.branch_name}}" != "main" ]]
then
preRelease="-${{steps.branch_names.outputs.branch_name}}-$(date +'%Y%m%d%H%M%S')"
fi
revision="-${{ needs.versions.outputs.revision }}"
tagVersion="$kubernetesVersion$revision$preRelease";
echo "tagVersion=$tagVersion" >> $GITHUB_OUTPUT;
echo "tagVersion=$tagVersion";
allVersionsTag="kube${{ matrix.kubectl}}-helm${{ matrix.helm}}-pwsh${{matrix.powershell}}$revision$preRelease"
echo "allVersionsTag=$allVersionsTag" >> $GITHUB_OUTPUT;
echo "allVersionsTag=$allVersionsTag";
id: createTagVersion
- name: Build and push for test
if: ${{ github.ref != 'refs/heads/main' }}
uses: docker/build-push-action@v5
with:
push: true
tags: "${{ env.ArtifactoryImagePath }}:${{ steps.createTagVersion.outputs.tagVersion }},${{ env.ArtifactoryImagePath }}:${{ steps.createTagVersion.outputs.allVersionsTag}}"
platforms: linux/amd64,linux/arm64
build-args: |
"KUBECTL_VERSION=${{ matrix.kubectl }}"
"HELM_VERSION=${{ matrix.helm }}"
"POWERSHELL_VERSION=${{ matrix.powershell }}"
- name: Create production docker tags
if: ${{ github.ref == 'refs/heads/main' }}
run: |
artifactoryTags="$ArtifactoryImagePath:${{ steps.createTagVersion.outputs.tagVersion }},$ArtifactoryImagePath:${{ steps.createTagVersion.outputs.allVersionsTag}}"
dockerhubTags="$DockerHubImagePath:${{ steps.createTagVersion.outputs.tagVersion }},$DockerHubImagePath:${{ steps.createTagVersion.outputs.allVersionsTag}}"
kubernetesVersion="${{ matrix.kubectl }}"
if [[ "${{ needs.versions.outputs.latestVersion }}" == "${{ steps.kubernetes-version.outputs.kubernetesVersion }}" ]]
then
artifactoryTags="$artifactoryTags,$ArtifactoryImagePath:latest"
dockerhubTags="$dockerhubTags,$DockerHubImagePath:latest"
fi
dockerTags="$artifactoryTags,$dockerhubTags"
echo "dockerTags=$dockerTags" >> $GITHUB_OUTPUT;
echo "dockerTags=$dockerTags";
id: createProductionDockerTags
- name: Build and push for production
if: ${{ github.ref == 'refs/heads/main' }}
uses: docker/build-push-action@v5
with:
push: true
tags: ${{ steps.createProductionDockerTags.outputs.dockerTags }}
platforms: linux/amd64,linux/arm64
build-args: |
"KUBECTL_VERSION=${{ matrix.kubectl }}"
"HELM_VERSION=${{ matrix.helm }}"
"POWERSHELL_VERSION=${{ matrix.powershell }}"