Skip to content

Commit

Permalink
Merge pull request #7 from opendevstack/feature/step-action
Browse files Browse the repository at this point in the history
WIP: Implement step action
  • Loading branch information
michaelsauter authored Mar 12, 2024
2 parents 328b396 + c8bed01 commit 4c1e3bb
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 87 deletions.
14 changes: 9 additions & 5 deletions build/images/Dockerfile.go-toolset
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/$GOLANGC
RUN go install github.com/jstemmer/go-junit-report/v2@$GO_JUNIT_REPORT_VERSION

# Add scripts
ADD https://raw.githubusercontent.com/opendevstack/ods-pipeline/v0.14.0-preview.1/build/images/scripts/cache-build.sh /usr/local/bin/cache-build
ADD https://raw.githubusercontent.com/opendevstack/ods-pipeline/v0.14.0-preview.1/build/images/scripts/copy-build-if-cached.sh /usr/local/bin/copy-build-if-cached
ADD https://raw.githubusercontent.com/opendevstack/ods-pipeline/v0.14.0-preview.1/build/images/scripts/copy-artifacts.sh /usr/local/bin/copy-artifacts
COPY build/images/scripts/build.sh /usr/local/bin/go-build
RUN chmod +rx /usr/local/bin/go-build && \
ADD https://raw.githubusercontent.com/opendevstack/ods-pipeline/v0.15.0/build/images/scripts/cache-build.sh /usr/local/bin/cache-build
ADD https://raw.githubusercontent.com/opendevstack/ods-pipeline/v0.15.0/build/images/scripts/copy-build-if-cached.sh /usr/local/bin/copy-build-if-cached
ADD https://raw.githubusercontent.com/opendevstack/ods-pipeline/v0.15.0/build/images/scripts/copy-artifacts.sh /usr/local/bin/copy-artifacts
COPY build/images/scripts/action.sh /usr/local/bin/go-build-action
COPY build/images/scripts/build.sh /usr/local/bin/go-build-script
RUN chmod +rx /usr/local/bin/go-build-action && \
chmod +rx /usr/local/bin/go-build-script && \
chmod +rx /usr/local/bin/cache-build && \
chmod +rx /usr/local/bin/copy-build-if-cached && \
chmod +rx /usr/local/bin/copy-artifacts
Expand All @@ -28,3 +30,5 @@ VOLUME /workspace/source
RUN git config --system --add safe.directory '/workspace/source'

USER 1001

ENTRYPOINT [ "go-build-action" ]
68 changes: 68 additions & 0 deletions build/images/scripts/action.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
set -eu

build_extra_inputs=""
build_reused_from_location_path=""
build_script="/usr/local/bin/go-build-script"
cache_build="true"
enable_cgo="false"
go_os="linux"
go_arch="amd64"
output_dir="docker"
working_dir="."
pre_test_script=""
debug="false"

while [[ "$#" -gt 0 ]]; do
case $1 in
-build-extra-inputs=*) build_extra_inputs="${1#*=}";;
-build-reused-from-location-path=*) build_reused_from_location_path="${1#*=}";;
-build-script=*) build_script="${1#*=}";;
-cache-build=*) cache_build="${1#*=}";;
-debug=*) debug="${1#*=}";;
-enable-cgo=*) enable_cgo="${1#*=}";;
-go-os=*) go_os="${1#*=}";;
-go-arch=*) go_arch="${1#*=}";;
-output-dir=*) output_dir="${1#*=}";;
-pre-test-script=*) pre_test_script="${1#*=}";;
-working-dir=*) working_dir="${1#*=}";;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done

echo -n "" > "${build_reused_from_location_path}"
cache_build_key="go-${go_os}-${go_arch}"
if copy-build-if-cached \
--cache-build="${cache_build}" \
--cache-build-key="${cache_build_key}" \
--build-extra-inputs="${build_extra_inputs}" \
--cached-outputs="${output_dir}" \
--cache-location-used-path="${build_reused_from_location_path}" \
--working-dir="${working_dir}" \
--debug="${debug}" ; then
exit 0
fi
set +e
"${build_script}" \
--working-dir="${working_dir}" \
--enable-cgo="${enable_cgo}" \
--go-os="${go_os}" \
--go-arch="${go_arch}" \
--pre-test-script="${pre_test_script}" \
--output-dir="${output_dir}" \
--debug="${debug}"
build_exit=$?
set -e
copy-artifacts --debug="${debug}"
if [ $build_exit -ne 0 ]; then
exit $build_exit
fi
cache-build \
--cache-build="${cache_build}" \
--cache-build-key="${cache_build_key}" \
--build-extra-inputs="${build_extra_inputs}" \
--cached-outputs="${output_dir}" \
--cache-location-used-path="${build_reused_from_location_path}" \
--working-dir="${working_dir}" \
--debug="${debug}"


53 changes: 15 additions & 38 deletions build/tasks/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ spec:
instead, it will be resolved from the workspace. See the task definition
for details how the build script is invoked.
type: string
default: "/usr/local/bin/go-build"
default: "/usr/local/bin/go-build-script"
- name: pre-test-script
description: Script to execute before running tests, relative to the working directory.
type: string
Expand All @@ -76,43 +76,20 @@ spec:
configMapKeyRef:
key: debug
name: ods-pipeline
script: |
echo -n "" > $(results.build-reused-from-location.path)
cache_build_key=go-$(params.go-os)-$(params.go-arch)
if copy-build-if-cached \
--cache-build=$(params.cache-build) \
--cache-build-key="$cache_build_key" \
--build-extra-inputs=$(params.build-extra-inputs) \
--cached-outputs=$(params.output-dir) \
--cache-location-used-path=$(results.build-reused-from-location.path) \
--working-dir=$(params.working-dir) \
--debug=${DEBUG} ; then
exit 0
fi
# Default build script is build/images/scripts/build.sh.
set +e
$(params.build-script) \
--working-dir=$(params.working-dir) \
--enable-cgo=$(params.enable-cgo) \
--go-os=$(params.go-os) \
--go-arch=$(params.go-arch) \
--pre-test-script=$(params.pre-test-script) \
--output-dir=$(params.output-dir) \
--debug=${DEBUG}
build_exit=$?
set -e
copy-artifacts --debug=${DEBUG}
if [ $build_exit -ne 0 ]; then
exit $build_exit
fi
cache-build \
--cache-build=$(params.cache-build) \
--cache-build-key="$cache_build_key" \
--build-extra-inputs=$(params.build-extra-inputs) \
--cached-outputs=$(params.output-dir) \
--cache-location-used-path=$(results.build-reused-from-location.path) \
--working-dir=$(params.working-dir) \
--debug=${DEBUG}
command: [ "go-build-action" ]
args: [
"-build-extra-inputs=$(params.build-extra-inputs)",
"-build-reused-from-location-path=$(results.build-reused-from-location.path)",
"-build-script=$(params.build-script)",
"-cache-build=$(params.cache-build)",
"-debug=${DEBUG}",
"-enable-cgo=$(params.enable-cgo)",
"-go-os=$(params.go-os)",
"-go-arch=$(params.go-arch)",
"-output-dir=$(params.output-dir)",
"-pre-test-script=$(params.pre-test-script)",
"-working-dir=$(params.working-dir)",
]
volumeMounts:
- mountPath: /etc/ssl/certs/private-cert.pem
name: private-cert
Expand Down
2 changes: 1 addition & 1 deletion docs/build.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ without leading `./` and trailing `/`.


| build-script
| /usr/local/bin/go-build
| /usr/local/bin/go-build-script
| Build script to execute. The link:https://github.com/opendevstack/ods-pipeline-go/blob/main/build/images/scripts/go-build.sh[default script] is located in the container image. If you specify a relative path instead, it will be resolved from the workspace. See the task definition for details how the build script is invoked.


Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ module github.com/opendevstack/ods-pipeline-go
go 1.19

require (
github.com/opendevstack/ods-pipeline v0.14.0
github.com/opendevstack/ods-pipeline v0.15.1-0.20240308101851-9d16db364456
github.com/tektoncd/pipeline v0.50.1
)

require (
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand Down Expand Up @@ -70,6 +68,8 @@ require (
google.golang.org/api v0.128.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230530153820-e85fd2cbaebc // indirect
google.golang.org/grpc v1.56.2 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,8 @@ github.com/onsi/ginkgo/v2 v2.9.1 h1:zie5Ly042PD3bsCvsSOPvRnFwyo3rKe64TJlD6nu0mk=
github.com/onsi/gomega v1.27.4 h1:Z2AnStgsdSayCMDiCU42qIz+HLqEPcgiOCXjAU/w+8E=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opendevstack/ods-pipeline v0.14.0 h1:PaDVG7HJzCFWCuDownR/iDPey+hY8UL/kVq/qLdayjU=
github.com/opendevstack/ods-pipeline v0.14.0/go.mod h1:43sXbO+DpXewW5OEz5JGFloAg9gZ5k9IkfY323l5+QE=
github.com/opendevstack/ods-pipeline v0.15.1-0.20240308101851-9d16db364456 h1:PDfDCHrCLdnGb4DeJyMm8+lIhM+NGWMn/X1YFycLxCM=
github.com/opendevstack/ods-pipeline v0.15.1-0.20240308101851-9d16db364456/go.mod h1:43sXbO+DpXewW5OEz5JGFloAg9gZ5k9IkfY323l5+QE=
github.com/otiai10/copy v1.12.0 h1:cLMgSQnXBs1eehF0Wy/FAGsgDTDmAqFR7rQylBb1nDY=
github.com/otiai10/copy v1.12.0/go.mod h1:rSaLseMUsZFFbsFGc7wCJnnkTAvdc5L6VWxPE4308Ww=
github.com/otiai10/mint v1.5.1 h1:XaPLeE+9vGbuyEHem1JNk3bYc7KKqyI/na0/mLd/Kks=
Expand Down
98 changes: 98 additions & 0 deletions step-actions/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# File is generated; DO NOT EDIT.
apiVersion: tekton.dev/v1alpha1
kind: StepAction
metadata:
name: ods-pipeline-go-build
spec:
params:
- name: working-dir
description: |
Working directory. The path must be relative to the root of the repository,
without leading `./` and trailing `/`.
type: string
default: "."
- name: enable-cgo
description: Whether to enable CGO. When not enabled the build will set `CGO_ENABLED=0`.
type: string
default: "false"
- name: go-os
description: "`GOOS` variable (the execution operating system such as `linux`, `windows`)."
type: string
default: "linux"
- name: go-arch
description: "`GOARCH` variable (the execution architecture such as `arm`, `amd64`)."
type: string
default: "amd64"
- name: output-dir
description: >-
Path to the directory into which the resulting Go binary should be copied, relative to `working-dir`.
This directory may then later be used as Docker context for example.
type: string
default: docker
- name: cache-build
description: >-
If enabled tasks uses or populates cache with the output dir contents (and artifacts) so that
a build can be skipped if the `working-dir` contents did not change.
You must set this to `"false"` if the build can be affected by files outside `working-dir`. See ADR caching-build-tasks for more details and workarounds.
type: string
default: "true"
- name: build-extra-inputs
description: >-
List of build source directories (as colon separated string) which in addition working-dir influence the build.
These directories are relative to the repository root.
If the contents in these directories change the cache is invalidated so that the build task will rebuild from scratch.
type: string
default: ""
- name: build-script
description: >-
Build script to execute. The
link:https://github.com/opendevstack/ods-pipeline-go/blob/main/build/images/scripts/go-build.sh[default script]
is located in the container image. If you specify a relative path
instead, it will be resolved from the workspace. See the task definition
for details how the build script is invoked.
type: string
default: "/usr/local/bin/go-build"
- name: pre-test-script
description: Script to execute before running tests, relative to the working directory.
type: string
default: ""
- name: private-cert
description: Volume mount name
type: string
- name: debug
description: Whether to turn on debug mode
type: string
default: "false"
- name: source
description: Path to source workspace
type: string
results:
- description: The cache location that the build task used. If caching is not enabled this will be an empty string.
name: build-reused-from-location
# Image is built from build/images/Dockerfile.go-toolset.
image: 'ghcr.io/opendevstack/ods-pipeline-go/go-toolset:0.3.0'
env:
- name: HOME
value: '/tekton/home'
- name: CI
value: "true"
command: [ "go-build-action" ]
args: [
"-build-extra-inputs=$(params.build-extra-inputs)",
"-build-reused-from-location-path=$(params.build-reused-from-location-path)",
"-build-script=$(params.build-script)",
"-cache-build=$(params.cache-build)",
"-debug=$(params.debug)",
"-enable-cgo=$(params.enable-cgo)",
"-go-os=$(params.go-os)",
"-go-arch=$(params.go-arch)",
"-output-dir=$(params.output-dir)",
"-pre-test-script=$(params.pre-test-script)",
"-working-dir=$(params.working-dir)",
]
volumeMounts:
- mountPath: /etc/ssl/certs/private-cert.pem
name: $(params.private-cert)
readOnly: true
subPath: tls.crt
workingDir: $(params.source)
53 changes: 15 additions & 38 deletions tasks/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ spec:
instead, it will be resolved from the workspace. See the task definition
for details how the build script is invoked.
type: string
default: "/usr/local/bin/go-build"
default: "/usr/local/bin/go-build-script"
- name: pre-test-script
description: Script to execute before running tests, relative to the working directory.
type: string
Expand All @@ -78,43 +78,20 @@ spec:
configMapKeyRef:
key: debug
name: ods-pipeline
script: |
echo -n "" > $(results.build-reused-from-location.path)
cache_build_key=go-$(params.go-os)-$(params.go-arch)
if copy-build-if-cached \
--cache-build=$(params.cache-build) \
--cache-build-key="$cache_build_key" \
--build-extra-inputs=$(params.build-extra-inputs) \
--cached-outputs=$(params.output-dir) \
--cache-location-used-path=$(results.build-reused-from-location.path) \
--working-dir=$(params.working-dir) \
--debug=${DEBUG} ; then
exit 0
fi
# Default build script is build/images/scripts/build.sh.
set +e
$(params.build-script) \
--working-dir=$(params.working-dir) \
--enable-cgo=$(params.enable-cgo) \
--go-os=$(params.go-os) \
--go-arch=$(params.go-arch) \
--pre-test-script=$(params.pre-test-script) \
--output-dir=$(params.output-dir) \
--debug=${DEBUG}
build_exit=$?
set -e
copy-artifacts --debug=${DEBUG}
if [ $build_exit -ne 0 ]; then
exit $build_exit
fi
cache-build \
--cache-build=$(params.cache-build) \
--cache-build-key="$cache_build_key" \
--build-extra-inputs=$(params.build-extra-inputs) \
--cached-outputs=$(params.output-dir) \
--cache-location-used-path=$(results.build-reused-from-location.path) \
--working-dir=$(params.working-dir) \
--debug=${DEBUG}
command: [ "go-build-action" ]
args: [
"-build-extra-inputs=$(params.build-extra-inputs)",
"-build-reused-from-location-path=$(results.build-reused-from-location.path)",
"-build-script=$(params.build-script)",
"-cache-build=$(params.cache-build)",
"-debug=${DEBUG}",
"-enable-cgo=$(params.enable-cgo)",
"-go-os=$(params.go-os)",
"-go-arch=$(params.go-arch)",
"-output-dir=$(params.output-dir)",
"-pre-test-script=$(params.pre-test-script)",
"-working-dir=$(params.working-dir)",
]
volumeMounts:
- mountPath: /etc/ssl/certs/private-cert.pem
name: private-cert
Expand Down

0 comments on commit 4c1e3bb

Please sign in to comment.