Skip to content

Commit 4d52de6

Browse files
committed
Import some tasks from my experimental tasks repository
Signed-off-by: Vincent Demeester <[email protected]>
1 parent 822aad1 commit 4d52de6

File tree

9 files changed

+618
-0
lines changed

9 files changed

+618
-0
lines changed

.github/dependabot.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: 2
2+
updates:
3+
- package-ecosystem: "docker"
4+
directory: "/"
5+
schedule:
6+
interval: "weekly"
7+
# Disable all pull requests for Docker dependencies
8+
open-pull-requests-limit: 0
9+
- package-ecosystem: "github-actions"
10+
directory: "/"
11+
schedule:
12+
interval: "weekly"

.github/workflows/images.yaml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
on:
2+
pull_request:
3+
branches: [ main ]
4+
push:
5+
branches: [ main ]
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
name: build-images
10+
11+
env:
12+
REGISTRY: ghcr.io
13+
14+
jobs:
15+
list:
16+
runs-on: ubuntu-latest
17+
outputs:
18+
images: ${{ steps.output-images.outputs.images }}
19+
steps:
20+
- uses: actions/checkout@v3
21+
- name: list images
22+
id: output-images
23+
run: |
24+
echo "images=$(ls images | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
25+
26+
build:
27+
runs-on: ubuntu-latest
28+
permissions:
29+
contents: read
30+
packages: write
31+
needs: list
32+
strategy:
33+
matrix:
34+
image: ${{fromJSON(needs.list.outputs.images)}}
35+
steps:
36+
- uses: actions/checkout@v3
37+
with:
38+
fetch-depth: 2
39+
- name: Get changed files
40+
id: changed-files
41+
uses: tj-actions/changed-files@v35
42+
with:
43+
files: |
44+
images/${{ matrix.image }}/**
45+
- name: changed files
46+
if: steps.changed-files.outputs.any_changed == 'true'
47+
run: |
48+
echo ${{ steps.changed-files.outputs.any_changed }}
49+
echo "List all the files that have changed: ${{ steps.changed-files-specific.outputs.all_changed_files }}"
50+
- name: Log in to the Container registry
51+
if: steps.changed-files.outputs.any_changed == 'true' || github.event_name != 'pull_request'
52+
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
53+
with:
54+
registry: ${{ env.REGISTRY }}
55+
username: ${{ github.actor }}
56+
password: ${{ secrets.GITHUB_TOKEN }}
57+
- name: Extract metadata (tags, labels) for Docker
58+
if: steps.changed-files.outputs.any_changed == 'true' || github.event_name != 'pull_request'
59+
id: meta
60+
uses: docker/metadata-action@57396166ad8aefe6098280995947635806a0e6ea
61+
with:
62+
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ matrix.image }}
63+
- name: Build and push Docker image
64+
if: steps.changed-files.outputs.any_changed == 'true' || github.event_name != 'pull_request'
65+
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
66+
with:
67+
context: images/${{ matrix.image }}
68+
push: ${{ github.event_name != 'pull_request' }}
69+
tags: ${{ steps.meta.outputs.tags }}
70+
labels: ${{ steps.meta.outputs.labels }}

.github/workflows/test-tasks.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
on:
2+
pull_request:
3+
branches: [ main ]
4+
push:
5+
branches: [ main ]
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
name: test-tasks
10+
11+
jobs:
12+
test:
13+
runs-on: ubuntu-latest
14+
permissions:
15+
contents: read
16+
packages: write
17+
steps:
18+
- uses: actions/checkout@v3
19+
- uses: chainguard-dev/actions/setup-kind@main
20+
with:
21+
k8s-version: v1.23.x
22+
- uses: vdemeester/setup-tektoncd@main
23+
with:
24+
pipeline: v0.44.x
25+
pipeline-feature-flags: '{"enable-api-fields": "alpha"}'
26+
- name: run-tests
27+
run: |
28+
kubectl apply -f https://raw.githubusercontent.com/tektoncd/catalog/main/task/git-clone/0.7/git-clone.yaml
29+
# go-crane-image
30+
kubectl create -f ./tasks/go-crane-image/go-crane-image.yaml
31+
kubectl create -f ./tasks/go-crane-image/tests/run.yaml
32+
33+
tkn pipelinerun logs -f go-crane-image-test-run
34+
tkn pipelinerun describe go-crane-image-test-run
35+
36+
kubectl get pipelinerun go-crane-image-test-run
37+
#status=$(kubectl get pipelinerun go-crane-image-test-run -o json | jq -r '.items[] | .status.conditions[] | select(.type == "Succeeded") | .status')
38+
#if [[ "${status}" != "True" ]]; then
39+
# echo "go-crane-image test failed"
40+
# kubectl get pipelinerun/go-crane-image-test-run -o yaml
41+
#fi
42+
43+
# go-ko-image
44+
kubectl create -f ./tasks/go-ko-image/go-ko-image.yaml
45+
kubectl create -f ./tasks/go-ko-image/tests/run.yaml
46+
47+
tkn pipelinerun logs -f go-ko-image-test-run
48+
tkn pipelinerun describe go-ko-image-test-run
49+
50+
kubectl get pipelinerun go-ko-image-test-run

go-crane-image/go-crane-image.yaml

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
apiVersion: tekton.dev/v1beta1
2+
kind: Task
3+
metadata:
4+
name: go-crane-image
5+
labels:
6+
app.kubernetes.io/version: "0.1"
7+
annotations:
8+
tekton.dev/pipelines.minVersion: "0.40.0"
9+
tekton.dev/categories: language
10+
tekton.dev/tags: go
11+
tekton.dev/displayName: "go crane image"
12+
tekton.dev/platforms: "linux/amd64,linux/s390x,linux/ppc64le,linux/arm64"
13+
spec:
14+
description: >-
15+
The go-crane-image Task will build a container image based of off a go
16+
project to be compiled, using crane.
17+
workspaces:
18+
- name: source
19+
description: The go source to build
20+
- name: dockerconfig
21+
description: Includes a docker `config.json` or `.dockerconfigjson`
22+
optional: true
23+
params:
24+
- name: app
25+
description: >-
26+
The name of the "application" to build. This will have an impact on the binary
27+
and possibly the image reference
28+
- name: package
29+
description: >-
30+
The package to build. It needs to be a package `main` that compiles into a binary.
31+
The default value is `.`, usual value can be `./cmd/{name}`
32+
default: .
33+
- name: image
34+
description: >-
35+
The image specific options such as prefix, labels, env, …
36+
type: object
37+
properties:
38+
base: {type: string}
39+
labels: {type: string}
40+
envs: {type: string}
41+
push: {type: string}
42+
prefix: {type: string}
43+
tag: {type: string}
44+
default:
45+
base: ""
46+
labels: ""
47+
envs: ""
48+
push: "true"
49+
tag: "latest"
50+
- name: go
51+
description: >-
52+
Golang options, such as flags, version, …
53+
type: object
54+
properties:
55+
version: {type: string}
56+
GOFLAGS: {type: string}
57+
GOOS: {type: string}
58+
GOARCH: {type: string}
59+
CGO_ENABLED: {type: string}
60+
default:
61+
version: "1.18"
62+
GOFLAGS: "-v"
63+
GOOS: ""
64+
GOARCH: ""
65+
CGO_ENABLED: "0"
66+
results:
67+
- name: IMAGE_DIGEST
68+
description: Digest of the image just built.
69+
- name: IMAGE_URL
70+
description: URL of the image just built.
71+
steps:
72+
- name: build-go
73+
image: docker.io/library/golang:$(params.go.version)
74+
workingDir: $(workspaces.source.path)
75+
script: |
76+
#!/usr/bin/env bash
77+
set -e
78+
go env
79+
go build -o $(params.app) $(params.package)
80+
env:
81+
- name: GOFLAGS
82+
value: "$(params.go.GOFLAGS)"
83+
- name: GOOS
84+
value: "$(params.go.GOOS)"
85+
- name: GOARCH
86+
value: "$(params.go.GOARCH)"
87+
- name: CGO_ENABLED
88+
value: "$(params.go.CGO_ENABLED)"
89+
- name: publish-image
90+
image: ghcr.io/shortbrain/golang-tasks/crane:0.12.0
91+
workingDir: $(workspaces.source.path)
92+
script: |
93+
#!/usr/bin/env bash
94+
set -e
95+
96+
if [[ "$(params.image.push)" == "false" ]]; then
97+
echo "Not doing anything as push is disabled"
98+
echo -n "" > $(resutls.IMAGE_DIGEST.path)
99+
echo -n "" > $(resutls.IMAGE_URL.path)
100+
exit 0
101+
fi
102+
103+
# Prepare the layer to add
104+
mkdir output
105+
cp $(params.app) ./output
106+
# FIXME: extra things to copy ?
107+
108+
if [[ "$(workspaces.dockerconfig.bound)" == "true" ]]; then
109+
# if config.json exists at workspace root, we use that
110+
if test -f "$(workspaces.dockerconfig.path)/config.json"; then
111+
export DOCKER_CONFIG="$(workspaces.dockerconfig.path)"
112+
# else we look for .dockerconfigjson at the root
113+
elif test -f "$(workspaces.dockerconfig.path)/.dockerconfigjson"; then
114+
cp "$(workspaces.dockerconfig.path)/.dockerconfigjson" "$HOME/.docker/config.json"
115+
export DOCKER_CONFIG="$HOME/.docker"
116+
# need to error out if neither files are present
117+
else
118+
echo "neither 'config.json' nor '.dockerconfigjson' found at workspace root"
119+
exit 1
120+
fi
121+
fi
122+
123+
APPEND_FLAGS="--new_tag $(params.image.prefix)/$(params.app):$(params.image.tag)"
124+
if [[ -n "$(params.image.base)" ]]; then
125+
APPEND_FLAGS="--base $(params.image.base) ${APPEND_FLAGS}"
126+
fi
127+
128+
MUTATE_FLAGS="--entrypoint=/$(params.app) --tag $(params.image.prefix)/$(params.app):$(params.image.tag)"
129+
# envs
130+
while IFS=';' read -ra ENVS; do
131+
for ENV in "${ENVS[@]}"; do
132+
MUTATE_FLAGS="${MUTATE_FLAGS} --env ${ENV}"
133+
done
134+
done <<< "$(params.image.envs)"
135+
136+
# labels
137+
while IFS=';' read -ra LABELS; do
138+
for LABEL in "${LABELS[@]}"; do
139+
MUTATE_FLAGS="${MUTATE_FLAGS} --label ${LABEL}"
140+
done
141+
done <<< "$(params.image.labels)"
142+
143+
crane mutate $( \
144+
crane append ${APPEND_FLAGS} \
145+
--new_layer <(cd ./output && tar -f - -c .) \
146+
) \
147+
${MUTATE_FLAGS} > crane_output
148+
CRANE_OUTPUT=$(cat crane_output)
149+
echo -n ${CRANE_OUTPUT#*@} > $(results.IMAGE_DIGEST.path)
150+
echo -n ${CRANE_OUTPUT} > $(results.IMAGE_URL.path)
151+
# echo -n ${CRANE_OUTPUT%@*} > $(results.IMAGE_URL.path)

go-crane-image/tests/run.yaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
apiVersion: v1
3+
kind: PersistentVolumeClaim
4+
metadata:
5+
name: go-crane-image-source-pvc
6+
spec:
7+
accessModes:
8+
- ReadWriteOnce
9+
resources:
10+
requests:
11+
storage: 500Mi
12+
---
13+
apiVersion: tekton.dev/v1beta1
14+
kind: Pipeline
15+
metadata:
16+
name: go-crane-image-pipeline
17+
spec:
18+
workspaces:
19+
- name: shared-workspace
20+
tasks:
21+
- name: fetch-repository
22+
taskRef:
23+
name: git-clone
24+
workspaces:
25+
- name: output
26+
workspace: shared-workspace
27+
params:
28+
- name: url
29+
value: https://github.com/chmouel/go-rest-api-test
30+
- name: subdirectory
31+
value: ""
32+
- name: deleteExisting
33+
value: "true"
34+
- name: run-build
35+
taskRef:
36+
name: go-crane-image
37+
runAfter:
38+
- fetch-repository
39+
workspaces:
40+
- name: source
41+
workspace: shared-workspace
42+
params:
43+
- name: app
44+
value: go-rest-api
45+
- name: go
46+
value:
47+
CGO_ENABLED: "0"
48+
- name: image
49+
value:
50+
base: docker.io/library/alpine
51+
prefix: registry.local:5000/go-rest-api-test
52+
labels: foo=bar;bar=baz
53+
envs: FOO=bar;BAR=baz
54+
---
55+
apiVersion: tekton.dev/v1beta1
56+
kind: PipelineRun
57+
metadata:
58+
name: go-crane-image-test-run
59+
spec:
60+
pipelineRef:
61+
name: go-crane-image-pipeline
62+
workspaces:
63+
- name: shared-workspace
64+
persistentvolumeclaim:
65+
claimName: go-crane-image-source-pvc

0 commit comments

Comments
 (0)