Skip to content

Commit 4f11c43

Browse files
authored
Merge pull request #74 from afdesk/ci/publish-helm
ci: publish helm
2 parents c8c14d3 + 8912178 commit 4f11c43

File tree

6 files changed

+217
-1
lines changed

6 files changed

+217
-1
lines changed

.github/workflows/bypass-test.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ on:
99
- 'mkdocs.yml'
1010
- 'LICENSE'
1111
- '.release-please-manifest.json'
12+
- 'helm/trivy/Chart.yaml'
1213
pull_request:
1314
paths:
1415
- '**.md'
1516
- 'docs/**'
1617
- 'mkdocs.yml'
1718
- 'LICENSE'
1819
- '.release-please-manifest.json'
20+
- 'helm/trivy/Chart.yaml'
1921
jobs:
2022
test:
2123
name: Test

.github/workflows/publish-chart.yaml

+36-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ name: Publish Helm chart
44
on:
55
workflow_dispatch:
66
pull_request:
7+
types:
8+
- opened
9+
- synchronize
10+
- reopened
11+
- closed
712
branches:
813
- main
914
paths:
@@ -18,7 +23,9 @@ env:
1823
KIND_VERSION: "v0.14.0"
1924
KIND_IMAGE: "kindest/node:v1.23.6@sha256:b1fa224cc6c7ff32455e0b1fd9cbfd3d3bc87ecaa8fcb06961ed1afb3db0f9ae"
2025
jobs:
26+
# `test-chart` job starts if a PR with Helm Chart is created, merged etc.
2127
test-chart:
28+
if: github.event_name != 'push'
2229
runs-on: ubuntu-20.04
2330
steps:
2431
- name: Checkout
@@ -48,8 +55,36 @@ jobs:
4855
sed -i -e '136s,false,'true',g' ./helm/trivy/values.yaml
4956
ct lint-and-install --validate-maintainers=false --charts helm/trivy
5057
58+
# `update-chart-version` job starts if a new tag is pushed
59+
update-chart-version:
60+
if: github.event_name == 'push'
61+
runs-on: ubuntu-20.04
62+
steps:
63+
- name: Checkout
64+
uses: actions/[email protected]
65+
with:
66+
fetch-depth: 0
67+
- name: Set up Git user
68+
run: |
69+
git config --global user.email "[email protected]"
70+
git config --global user.name "GitHub Actions"
71+
72+
- name: Install tools
73+
uses: aquaproj/[email protected]
74+
with:
75+
aqua_version: v1.25.0
76+
aqua_opts: ""
77+
78+
- name: Create a PR with Trivy version
79+
run: mage helm:updateVersion
80+
env:
81+
# Use ORG_REPO_TOKEN instead of GITHUB_TOKEN
82+
# This allows the created PR to trigger tests and other workflows
83+
GITHUB_TOKEN: ${{ secrets.ORG_REPO_TOKEN }}
84+
85+
# `publish-chart` job starts if a PR with a new Helm Chart is merged or manually
5186
publish-chart:
52-
if: github.event_name == 'push' || github.event_name == 'workflow_dispatch'
87+
if: github.event.pull_request.merged == true || github.event_name == 'workflow_dispatch'
5388
needs:
5489
- test-chart
5590
runs-on: ubuntu-20.04

.github/workflows/test.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ on:
77
- 'mkdocs.yml'
88
- 'LICENSE'
99
- '.release-please-manifest.json' ## don't run tests for release-please PRs
10+
- 'helm/trivy/Chart.yaml'
1011
merge_group:
1112
workflow_dispatch:
1213

magefiles/helm.go

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
//go:build mage_helm
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"log"
8+
"os"
9+
"strconv"
10+
"strings"
11+
12+
"github.com/magefile/mage/sh"
13+
"golang.org/x/xerrors"
14+
"gopkg.in/yaml.v3"
15+
)
16+
17+
const chartFile = "./helm/trivy/Chart.yaml"
18+
19+
func main() {
20+
trivyVersion, err := version()
21+
if err != nil {
22+
log.Fatalf("could not determine Trivy version: %v", err)
23+
}
24+
input, err := os.ReadFile(chartFile)
25+
if err != nil {
26+
log.Fatalf("could not find helm chart %s: %v", chartFile, err)
27+
}
28+
jsonData := map[string]interface{}{}
29+
if err := yaml.Unmarshal(input, &jsonData); err != nil {
30+
log.Fatalf("could not unmarshal helm chart %s: %v", chartFile, err)
31+
}
32+
currentAppVersion, ok := jsonData["appVersion"].(string)
33+
if !ok {
34+
log.Fatalf("could not determine current app version")
35+
}
36+
currentHelmVersion, ok := jsonData["version"].(string)
37+
if !ok {
38+
log.Fatalf("could not determine current helm version")
39+
}
40+
newHelmVersion := newHelmVersion(currentHelmVersion, currentAppVersion, trivyVersion)
41+
42+
log.Printf("Current helm version %q with Trivy %q will bump up %q with Trivy %q",
43+
currentHelmVersion, currentAppVersion, newHelmVersion, trivyVersion)
44+
45+
newBranch := fmt.Sprintf("ci/helm-chart/bump-trivy-to-%s", trivyVersion)
46+
title := fmt.Sprintf("\"ci(helm): bump Trivy version to %s\"", trivyVersion)
47+
description := fmt.Sprintf("\"# Description\n\nThis PR bumps Trivy up to the %s version for the Helm chart.\"", trivyVersion)
48+
49+
cmds := [][]string{
50+
[]string{"sed", "-i", "-e", fmt.Sprintf("s/appVersion: %s/appVersion: %s/g", currentAppVersion, trivyVersion), chartFile},
51+
[]string{"sed", "-i", "-e", fmt.Sprintf("s/version: %s/version: %s/g", currentHelmVersion, trivyVersion), chartFile},
52+
[]string{"git", "switch", "-c", newBranch},
53+
[]string{"git", "add", "./helm/trivy/Chart.yaml"},
54+
[]string{"git", "commit", "-m", title},
55+
[]string{"git", "push", "origin", newBranch},
56+
[]string{"gh", "pr", "create", "--base", "main", "--head", newBranch, "--title", title, "--body", description, "--repo", "$GITHUB_REPOSITORY"},
57+
}
58+
59+
if err := runShCommands(cmds); err != nil {
60+
log.Fatal(err)
61+
}
62+
log.Print("Successfully created PR with a new helm version")
63+
}
64+
65+
func runShCommands(cmds [][]string) error {
66+
for _, cmd := range cmds {
67+
if err := sh.Run(cmd[0], cmd[1:]...); err != nil {
68+
return xerrors.Errorf("failed to run %v: %w", cmd, err)
69+
}
70+
}
71+
return nil
72+
}
73+
74+
func splitVersion(version string) []int {
75+
items := strings.Split(version, ".")
76+
result := make([]int, len(items))
77+
for i, item := range items {
78+
result[i], _ = strconv.Atoi(item)
79+
}
80+
return result
81+
}
82+
83+
func newHelmVersion(currentHelm, currentTrivy, newTrivy string) string {
84+
ch := splitVersion(currentHelm)
85+
ct := splitVersion(currentTrivy)
86+
tr := splitVersion(newTrivy)
87+
88+
if len(ch) != len(ct) || len(ch) != len(tr) || len(ch) != 3 {
89+
log.Fatalf("invalid version lengths for %q, %q and %q", currentHelm, currentTrivy, newTrivy)
90+
}
91+
92+
n := len(ch)
93+
res := make([]string, n)
94+
if tr[0] != ct[0] {
95+
res[0] = strconv.Itoa(tr[0])
96+
res[1] = strconv.Itoa(tr[1])
97+
res[2] = "0"
98+
return strings.Join(res, ".")
99+
}
100+
101+
res[0] = strconv.Itoa(tr[0])
102+
if tr[1] != ct[1] {
103+
res[1] = strconv.Itoa(ch[1] + tr[1] - ct[1])
104+
res[2] = "0"
105+
} else {
106+
res[1] = strconv.Itoa(ch[1])
107+
res[2] = strconv.Itoa(ch[2] + tr[2] - ct[2])
108+
}
109+
return strings.Join(res, ".")
110+
}

magefiles/helm_test.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//go:build mage_helm
2+
3+
package main
4+
5+
import (
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewVersion(t *testing.T) {
12+
tests := []struct {
13+
name string
14+
currentHelmVersion string
15+
currentTrivyVersion string
16+
newTrivyVersion string
17+
newHelmVersion string
18+
}{
19+
{
20+
"created the first patch",
21+
"0.1.0",
22+
"0.55.0",
23+
"0.55.1",
24+
"0.1.1",
25+
},
26+
{
27+
"created the second patch",
28+
"0.1.1",
29+
"0.55.1",
30+
"0.55.2",
31+
"0.1.2",
32+
},
33+
{
34+
"created the second patch but helm chart was changed",
35+
"0.1.2",
36+
"0.55.1",
37+
"0.55.2",
38+
"0.1.3",
39+
},
40+
{
41+
"created a new minor version",
42+
"0.1.1",
43+
"0.55.1",
44+
"0.56.0",
45+
"0.2.0",
46+
},
47+
{
48+
"created a new major version",
49+
"0.1.1",
50+
"0.55.1",
51+
"1.0.0",
52+
"1.0.0",
53+
},
54+
}
55+
56+
for _, test := range tests {
57+
t.Run(test.name, func(t *testing.T) {
58+
assert.Equal(t, test.newHelmVersion, newHelmVersion(test.currentHelmVersion, test.currentTrivyVersion, test.newTrivyVersion))
59+
})
60+
}
61+
}

magefiles/magefile.go

+7
Original file line numberDiff line numberDiff line change
@@ -489,3 +489,10 @@ func (CloudActions) Generate() error {
489489
func VEX(_ context.Context, dir string) error {
490490
return sh.RunWith(ENV, "go", "run", "-tags=mage_vex", "./magefiles/vex.go", "--dir", dir)
491491
}
492+
493+
type Helm mg.Namespace
494+
495+
// UpdateVersion updates a version for Trivy Helm Chart and creates a PR
496+
func (Helm) UpdateVersion() error {
497+
return sh.RunWith(ENV, "go", "run", "-tags=mage_helm", "./magefiles")
498+
}

0 commit comments

Comments
 (0)