From 587ebe5c2e3b7ca4edd2ead5b632375aea193c7e Mon Sep 17 00:00:00 2001 From: Caleb Woodbine Date: Mon, 2 Oct 2023 11:13:17 +1300 Subject: [PATCH] feat: add ko-build tests test ko build functions --- .github/workflows/reusable-ko-build.yml | 6 +- .github/workflows/test-reusable-ko-build.yml | 159 +++++++++++++++++++ go.mod | 3 + 3 files changed, 165 insertions(+), 3 deletions(-) create mode 100644 .github/workflows/test-reusable-ko-build.yml create mode 100644 go.mod diff --git a/.github/workflows/reusable-ko-build.yml b/.github/workflows/reusable-ko-build.yml index 8d93acc..cd01d6f 100644 --- a/.github/workflows/reusable-ko-build.yml +++ b/.github/workflows/reusable-ko-build.yml @@ -90,6 +90,9 @@ jobs: go-version-file: go.mod cache-dependency-path: go.sum check-latest: true + - name: setup + run: | + eval '${{ inputs.setup }}' - id: run-info name: collect job run info env: @@ -109,9 +112,6 @@ jobs: with: version: ${{ env.VERSION_CRANE }} - uses: GeoNet/setup-ko@190558b6b0e4ebe7e2caa5d99309563d1bceaa8d # main - - name: setup - run: | - eval '${{ inputs.setup }}' - name: get session name id: get-session-name if: ${{ inputs.aws-region != '' && inputs.aws-role-arn-to-assume != '' && inputs.aws-role-duration-seconds != '' && inputs.registryOverride != '' }} diff --git a/.github/workflows/test-reusable-ko-build.yml b/.github/workflows/test-reusable-ko-build.yml new file mode 100644 index 0000000..f4119c8 --- /dev/null +++ b/.github/workflows/test-reusable-ko-build.yml @@ -0,0 +1,159 @@ +name: test reusable-ko-build +on: + pull_request: {} + workflow_dispatch: {} +env: + VERSION_CRANE: v0.16.1 + +# LIMITATIONS +# - this workflow cannot run in parallel between PRs; retries are required (for now). +# this is due to non-unique image names. having unique image names tends to cause needing to retry more times due to some sort of internal container artifact "registration" behaviour + +# TESTCASES +# - t0: basic +# - t1: auth-with-geonetci +# - t2: with-paths-and-packages +# - t3: no-push + +permissions: + packages: write + contents: read + id-token: write +jobs: + t0-basic: + uses: ./.github/workflows/reusable-ko-build.yml + with: + push: true + setup: | + mkdir -p testdata-ko-build/cmd/testimage-ko-build-t0-basic-1 + cat << EOF > testdata-ko-build/cmd/testimage-ko-build-t0-basic-1/main.go + package main + import "fmt" + func main() { + fmt.Println("Hello World!") + } + EOF + t0-basic-check: + needs: t0-basic + runs-on: ubuntu-latest + steps: + - uses: GeoNet/setup-crane@00c9e93efa4e1138c9a7a5c594acd6c75a2fbf0c # main + with: + version: ${{ env.VERSION_CRANE }} + - name: check for images + env: + IMAGES: ${{ needs.t0-basic.outputs.images }} + run: | + echo "$IMAGES" | tr ',' '\n' | xargs -n 1 -I{} crane manifest {} + - name: cleanup + if: always() + env: + GH_TOKEN: ${{ github.token }} + run: | + gh api -X DELETE /orgs/GeoNet/packages/container/actions%2Ftestimage-ko-build-t0-basic-1 || true + t1-auth-with-geonetci: + uses: ./.github/workflows/reusable-ko-build.yml + secrets: inherit + with: + push: true + registryGhcrUsernameOverride: geonetci + setup: | + mkdir -p testdata-ko-build/cmd/testimage-ko-build-t1-auth-with-geonetci-1 + cat << EOF > testdata-ko-build/cmd/testimage-ko-build-t1-auth-with-geonetci-1/main.go + package main + import "fmt" + func main() { + fmt.Println("Hello World!") + } + EOF + t1-auth-with-geonetci-check: + needs: t1-auth-with-geonetci + runs-on: ubuntu-latest + steps: + - uses: GeoNet/setup-crane@00c9e93efa4e1138c9a7a5c594acd6c75a2fbf0c # main + with: + version: ${{ env.VERSION_CRANE }} + - name: check for images + env: + IMAGES: ${{ needs.t1-auth-with-geonetci.outputs.images }} + run: | + echo "$IMAGES" | tr ',' '\n' | xargs -n 1 -I{} crane manifest {} + - name: cleanup + if: always() + env: + GH_TOKEN: ${{ github.token }} + run: | + gh api -X DELETE /orgs/GeoNet/packages/container/actions%2Ftestimage-ko-build-t1-auth-with-geonetci-1 || true + t2-with-paths-and-packages: + uses: ./.github/workflows/reusable-ko-build.yml + with: + push: true + paths: | + ./testdata-ko-build/cmd/testimage-ko-build-t2-with-paths-and-packages-1 + github.com/GeoNet/Actions/testdata-ko-build/cmd/testimage-ko-build-t2-with-paths-and-packages-2 + setup: | + mkdir -p testdata-ko-build/cmd/testimage-ko-build-t2-with-paths-and-packages-1 + cat << EOF > testdata-ko-build/cmd/testimage-ko-build-t2-with-paths-and-packages-1/main.go + package main + import "fmt" + func main() { + fmt.Println("Hello World!") + } + EOF + mkdir -p testdata-ko-build/cmd/testimage-ko-build-t2-with-paths-and-packages-2 + cat << EOF > testdata-ko-build/cmd/testimage-ko-build-t2-with-paths-and-packages-2/main.go + package main + import "fmt" + func main() { + fmt.Println("Hello World!") + } + EOF + t2-with-paths-and-packages-check: + needs: t2-with-paths-and-packages + runs-on: ubuntu-latest + steps: + - uses: GeoNet/setup-crane@00c9e93efa4e1138c9a7a5c594acd6c75a2fbf0c # main + with: + version: ${{ env.VERSION_CRANE }} + - name: check for images + env: + IMAGES: ${{ needs.t2-with-paths-and-packages.outputs.images }} + run: | + echo "$IMAGES" | tr ',' '\n' | sort | cut -d'@' -f1 | xargs | grep -qE '^ghcr.io/geonet/actions/testimage-ko-build-t2-with-paths-and-packages-1 ghcr.io/geonet/actions/testimage-ko-build-t2-with-paths-and-packages-2$' + echo "$IMAGES" | tr ',' '\n' | xargs -n 1 -I{} crane manifest {} + - name: cleanup + if: always() + env: + GH_TOKEN: ${{ github.token }} + run: | + gh api -X DELETE /orgs/GeoNet/packages/container/actions%2Ftestimage-ko-build-t2-with-paths-and-packages-1 || true + gh api -X DELETE /orgs/GeoNet/packages/container/actions%2Ftestimage-ko-build-t2-with-paths-and-packages-2 || true + t3-no-push: + uses: ./.github/workflows/reusable-ko-build.yml + with: + push: false + setup: | + mkdir -p testdata-ko-build/cmd/testimage-ko-build-t3-no-push-1 + cat << EOF > testdata-ko-build/cmd/testimage-ko-build-t3-no-push-1/main.go + package main + import "fmt" + func main() { + fmt.Println("Hello World!") + } + EOF + t3-no-push-check: + needs: t3-no-push + runs-on: ubuntu-latest + steps: + - uses: GeoNet/setup-crane@00c9e93efa4e1138c9a7a5c594acd6c75a2fbf0c # main + with: + version: ${{ env.VERSION_CRANE }} + - name: check for images + env: + IMAGE: ${{ needs.t3-no-push.outputs.images }} + GH_TOKEN: ${{ github.token }} + run: | + ! crane manifest $IMAGE || \ + ( + gh api -X DELETE /orgs/GeoNet/packages/container/actions%2Ftestimage-ko-build-t3-no-push-1 || true + ) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..fe3a195 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/GeoNet/Actions + +go 1.20