Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Commit ed99674

Browse files
author
Michael Sauter
committed
WIP
1 parent d649596 commit ed99674

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1776
-150
lines changed

build/package/scripts/build-go.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ ARTIFACT_PREFIX=""
1616
PRE_TEST_SCRIPT=""
1717
DEBUG="${DEBUG:-false}"
1818

19-
while [[ "$#" -gt 0 ]]; do
19+
while [ "$#" -gt 0 ]; do
2020
case $1 in
2121

2222
--working-dir) WORKING_DIR="$2"; shift;;
@@ -131,4 +131,4 @@ if [ $exitcode != 0 ]; then
131131
exit $exitcode
132132
fi
133133
echo "Building ..."
134-
go build -gcflags "all=-trimpath=$(pwd)" -o "${OUTPUT_DIR}/app"
134+
go build -gcflags "all=-trimpath=$(pwd)" -o "${OUTPUT_DIR}/app"

build/package/scripts/build-gradle.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ gradle_build_dir="build"
1414
gradle_additional_tasks=
1515
gradle_options=
1616

17-
while [[ "$#" -gt 0 ]]; do
17+
while [ "$#" -gt 0 ]; do
1818
case $1 in
1919

2020
--working-dir) working_dir="$2"; shift;;

build/package/scripts/build-npm.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ ARTIFACT_PREFIX=""
3030
DEBUG="${DEBUG:-false}"
3131
COPY_NODE_MODULES="false"
3232

33-
while [[ "$#" -gt 0 ]]; do
33+
while [ "$#" -gt 0 ]; do
3434
case $1 in
3535

3636
--working-dir) WORKING_DIR="$2"; shift;;

build/package/scripts/build-python.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ ARTIFACT_PREFIX=""
2020
PRE_TEST_SCRIPT=""
2121
DEBUG="${DEBUG:-false}"
2222

23-
while [[ "$#" -gt 0 ]]; do
23+
while [ "$#" -gt 0 ]; do
2424
case $1 in
2525

2626
--working-dir) WORKING_DIR="$2"; shift;;

build/package/scripts/cache-build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CACHE_BUILD_KEY=
1515
CACHE_LOCATION_USED_PATH=
1616
DEBUG="${DEBUG:-false}"
1717

18-
while [[ "$#" -gt 0 ]]; do
18+
while [ "$#" -gt 0 ]; do
1919
case $1 in
2020

2121
--working-dir) WORKING_DIR="$2"; shift;;

build/package/scripts/configure-truststore.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ src_truststore="${JAVA_HOME}/lib/security/cacerts"
77
src_pass="changeit"
88
dest_pass="changeit"
99

10-
while [[ "$#" -gt 0 ]]; do
10+
while [ "$#" -gt 0 ]; do
1111
case $1 in
1212

1313
--src-store) src_truststore="$2"; shift;;

build/package/scripts/copy-artifacts.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CP="${GNU_CP:-cp}"
88

99
DEBUG="${DEBUG:-false}"
1010

11-
while [[ "$#" -gt 0 ]]; do
11+
while [ "$#" -gt 0 ]; do
1212
case $1 in
1313
--debug) DEBUG="$2"; shift;;
1414
--debug=*) DEBUG="${1#*=}";;

build/package/scripts/copy-build-if-cached.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ CACHE_LOCATION_USED_PATH=
1717
WORKING_DIR="."
1818
DEBUG="${DEBUG:-false}"
1919

20-
while [[ "$#" -gt 0 ]]; do
20+
while [ "$#" -gt 0 ]; do
2121
case $1 in
2222

2323
--cache-build) CACHE_BUILD="$2"; shift;;

build/package/scripts/download-aqua-scanner.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ md5_bin="${MD5_BIN:-"md5sum"}"
55
aqua_scanner_url=""
66
bin_dir=".ods-cache/bin"
77

8-
while [[ "$#" -gt 0 ]]; do
8+
while [ "$#" -gt 0 ]; do
99
case $1 in
1010

1111
--bin-dir) bin_dir="$2"; shift;;

build/package/scripts/supply-sonar-project-properties-default.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ set -eu
33

44
working_dir="."
55

6-
while [[ "$#" -gt 0 ]]; do
6+
while [ "$#" -gt 0 ]; do
77
case $1 in
88
--working-dir) working_dir="$2"; shift;;
99
--working-dir=*) working_dir="${1#*=}";;

cmd/taskdoc/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Package taskdoc implements documentation rendering for tasks.
2+
// It is intended to be run via `go run`, passing a task YAML manifest
3+
// and a description in Asciidoctor format. The combined result will be
4+
// written to the specified destination.
5+
//
6+
// Example invocation:
7+
//
8+
// go run github.com/opendevstack/ods-pipeline/cmd/taskdoc \
9+
// -task tasks/my-task.yaml \
10+
// -description build/docs/my-task.adoc \
11+
// -destination docs/my-task.adoc
12+
//
13+
// By default, taskdoc will use the template located at
14+
// docs/tasks/template.adoc.tmpl to produce the resulting file. Another
15+
// template can be specified via -template:
16+
//
17+
// go run github.com/opendevstack/ods-pipeline/cmd/taskdoc \
18+
// -task tasks/my-task.yaml \
19+
// -description build/docs/my-task.adoc \
20+
// -template /path/to/my-custom-template.adoc.tmpl \
21+
// -destination docs/my-task.adoc
22+
package main
23+
24+
import (
25+
"flag"
26+
"log"
27+
"os"
28+
"text/template"
29+
30+
"github.com/opendevstack/ods-pipeline/internal/projectpath"
31+
"github.com/opendevstack/ods-pipeline/pkg/taskdoc"
32+
)
33+
34+
func main() {
35+
taskFile := flag.String("task", "", "Task manifest")
36+
descriptionFile := flag.String("description", "", "Description snippet")
37+
templateFile := flag.String("template", projectpath.RootedPath("docs/tasks/template.adoc.tmpl"), "Template file")
38+
destinationFile := flag.String("destination", "", "Destination file")
39+
flag.Parse()
40+
if err := render(*taskFile, *descriptionFile, *templateFile, *destinationFile); err != nil {
41+
log.Fatal(err)
42+
}
43+
}
44+
45+
func render(taskFile, descriptionFile, templateFile, destinationFile string) error {
46+
t, err := os.ReadFile(taskFile)
47+
if err != nil {
48+
return err
49+
}
50+
d, err := os.ReadFile(descriptionFile)
51+
if err != nil {
52+
return err
53+
}
54+
tmpl, err := template.ParseFiles(templateFile)
55+
if err != nil {
56+
return err
57+
}
58+
59+
task, err := taskdoc.ParseTask(t, d)
60+
if err != nil {
61+
return err
62+
}
63+
64+
w, err := os.Create(destinationFile)
65+
if err != nil {
66+
return err
67+
}
68+
return taskdoc.RenderTaskDocumentation(w, tmpl, task)
69+
}

cmd/taskmanifest/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Package taskmanifest implements manifest rendering for tasks.
2+
// It is intended to be run via `go run`, passing a task YAML template
3+
// and data to be rendered. The combined result will be
4+
// written to the specified destination. The -data flag can be passed
5+
// multiple times and may specify any key-value combination, which can then
6+
// be consumed in the template through Go's text/template package. E.g.
7+
// passing -data Foo=bar will replace {{.Foo}} in the template with bar.
8+
//
9+
// Example invocation:
10+
//
11+
// go run github.com/opendevstack/ods-pipeline/cmd/taskmanifest \
12+
// -data ImageRepository=ghcr.io/my-org/my-repo \
13+
// -data Version=latest \
14+
// -template build/tasks/my-task.yaml \
15+
// -destination tasks/my-task.yaml
16+
package main
17+
18+
import (
19+
"flag"
20+
"fmt"
21+
"log"
22+
"os"
23+
"strings"
24+
"text/template"
25+
26+
"github.com/opendevstack/ods-pipeline/pkg/taskmanifest"
27+
"github.com/opendevstack/ods-pipeline/pkg/tektontaskrun"
28+
)
29+
30+
func main() {
31+
templateFile := flag.String("template", "", "Template file")
32+
destinationFile := flag.String("destination", "", "Destination file")
33+
cc := tektontaskrun.NewClusterConfig()
34+
mf := &MapFlag{v: cc.DefaultTaskTemplateData()}
35+
flag.Var(mf, "data", "Key-value pairs")
36+
flag.Parse()
37+
if err := render(*templateFile, *destinationFile, mf.v); err != nil {
38+
log.Fatal(err)
39+
}
40+
}
41+
42+
func render(templateFile, destinationFile string, data map[string]string) error {
43+
tmpl, err := template.ParseFiles(templateFile)
44+
if err != nil {
45+
return err
46+
}
47+
48+
w, err := os.Create(destinationFile)
49+
if err != nil {
50+
return err
51+
}
52+
return taskmanifest.RenderTask(w, tmpl, data)
53+
}
54+
55+
type MapFlag struct {
56+
v map[string]string
57+
}
58+
59+
func (mf *MapFlag) String() string {
60+
return fmt.Sprintf("%v", mf.v)
61+
}
62+
func (mf *MapFlag) Set(v string) error {
63+
key, value, ok := strings.Cut(v, "=")
64+
if !ok {
65+
return fmt.Errorf("must have = sign")
66+
}
67+
mf.v[key] = value
68+
return nil
69+
}

deploy/install.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function usage {
6363
\n\t\t--sonar-auth 'auth-token' \n\n" "$0"
6464
}
6565

66-
while [[ "$#" -gt 0 ]]; do
66+
while [ "$#" -gt 0 ]; do
6767
# shellcheck disable=SC2034
6868
case $1 in
6969

docs/authoring-tasks.adoc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ In theory you can use pretty much any image that works in OpenShift (e.g. the im
102102

103103
=== How do I create my own container image to use in a task?
104104

105-
In OpenShift, the easiest way is by creating an `ImageStream` and a `BuildConfig`. See the link:https://docs.openshift.com/container-platform/latest/cicd/builds/understanding-image-builds.html[OpenShift documentation on builds] for more information. You may also use the YAML definitions in `deploy/ods-pipeline/charts/images` as an example.
106-
107-
Occasionally, you might want to extend the images used in an official tasks, e.g. to deploy additional CA certificates, configure proxy settings, etc. The `images` subchart of `ods-pipeline` provides build configurations that allow you to create images that are based on the official `ods-pipeline` images from ghcr.io. The build configurations include inline Dockerfiles that you can adjust to suit your specific needs.
105+
In OpenShift, the easiest way is by creating an `ImageStream` and a `BuildConfig`. See the link:https://docs.openshift.com/container-platform/latest/cicd/builds/understanding-image-builds.html[OpenShift documentation on builds] for more information.
108106

109107
=== How can I test my tasks?
110108

internal/docs/tasks.go

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package docs
22

33
import (
44
"bytes"
5+
"errors"
56
"fmt"
7+
"io"
68
"log"
79
"os"
810
"path/filepath"
9-
"strings"
1011
"text/template"
1112

1213
"github.com/opendevstack/ods-pipeline/internal/command"
@@ -37,20 +38,11 @@ func renderTemplate(targetDir, targetFilename string, data Task) error {
3738
if err != nil {
3839
return err
3940
}
40-
templateFilename := filepath.Join(targetDir, "template.adoc.tmpl")
41-
templateFileParts := strings.Split(templateFilename, "/")
42-
templateDisplayname := templateFileParts[len(templateFileParts)-1]
43-
_, err = targetFile.WriteString(
44-
"// Document generated by internal/documentation/tasks.go from " + templateDisplayname + "; DO NOT EDIT.\n\n",
45-
)
41+
tmpl, err := template.ParseFiles(filepath.Join(targetDir, "template.adoc.tmpl"))
4642
if err != nil {
4743
return err
4844
}
49-
tmpl, err := template.ParseFiles(templateFilename)
50-
if err != nil {
51-
return err
52-
}
53-
return tmpl.Execute(targetFile, data)
45+
return RenderTaskDocumentation(targetFile, tmpl, &data)
5446
}
5547

5648
func parseTasks(helmTemplateOutput []byte) ([]*tekton.Task, error) {
@@ -131,3 +123,47 @@ func RenderTasks(tasksSourceDir, descriptionsSourceDir, targetDir string) error
131123
}
132124
return nil
133125
}
126+
127+
func ParseTask(f []byte, desc []byte) (*Task, error) {
128+
var t tekton.Task
129+
err := yaml.Unmarshal(f, &t)
130+
if err != nil {
131+
return nil, err
132+
}
133+
if t.Name == "" {
134+
return nil, errors.New("encountered empty name, something is wrong with the task")
135+
}
136+
task := &Task{
137+
Name: t.Name,
138+
Description: string(desc),
139+
Params: []Param{},
140+
Results: []Result{},
141+
}
142+
for _, p := range t.Spec.Params {
143+
defaultValue := ""
144+
if p.Default != nil {
145+
defaultValue = p.Default.StringVal
146+
}
147+
task.Params = append(task.Params, Param{
148+
Name: p.Name,
149+
Default: defaultValue,
150+
Description: p.Description,
151+
})
152+
}
153+
for _, r := range t.Spec.Results {
154+
task.Results = append(task.Results, Result{
155+
Name: r.Name,
156+
Description: r.Description,
157+
})
158+
}
159+
return task, nil
160+
}
161+
162+
func RenderTaskDocumentation(w io.Writer, tmpl *template.Template, task *Task) error {
163+
if _, err := w.Write(
164+
[]byte("// File is generated; DO NOT EDIT.\n\n"),
165+
); err != nil {
166+
return err
167+
}
168+
return tmpl.Execute(w, task)
169+
}

internal/projectpath/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ var (
1111
// Root folder of this project
1212
Root = filepath.Join(filepath.Dir(b), "../..")
1313
)
14+
15+
func RootedPath(path string) string {
16+
return filepath.Join(Root, path)
17+
}

pkg/odstasktest/assertions.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package odstasktest
2+
3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
"testing"
8+
)
9+
10+
// AssertFilesExist checks that all files named by wantFiles exist in wsDir.
11+
// Any files that do not exist will report a test error.
12+
func AssertFilesExist(t *testing.T, wsDir string, wantFiles ...string) {
13+
for _, wf := range wantFiles {
14+
filename := filepath.Join(wsDir, wf)
15+
if _, err := os.Stat(filename); os.IsNotExist(err) {
16+
t.Errorf("Want %s, but got nothing", filename)
17+
}
18+
}
19+
}
20+
21+
// AssertFileContent checks that the file named by filename in the directory
22+
// wsDir has the exact context specified by want.
23+
func AssertFileContent(t *testing.T, wsDir, filename, want string) {
24+
got, err := getTrimmedFileContent(filepath.Join(wsDir, filename))
25+
if err != nil {
26+
t.Errorf("get content of %s: %s", filename, err)
27+
return
28+
}
29+
if got != want {
30+
t.Errorf("got '%s', want '%s' in file %s", got, want, filename)
31+
}
32+
}
33+
34+
func getTrimmedFileContent(filename string) (string, error) {
35+
content, err := os.ReadFile(filename)
36+
if err != nil {
37+
return "", err
38+
}
39+
return strings.TrimSpace(string(content)), nil
40+
}

0 commit comments

Comments
 (0)