From 31c7882d9a9b10813f432f399d597fc3f1dbba4e Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Mon, 8 Apr 2024 08:43:35 -0700 Subject: [PATCH] Add github action to package envtest binaries in releases Signed-off-by: Vince Prignano --- .github/workflows/tools-releases.yml | 58 +++++++++------------------- .gitignore | 1 + Makefile | 44 +++++++++++++++++++++ hack/envtest/darwin/Dockerfile | 10 ++++- hack/envtest/linux/Dockerfile | 10 ++++- hack/envtest/release-notes.sh | 51 ++++++++++++++++++++++++ hack/envtest/windows/Dockerfile | 10 ++++- 7 files changed, 142 insertions(+), 42 deletions(-) create mode 100755 hack/envtest/release-notes.sh diff --git a/.github/workflows/tools-releases.yml b/.github/workflows/tools-releases.yml index 7e195c387..c298cb5b7 100644 --- a/.github/workflows/tools-releases.yml +++ b/.github/workflows/tools-releases.yml @@ -4,31 +4,16 @@ on: push: branches: - main + - tools-releases paths: - 'hack/envtest/_matrix/*.yaml' permissions: - contents: read - packages: write + contents: write jobs: build-and-push: runs-on: ubuntu-latest - strategy: - matrix: - os: - - linux - - darwin - arch: - - amd64 - - arm64 - include: - - arch: ppc64le - os: linux - - arch: s390x - os: linux - - arch: amd64 - os: windows steps: - name: Checkout code uses: actions/checkout@9bb56186c3b09b4f86b1c65136769dd318469633 # tag=v4.1.2 @@ -42,9 +27,10 @@ jobs: id: release-version run: | if [[ ${{ steps.changed-files.outputs.all_changed_files_count }} != 1 ]]; then - echo "One Kubernetes patch version files should be changed to create a package, found ${{ steps.changed-files.outputs.all_changed_files_count }}" + echo "One Kubernetes patch version files should be changed for a release, found ${{ steps.changed-files.outputs.all_changed_files_count }}" exit 1 fi + for changed_file in ${{ steps.changed-files.outputs.all_changed_files }}; do export KUBERNETES_VERSION=$(echo "${changed_file}" | grep -oP '(?<=/)[^/]+(?=\.yaml)') echo "KUBERNETES_VERSION=$KUBERNETES_VERSION" >> $GITHUB_ENV @@ -55,25 +41,19 @@ jobs: ETCD_VERSION=$(yq eval '.etcd' $changed_file) echo "ETCD_VERSION=$ETCD_VERSION" >> $GITHUB_ENV done - - name: Set up Docker - uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # tag=v3.3.0 - - name: Log in to the Container registry - uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # tag=v3.1.0 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: Build and push Docker images - uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # tag=v5.3.0 + - name: Build packages + run: | + make release-envtest \ + KUBERNETES_VERSION=${{ env.KUBERNETES_VERSION }} \ + GO_VERSION=${{ env.GO_VERSION }} \ + ETCD_VERSION=${{ env.ETCD_VERSION }} + - name: Release + uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564 # tag=v2.0.4 with: - context: . - file: ./hack/envtest/${{matrix.os}}/Dockerfile - build-args: | - GO_VERSION=${{env.GO_VERSION}} - KUBERNETES_VERSION=${{env.KUBERNETES_VERSION}} - ETCD_VERSION=${{env.ETCD_VERSION}} - OS=${{matrix.os}} - ARCH=${{matrix.arch}} - push: true - tags: | - ghcr.io/kubernetes-sigs/controller-tools/envtest:${{env.KUBERNETES_VERSION}}-${{matrix.os}}-${{matrix.arch}} + name: envtest-${{ env.KUBERNETES_VERSION }} + draft: true + prerelease: true + make_latest: false + files: out/envtest-*.tar.gz + fail_on_unmatched_files: true + body_path: out/release-notes.md diff --git a/.gitignore b/.gitignore index 7a427bdbe..01a75264a 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ *~ # Tools binaries. +out hack/tools/bin junit-report.xml diff --git a/Makefile b/Makefile index 6bc225d3f..4d91d8d5d 100644 --- a/Makefile +++ b/Makefile @@ -92,3 +92,47 @@ clean: ## Cleanup. .PHONY: clean-bin clean-bin: ## Remove all generated binaries. rm -rf hack/tools/bin + +.PHONE: clean-release +clean-release: ## Remove all generated release binaries. + rm -rf $(RELEASE_DIR) + +## -------------------------------------- +## Envtest Build +## -------------------------------------- + +RELEASE_DIR := out + +.PHONY: $(RELEASE_DIR) +$(RELEASE_DIR): + mkdir -p $(RELEASE_DIR)/ + +.PHONY: release-envtest +release-envtest: clean-release ## Build the envtest binaries by operating system. + OS=linux ARCH=amd64 $(MAKE) release-envtest-docker-build + OS=linux ARCH=arm64 $(MAKE) release-envtest-docker-build + OS=linux ARCH=ppc64le $(MAKE) release-envtest-docker-build + OS=linux ARCH=s390x $(MAKE) release-envtest-docker-build + OS=darwin ARCH=amd64 $(MAKE) release-envtest-docker-build + OS=darwin ARCH=arm64 $(MAKE) release-envtest-docker-build + OS=windows ARCH=amd64 $(MAKE) release-envtest-docker-build + ./hack/envtest/release-notes.sh + +.PHONY: release-envtest-docker-build +release-envtest-docker-build: $(RELEASE_DIR) ## Build the envtest binaries. + @: $(if $(GO_VERSION),,$(error GO_VERSION is not set)) + @: $(if $(KUBERNETES_VERSION),,$(error KUBERNETES_VERSION is not set)) + @: $(if $(ETCD_VERSION),,$(error ETCD_VERSION is not set)) + @: $(if $(OS),,$(error OS is not set)) + @: $(if $(ARCH),,$(error ARCH is not set)) + + docker buildx build \ + --file ./hack/envtest/$(OS)/Dockerfile \ + --build-arg GO_VERSION=$(GO_VERSION) \ + --build-arg KUBERNETES_VERSION=$(KUBERNETES_VERSION) \ + --build-arg ETCD_VERSION=$(ETCD_VERSION) \ + --build-arg OS=$(OS) \ + --build-arg ARCH=$(ARCH) \ + --tag sigs.k8s.io/controller-tools/envtest:$(KUBERNETES_VERSION)-$(OS)-$(ARCH) \ + --output type=local,dest=$(RELEASE_DIR) \ + . diff --git a/hack/envtest/darwin/Dockerfile b/hack/envtest/darwin/Dockerfile index 8adf45ace..c5746b5bf 100644 --- a/hack/envtest/darwin/Dockerfile +++ b/hack/envtest/darwin/Dockerfile @@ -51,6 +51,14 @@ RUN curl -sfLO https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/ unzip -o ${ETCD_BASE_NAME}.zip && \ cp ${ETCD_BASE_NAME}/etcd $DEST +# Package into tarball. +RUN tar -czvf /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz $DEST +RUN sha512sum /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz > /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz.sha512 + # Build the final image with the binaries. FROM scratch -COPY --from=builder /controller-tools/envtest /controller-tools/envtest +ARG OS +ARG ARCH +ARG KUBERNETES_VERSION +COPY --from=builder /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz / +COPY --from=builder /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz.sha512 / diff --git a/hack/envtest/linux/Dockerfile b/hack/envtest/linux/Dockerfile index d68072ec5..cbc743fd2 100644 --- a/hack/envtest/linux/Dockerfile +++ b/hack/envtest/linux/Dockerfile @@ -45,6 +45,14 @@ RUN curl -sfLO https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/ tar xzf ${ETCD_BASE_NAME}.tar.gz && \ cp ${ETCD_BASE_NAME}/etcd $DEST +# Package into tarball. +RUN tar -czvf /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz $DEST +RUN sha512sum /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz > /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz.sha512 + # Build the final image with the binaries. FROM scratch -COPY --from=builder /controller-tools/envtest /controller-tools/envtest +ARG OS +ARG ARCH +ARG KUBERNETES_VERSION +COPY --from=builder /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz / +COPY --from=builder /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz.sha512 / diff --git a/hack/envtest/release-notes.sh b/hack/envtest/release-notes.sh new file mode 100755 index 000000000..88fa97029 --- /dev/null +++ b/hack/envtest/release-notes.sh @@ -0,0 +1,51 @@ +#!/usr/bin/env bash +# Copyright 2024 The Kubernetes Authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +set -o errexit +set -o nounset +set -o pipefail +set -x + +ROOT=$(dirname "${BASH_SOURCE[0]}")/../.. + +# This script is used to build the test binaries for the envtest package. +if [ -z "${KUBERNETES_VERSION}" ]; then + echo "Missing KUBERNETES_VERSION environment variable" + exit 1 +fi + +# For each file in out/*.tar.gz, build a out/release-notes.md files containing a table +# with every file and their respective hash + +# Create the release notes file +echo -e "# Envtest Kubernetes ${KUBERNETES_VERSION} Binaries\n" > out/release-notes.md + +# Create the table header +echo "filename | sha512 hash" >> out/release-notes.md +echo "-------- | -----------" >> out/release-notes.md + +for file in "${ROOT}"/out/*.tar.gz; do + # Get the file name + file_name=$(basename "${file}") + + # Get the hash of the file + file_hash=$(awk '{ print $1 }' < "${file}.sha512") + + # Add the file and hash to the release notes + echo "| [${file_name}](https://github.com/kubernetes-sigs/controller-tools/releases/download/envtest-${KUBERNETES_VERSION}/${file_name}) | ${file_hash} |" >> out/release-notes.md +done + +# Close the table +echo "" >> "${ROOT}"/out/release-notes.md diff --git a/hack/envtest/windows/Dockerfile b/hack/envtest/windows/Dockerfile index a74a73c0d..81ee2188c 100644 --- a/hack/envtest/windows/Dockerfile +++ b/hack/envtest/windows/Dockerfile @@ -51,6 +51,14 @@ RUN curl -sfLO https://github.com/coreos/etcd/releases/download/${ETCD_VERSION}/ unzip -o ${ETCD_BASE_NAME}.zip && \ cp ${ETCD_BASE_NAME}/etcd.exe $DEST +# Package into tarball. +RUN tar -czvf /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz $DEST +RUN sha512sum /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz > /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz.sha512 + # Build the final image with the binaries. FROM scratch -COPY --from=builder /controller-tools/envtest /controller-tools/envtest +ARG OS +ARG ARCH +ARG KUBERNETES_VERSION +COPY --from=builder /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz / +COPY --from=builder /envtest-${KUBERNETES_VERSION}-${OS}-${ARCH}.tar.gz.sha512 /