From 4fdce5b1aadce0582052f577cc855014e33bb281 Mon Sep 17 00:00:00 2001 From: Enrico Candino Date: Thu, 9 Jan 2025 10:10:53 +0100 Subject: [PATCH] Test release workflows (#173) * goreleaser action * removed old release * fix gomega version in tests * updated build workflow * fix for empty var --- .github/workflows/build.yml | 26 +++++--- .github/workflows/release-delete.yml | 61 ++++++++++++++++++ .github/workflows/release.yml | 94 +++++++++++++++++----------- .github/workflows/test.yaml | 6 +- .goreleaser.yaml | 90 ++++++++++++++++++++++++++ package/Dockerfile | 9 ++- package/Dockerfile.kubelet | 4 +- 7 files changed, 240 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/release-delete.yml create mode 100644 .goreleaser.yaml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index e6d40cc9..9b025d73 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,22 +1,34 @@ +name: Build + on: push: branches: - - master + - main pull_request: -name: Build permissions: contents: read + jobs: - build-cross-arch: + build: runs-on: ubuntu-latest + steps: - name: Checkout code uses: actions/checkout@v4 - - name: Cross Arch Build - run: | - make ci + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v6 + with: + distribution: goreleaser + version: v2 + args: --clean --snapshot env: - CROSS: "true" + REPO: ${{ github.repository }} + REGISTRY: \ No newline at end of file diff --git a/.github/workflows/release-delete.yml b/.github/workflows/release-delete.yml new file mode 100644 index 00000000..14f1f888 --- /dev/null +++ b/.github/workflows/release-delete.yml @@ -0,0 +1,61 @@ +name: Release - Delete Draft + +on: + workflow_dispatch: + inputs: + tag: + type: string + description: The tag of the release + +permissions: + contents: write + packages: write + +env: + GH_TOKEN: ${{ github.token }} + +jobs: + release-delete: + runs-on: ubuntu-latest + + steps: + - name: Check tag + if: inputs.tag == '' + run: echo "::error::Missing tag from input" && exit 1 + + - name: Checkout code + uses: actions/checkout@v4 + + - name: Check if release is draft + run: | + CURRENT_TAG=${{ inputs.tag }} + isDraft=$(gh release view ${CURRENT_TAG} --json isDraft --jq ".isDraft") + if [ "$isDraft" = true ]; then + echo "Release ${CURRENT_TAG} is draft" + else + echo "::error::Cannot delete non-draft release" && exit 1 + fi + + - name: Delete packages from Github Container Registry + run: | + CURRENT_TAG=${{ inputs.tag }} + echo "Deleting packages with tag ${CURRENT_TAG}" + + JQ_QUERY=".[] | select(.metadata.container.tags[] == \"${CURRENT_TAG}\")" + + for package in k3k k3k-kubelet + do + echo "Deleting ${package} image" + PACKAGE_TO_DELETE=$(gh api /user/packages/container/${package}/versions --jq "${JQ_QUERY}") + echo $PACKAGE_TO_DELETE | jq + + PACKAGE_ID=$(echo $PACKAGE_TO_DELETE | jq .id) + echo "Deleting ${PACKAGE_ID}" + gh api --method DELETE /user/packages/container/${package}/versions/${PACKAGE_ID} + done + + - name: Delete Github release + run: | + CURRENT_TAG=${{ inputs.tag }} + echo "Deleting release ${CURRENT_TAG}" + gh release delete ${CURRENT_TAG} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 91e7bdd0..1d598998 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,62 +1,86 @@ +name: Release + on: push: tags: - "v*" + workflow_dispatch: + inputs: + commit: + type: string + description: Checkout a specific commit -env: - GH_TOKEN: ${{ github.token }} - -name: Release permissions: contents: write - id-token: write + packages: write + jobs: - release-cross-arch: + release: runs-on: ubuntu-latest + steps: - name: Checkout code uses: actions/checkout@v4 - - - name: Cross Arch Build - run: | - make ci - env: - CROSS: "true" + with: + fetch-depth: 0 + fetch-tags: true + - name: Checkout code at the specific commit + if: inputs.commit != '' + run: git checkout ${{ inputs.commit }} + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version-file: go.mod + - name: "Read secrets" uses: rancher-eio/read-vault-secrets@main + if: github.repository_owner == 'rancher' with: secrets: | secret/data/github/repo/${{ github.repository }}/dockerhub/${{ github.repository_owner }}/credentials username | DOCKER_USERNAME ; secret/data/github/repo/${{ github.repository }}/dockerhub/${{ github.repository_owner }}/credentials password | DOCKER_PASSWORD ; - - name: release binaries + # Manually dispatched workflows (or forks) will use ghcr.io + - name: Setup ghcr.io + if: github.event == 'workflow_dispatch' || github.repository_owner != 'rancher' run: | - gh release upload ${{ github.ref_name }} bin/* - - - name: Login to Container Registry + echo "REGISTRY=ghcr.io" >> $GITHUB_ENV + echo "DOCKER_USERNAME=${{ github.actor }}" >> $GITHUB_ENV + echo "DOCKER_PASSWORD=${{ github.token }}" >> $GITHUB_ENV + + - name: Login to container registry uses: docker/login-action@v3 with: + registry: ${{ env.REGISTRY }} username: ${{ env.DOCKER_USERNAME }} password: ${{ env.DOCKER_PASSWORD }} - - - name: Build controller image - uses: docker/build-push-action@v5 - with: - context: . - push: true - tags: rancher/k3k:${{ github.ref_name }} - file: package/Dockerfile - platforms: linux/amd64 - - name: Build Virtual Kubelet image - uses: docker/build-push-action@v5 + # If the tag does not exists the workflow was manually triggered. + # That means we are creating temporary nightly builds, with a "fake" local tag + - name: Check release tag + id: release-tag + run: | + CURRENT_TAG=$(git describe --tag --always) + + if git show-ref --tags ${CURRENT_TAG} --quiet; then + echo "tag ${CURRENT_TAG} already exists"; + else + echo "tag ${CURRENT_TAG} does not exist" + git tag ${CURRENT_TAG} + fi + + echo "CURRENT_TAG=${CURRENT_TAG}" >> "$GITHUB_OUTPUT" + + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@v6 with: - context: . - push: true - tags: rancher/k3k:k3k-kubelet-dev - file: package/Dockerfile.kubelet - platforms: linux/amd64 - - - \ No newline at end of file + distribution: goreleaser + version: v2 + args: --clean + env: + GITHUB_TOKEN: ${{ github.token }} + GORELEASER_CURRENT_TAG: ${{ steps.release-tag.outputs.CURRENT_TAG }} + REGISTRY: ${{ env.REGISTRY }} + REPO: ${{ github.repository }} diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 7e2b393a..9cd22c4a 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -8,9 +8,6 @@ on: permissions: contents: read -env: - GO_VERSION: "1.22" - jobs: tests: runs-on: ubuntu-latest @@ -21,7 +18,7 @@ jobs: - uses: actions/setup-go@v5 with: - go-version: ${{env.GO_VERSION}} + go-version-file: go.mod - name: Check go modules run: | @@ -31,7 +28,6 @@ jobs: - name: Install tools run: | go install github.com/onsi/ginkgo/v2/ginkgo - go get github.com/onsi/gomega/... # With Golang 1.22 we need to use the release-0.18 branch go install sigs.k8s.io/controller-runtime/tools/setup-envtest@release-0.18 diff --git a/.goreleaser.yaml b/.goreleaser.yaml new file mode 100644 index 00000000..dca0db03 --- /dev/null +++ b/.goreleaser.yaml @@ -0,0 +1,90 @@ +version: 2 + +release: + draft: true + replace_existing_draft: true + prerelease: auto + +before: + hooks: + - go mod tidy + - go generate ./... + +builds: + - id: k3k + env: + - CGO_ENABLED=0 + goos: + - linux + goarch: + - "amd64" + - "arm64" + - "s390x" + + - id: k3k-kubelet + main: ./k3k-kubelet + binary: k3k-kubelet + env: + - CGO_ENABLED=0 + goos: + - linux + goarch: + - "amd64" + - "arm64" + - "s390x" + + - id: k3kcli + main: ./cli + binary: k3kcli + env: + - CGO_ENABLED=0 + goarch: + - "amd64" + - "arm64" + +archives: + - format: binary + name_template: >- + {{ .Binary }}-{{- .Os }}-{{ .Arch }} + {{- if .Arm }}v{{ .Arm }}{{ end }} + format_overrides: + - goos: windows + format: zip + +# For the image_templates we are using the following expression to build images for the correct registry +# {{- if .Env.REGISTRY }}{{ .Env.REGISTRY }}/{{ end }} +# +# REGISTRY= -> rancher/k3k:vX.Y.Z +# REGISTRY=ghcr.io -> ghcr.io/rancher/k3k:latest:vX.Y.Z +# +dockers: + - id: k3k + use: docker + ids: + - k3k + - k3kcli + dockerfile: "package/Dockerfile" + skip_push: false + image_templates: + - "{{- if .Env.REGISTRY }}{{ .Env.REGISTRY }}/{{ end }}{{ .Env.REPO }}:{{ .Tag }}" + build_flag_templates: + - "--build-arg=BIN_K3K=k3k" + - "--build-arg=BIN_K3KCLI=k3kcli" + + - id: k3k-kubelet + use: docker + ids: + - k3k-kubelet + dockerfile: "package/Dockerfile.kubelet" + skip_push: false + image_templates: + - "{{- if .Env.REGISTRY }}{{ .Env.REGISTRY }}/{{ end }}{{ .Env.REPO }}-kubelet:{{ .Tag }}" + build_flag_templates: + - "--build-arg=BIN_K3K_KUBELET=k3k-kubelet" + +changelog: + sort: asc + filters: + exclude: + - "^docs:" + - "^test:" diff --git a/package/Dockerfile b/package/Dockerfile index 54097820..93393807 100644 --- a/package/Dockerfile +++ b/package/Dockerfile @@ -1,4 +1,9 @@ FROM alpine -COPY bin/k3k /usr/bin/ -COPY bin/k3kcli /usr/bin/ + +ARG BIN_K3K=bin/k3k +ARG BIN_K3KCLI=bin/k3kcli + +COPY ${BIN_K3K} /usr/bin/ +COPY ${BIN_K3KCLI} /usr/bin/ + CMD ["k3k"] diff --git a/package/Dockerfile.kubelet b/package/Dockerfile.kubelet index a77597dc..1bb3dce5 100644 --- a/package/Dockerfile.kubelet +++ b/package/Dockerfile.kubelet @@ -1,6 +1,8 @@ # TODO: swicth this to BCI-micro or scratch. Left as base right now so that debug can be done a bit easier FROM registry.suse.com/bci/bci-base:15.6 -COPY bin/k3k-kubelet /usr/bin/ +ARG BIN_K3K_KUBELET=bin/k3k-kubelet + +COPY ${BIN_K3K_KUBELET} /usr/bin/ ENTRYPOINT ["/usr/bin/k3k-kubelet"]