Skip to content

Commit

Permalink
ci: inject dockerfile dependency versions from workflow (#1247)
Browse files Browse the repository at this point in the history
  • Loading branch information
zerok authored Nov 26, 2024
1 parent fb6380f commit eb9aac0
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 22 deletions.
22 changes: 21 additions & 1 deletion .github/workflows/acceptance-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,31 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2

- name: "Determine dependency versions"
id: "versions"
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const helmRelease = await github.rest.repos.getLatestRelease({
'owner': 'helm',
'repo': 'helm',
});
core.setOutput('helm', helmRelease.data.tag_name);
console.log('Helm version', helmRelease.data.tag_name);
const kustomizeReleases = await github.rest.repos.listReleases({
'owner': 'kubernetes-sigs',
'repo': 'kustomize',
});
const kustomizeRelease = kustomizeReleases.data.filter(release => release.tag_name.startsWith('kustomize') && !release.draft && !release.prerelease).map(release => release.tag_name.split('/')[1])[0];
console.log('Kustomize version', kustomizeRelease);
core.setOutput('kustomize', kustomizeRelease);
- name: Call Dagger Function
id: dagger
uses: dagger/dagger-for-github@e5153f5610d82ac9f3f848f3a25ad9d696641068 # v7.0.1
with:
version: "0.14.0"
verb: call
dagger-flags: "--silent"
args: "acceptance-tests --root-dir .:source-files --acceptance-tests-dir ./acceptance-tests"
args: "acceptance-tests --root-dir .:source-files --acceptance-tests-dir ./acceptance-tests --kustomize-version ${{ steps.versions.outputs.kustomize }} --helm-version ${{ steps.versions.outputs.helm }}"
30 changes: 30 additions & 0 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,34 @@ env:
type=semver,pattern={{version}},value=${{ inputs.tag }},enable=${{ inputs.tag != '' }}
jobs:
determine-versions:
runs-on: ubuntu-latest
outputs:
helm: ${{ steps.versions.outputs.helm }}
kustomize: ${{ steps.versions.outputs.kustomize }}
steps:
- name: "Determine dependency versions"
id: "versions"
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const helmRelease = await github.rest.repos.getLatestRelease({
'owner': 'helm',
'repo': 'helm',
});
core.setOutput('helm', helmRelease.data.tag_name);
console.log('Helm version', helmRelease.data.tag_name);
const kustomizeReleases = await github.rest.repos.listReleases({
'owner': 'kubernetes-sigs',
'repo': 'kustomize',
});
const kustomizeRelease = kustomizeReleases.data.filter(release => release.tag_name.startsWith('kustomize') && !release.draft && !release.prerelease).map(release => release.tag_name.split('/')[1])[0];
console.log('Kustomize version', kustomizeRelease);
core.setOutput('kustomize', kustomizeRelease);
build:
needs:
- determine-versions
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -66,6 +93,9 @@ jobs:
context: .
labels: ${{ steps.meta.outputs.labels }}
outputs: type=image,name=${{ env.REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=${{ github.event_name == 'push' }}
build-args: |
HELM_VERSION=${{ needs.determine-versions.outputs.helm }}
KUSTOMIZE_VERSION=${{ needs.determine-versions.outputs.kustomize }}
- name: Export digest
id: digest
Expand Down
17 changes: 9 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,24 +19,25 @@ RUN apk add --no-cache git make bash &&\

FROM golang:1.23.3-alpine AS helm
WORKDIR /tmp/helm
ARG HELM_VERSION
RUN apk add --no-cache jq curl
RUN export TAG=$(curl --silent "https://api.github.com/repos/helm/helm/releases/latest" | jq -r .tag_name) &&\
export OS=$(go env GOOS) &&\
RUN export OS=$(go env GOOS) && \
export ARCH=$(go env GOARCH) &&\
curl -SL "https://get.helm.sh/helm-${TAG}-${OS}-${ARCH}.tar.gz" > helm.tgz && \
if [[ -z ${HELM_VERSION} ]]; then export HELM_VERSION=$(curl --silent "https://api.github.com/repos/helm/helm/releases" | jq -r '.[0].tag_name'); fi && \
curl -SL "https://get.helm.sh/helm-${HELM_VERSION}-${OS}-${ARCH}.tar.gz" > helm.tgz && \
tar -xvf helm.tgz --strip-components=1

FROM golang:1.23.3-alpine AS kustomize
WORKDIR /tmp/kustomize
ARG KUSTOMIZE_VERSION
RUN apk add --no-cache jq curl
# Get the latest version of kustomize
# Releases are filtered by their name since the kustomize repository exposes multiple products in the releases
RUN export TAG=$(curl --silent "https://api.github.com/repos/kubernetes-sigs/kustomize/releases" | jq -r '[ .[] | select(.name | startswith("kustomize")) ] | .[0].tag_name') &&\
export VERSION_TAG=${TAG#*/} &&\
export OS=$(go env GOOS) &&\
RUN export OS=$(go env GOOS) &&\
export ARCH=$(go env GOARCH) &&\
echo "https://github.com/kubernetes-sigs/kustomize/releases/download/${TAG}/kustomize_${VERSION_TAG}_${OS}_${ARCH}.tar.gz" && \
curl -SL "https://github.com/kubernetes-sigs/kustomize/releases/download/${TAG}/kustomize_${VERSION_TAG}_${OS}_${ARCH}.tar.gz" > kustomize.tgz && \
if [[ -z ${KUSTOMIZE_VERSION} ]]; then export KUSTOMIZE_VERSION=$(curl --silent "https://api.github.com/repos/kubernetes-sigs/kustomize/releases" | jq -r '[ .[] | select(.name | startswith("kustomize")) ] | .[0].tag_name | split("/")[1]'); fi && \
echo "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/${KUSTOMIZE_VERSION}/kustomize_${KUSTOMIZE_VERSION}_${OS}_${ARCH}.tar.gz" && \
curl -SL "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize/${KUSTOMIZE_VERSION}/kustomize_${KUSTOMIZE_VERSION}_${OS}_${ARCH}.tar.gz" > kustomize.tgz && \
tar -xvf kustomize.tgz

FROM golang:1.23.3 AS build
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ BIN_DIR := $(GOPATH)/bin
GOX := $(BIN_DIR)/gox
GOLINTER := $(GOPATH)/bin/golangci-lint


$(GOLINTER):
go install github.com/golangci/golangci-lint/cmd/[email protected]

Expand All @@ -15,7 +16,7 @@ test:
go test ./... -bench=. -benchmem

acceptance-tests:
dagger call acceptance-tests --root-dir .:source-files --acceptance-tests-dir ./acceptance-tests
dagger call acceptance-tests --root-dir .:source-files --acceptance-tests-dir ./acceptance-tests --kustomize-version "" --helm-version ""

# Compilation
dev:
Expand Down
48 changes: 40 additions & 8 deletions dagger/dagger.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions dagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,18 @@ import (

type Tanka struct{}

func (m *Tanka) Build(ctx context.Context, rootDir *dagger.Directory) *dagger.Container {
func (m *Tanka) Build(ctx context.Context, rootDir *dagger.Directory, helmVersion string, kustomizeVersion string) *dagger.Container {
buildArgs := make([]dagger.BuildArg, 0, 2)
if helmVersion != "" {
buildArgs = append(buildArgs, dagger.BuildArg{Name: "HELM_VERSION", Value: helmVersion})
}
if kustomizeVersion != "" {
buildArgs = append(buildArgs, dagger.BuildArg{Name: "KUSTOMIZE_VERSION", Value: kustomizeVersion})
}
return dag.Container().
Build(rootDir)
Build(rootDir, dagger.ContainerBuildOpts{
BuildArgs: buildArgs,
})
}

func (m *Tanka) GetGoVersion(ctx context.Context, file *dagger.File) (string, error) {
Expand All @@ -33,12 +42,12 @@ func (m *Tanka) GetGoVersion(ctx context.Context, file *dagger.File) (string, er
return "", fmt.Errorf("no Go version found")
}

func (m *Tanka) AcceptanceTests(ctx context.Context, rootDir *dagger.Directory, acceptanceTestsDir *dagger.Directory) (string, error) {
func (m *Tanka) AcceptanceTests(ctx context.Context, rootDir *dagger.Directory, helmVersion string, kustomizeVersion string, acceptanceTestsDir *dagger.Directory) (string, error) {
goVersion, err := m.GetGoVersion(ctx, rootDir.File("go.mod"))
if err != nil {
return "", err
}
buildContainer := m.Build(ctx, rootDir)
buildContainer := m.Build(ctx, rootDir, helmVersion, kustomizeVersion)

k3s := dag.K3S("k3sdemo")
k3sSrv, err := k3s.Server().Start(ctx)
Expand Down

0 comments on commit eb9aac0

Please sign in to comment.