From 8e5ab6ba3b81c596f9e0f914ea13622a1ea8a62f Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 18 Sep 2024 23:49:47 +0200 Subject: [PATCH] cmd/konflux: add support for release branches Signed-off-by: Vincent Demeester --- cmd/konflux/main.go | 69 ++++++++++++------- .../github/workflows/update-sources.yaml | 13 ++-- config/konflux/tektoncd-pipeline.yaml | 3 + 3 files changed, 57 insertions(+), 28 deletions(-) diff --git a/cmd/konflux/main.go b/cmd/konflux/main.go index 246f03d..932a5a2 100644 --- a/cmd/konflux/main.go +++ b/cmd/konflux/main.go @@ -17,11 +17,12 @@ import ( var templateFS embed.FS type Application struct { - Name string - Repository string - Upstream string - Branch string - Components []string + Name string + Repository string + Upstream string + Branch string + UpstreamBranch string + Components []string } type Component struct { @@ -35,6 +36,12 @@ type Config struct { Repository string Upstream string Components []string + Branches []Branch +} + +type Branch struct { + Version string + Upstream string } func main() { @@ -52,31 +59,50 @@ func main() { } app := Application{ - Name: c.Repository, - Repository: path.Join("openshift-pipelines", c.Repository), - Upstream: c.Upstream, - Components: c.Components, - Branch: "main", + Name: c.Repository, + Repository: path.Join("openshift-pipelines", c.Repository), + Upstream: c.Upstream, + Components: c.Components, + Branch: "main", + UpstreamBranch: "main", } - // FIXME: support release branches next for konflux - if err := generateKonflux(app, "main", filepath.Join(*target, ".konflux")); err != nil { + log.Println("Generate configurations for main branch") + if err := generateKonflux(app, filepath.Join(*target, ".konflux")); err != nil { log.Fatalln(err) } if err := generateGitHub(app, filepath.Join(*target, ".github")); err != nil { log.Fatalln(err) } + for _, branch := range c.Branches { + log.Printf("Generate configurations for %s branch\n", branch.Version) + + app := Application{ + Name: c.Repository, + Repository: path.Join("openshift-pipelines", c.Repository), + Upstream: c.Upstream, + Components: c.Components, + Branch: fmt.Sprintf("release-v%s.x", branch.Version), + UpstreamBranch: branch.Upstream, + } + if err := generateKonflux(app, filepath.Join(*target, ".konflux")); err != nil { + log.Fatalln(err) + } + if err := generateGitHub(app, filepath.Join(*target, ".github")); err != nil { + log.Fatalln(err) + } + } } -func generateKonflux(application Application, branch, target string) error { +func generateKonflux(application Application, target string) error { log.Printf("Generate konflux manifest in %s\n", target) - if err := os.MkdirAll(filepath.Join(target, branch), 0o755); err != nil { + if err := os.MkdirAll(filepath.Join(target, application.Branch), 0o755); err != nil { return err } - if err := generateFileFromTemplate("application.yaml", application, filepath.Join(target, branch, "application.yaml")); err != nil { + if err := generateFileFromTemplate("application.yaml", application, filepath.Join(target, application.Branch, "application.yaml")); err != nil { return err } - if err := generateFileFromTemplate("tests.yaml", application, filepath.Join(target, branch, "tests.yaml")); err != nil { + if err := generateFileFromTemplate("tests.yaml", application, filepath.Join(target, application.Branch, "tests.yaml")); err != nil { return err } for _, c := range application.Components { @@ -84,8 +110,8 @@ func generateKonflux(application Application, branch, target string) error { Name: c, Application: application.Name, Repository: application.Repository, - Branch: "main", - }, filepath.Join(target, branch, fmt.Sprintf("component-%s.yaml", c))); err != nil { + Branch: application.Branch, + }, filepath.Join(target, application.Branch, fmt.Sprintf("component-%s.yaml", c))); err != nil { return err } } @@ -97,13 +123,10 @@ func generateGitHub(application Application, target string) error { if err := os.MkdirAll(filepath.Join(target, "workflows"), 0o755); err != nil { return err } - if err := generateFileFromTemplate("update-sources.yaml", application, filepath.Join(target, "workflows", "update-sources.yaml")); err != nil { + filename := fmt.Sprintf("update-sources.%s.yaml", application.Branch) + if err := generateFileFromTemplate("update-sources.yaml", application, filepath.Join(target, "workflows", filename)); err != nil { return err } - // FIXME: figure out how to map release branch with upstream release branches - // if err := generateFileFromTemplate("update-sources-branches.yaml", application, filepath.Join(target, "workflows", "update-sources-branches.yaml")); err != nil { - // return err - // } return nil } diff --git a/cmd/konflux/templates/github/workflows/update-sources.yaml b/cmd/konflux/templates/github/workflows/update-sources.yaml index 1592891..090f034 100644 --- a/cmd/konflux/templates/github/workflows/update-sources.yaml +++ b/cmd/konflux/templates/github/workflows/update-sources.yaml @@ -1,4 +1,4 @@ -name: update-sources +name: update-sources-{{.Branch}} on: workflow_dispatch: {} @@ -21,12 +21,15 @@ jobs: run: | rm -fR upstream git clone https://github.com/{{.Upstream}} upstream + pushd upstream + git checkout -b {{.UpstreamBranch}} origin/{{.UpstreamBranch}} + popd - name: Commit new changes run: | git config user.name github-actions git config user.email github-actions@github.com - git checkout -b actions/update/sources-main + git checkout -b actions/update/sources-{{.Branch}} pushd upstream OLD_COMMIT=$(cat ../head) NEW_COMMIT=$(git rev-parse HEAD) @@ -49,13 +52,13 @@ jobs: https://github.com/{{.Upstream}}/compare/${NEW_COMMIT}..${OLD_COMMIT} EOF - git push -f origin actions/update/sources-main + git push -f origin actions/update/sources-{{.Branch}} - name: Create pull request run: | - if [ "$(gh pr list --base main --head actions/update/sources-main --json url --jq 'length')" = "0" ]; then + if [ "$(gh pr list --base {{.Branch}} --head actions/update/sources-{{.Branch}} --json url --jq 'length')" = "0" ]; then echo "creating PR..." - gh pr create -B main -H actions/update/sources-main --fill + gh pr create -B {{.Branch}} -H actions/update/sources-{{.Branch}} --fill else echo "a PR already exists, skipping..." fi diff --git a/config/konflux/tektoncd-pipeline.yaml b/config/konflux/tektoncd-pipeline.yaml index bdf0a75..49346a2 100644 --- a/config/konflux/tektoncd-pipeline.yaml +++ b/config/konflux/tektoncd-pipeline.yaml @@ -9,3 +9,6 @@ components: - sidecarlogresults - webhook - workingdirinit +branches: +- version: 1.16 + upstream: release-v0.62.x