From d334c2362f8e653f582f6f95de2e30aeb5ce7aa3 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Fri, 28 Jun 2024 22:59:20 +0200 Subject: [PATCH] Create helper OCI images for use in CI. (#899) --- .github/workflows/docker-images.yml | 67 +++++++++++++++++++++++++++++ tests/images/build.sh | 26 +++++++++++ tests/images/simple-1/build.sh | 1 + tests/images/simple-1/main.go | 34 +++++++++++++++ tests/images/simple-2/build.sh | 1 + tests/images/simple-2/main.go | 34 +++++++++++++++ 6 files changed, 163 insertions(+) create mode 100644 .github/workflows/docker-images.yml create mode 100755 tests/images/build.sh create mode 120000 tests/images/simple-1/build.sh create mode 100644 tests/images/simple-1/main.go create mode 120000 tests/images/simple-2/build.sh create mode 100644 tests/images/simple-2/main.go diff --git a/.github/workflows/docker-images.yml b/.github/workflows/docker-images.yml new file mode 100644 index 00000000..af6c2219 --- /dev/null +++ b/.github/workflows/docker-images.yml @@ -0,0 +1,67 @@ +--- +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +name: Helper Docker images for testing +on: + # Run CI against all pushes (direct commits, also merged PRs), Pull Requests + push: + branches: + - main + paths: + - .github/workflows/docker-images.yml + - tests/images/** + pull_request: + branches: + - main + paths: + - .github/workflows/docker-images.yml + - tests/images/** + # Run CI once per day (at 03:00 UTC) + schedule: + - cron: '0 3 * * *' + +env: + CONTAINER_REGISTRY: ghcr.io/ansible-collections + +jobs: + build: + name: Build image ${{ matrix.name }}:${{ matrix.tag }} + runs-on: ubuntu-24.04 + strategy: + fail-fast: false + matrix: + include: + - name: simple-1 + tag: latest + - name: simple-2 + tag: latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Install dependencies + run: | + sudo apt-get install podman buildah + + - name: Set up Go 1.22 + uses: actions/setup-go@v5 + with: + go-version: '1.22' + + - name: Build ${{ matrix.name }} image + run: | + ./build.sh "${CONTAINER_REGISTRY}/${{ matrix.name }}:${{ matrix.tag }}" + working-directory: tests/images/${{ matrix.name }} + + - name: Publish container image as ${{ env.CONTAINER_REGISTRY }}/${{ matrix.name }}:latest + if: github.event_name != 'pull_request' + uses: redhat-actions/push-to-registry@v2 + with: + registry: ${{ env.CONTAINER_REGISTRY }} + image: ${{ matrix.name }} + tags: ${{ matrix.tag }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} diff --git a/tests/images/build.sh b/tests/images/build.sh new file mode 100755 index 00000000..f0fad13e --- /dev/null +++ b/tests/images/build.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash +# Copyright (c) Ansible Project +# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) +# SPDX-License-Identifier: GPL-3.0-or-later + +if [ ! -e main.go ]; then + echo "Must be run in a directory that contains main.go." + exit 1 +fi + +set -eux +IMAGE_NAME="${1:-localhost/$(basename "$(pwd)"):latest}" +podman manifest rm "${IMAGE_NAME}" || true +podman image rm "${IMAGE_NAME}" || true +buildah manifest create "${IMAGE_NAME}" +for ARCH in amd64 arm64; do + rm -f "main-${ARCH}" + GOARCH="${ARCH}" go build -ldflags "-s -w" -o "main-${ARCH}" main.go + + WORKING_CONTAINER="$(buildah from --arch "${ARCH}" scratch)" + buildah copy "${WORKING_CONTAINER}" "main-${ARCH}" "/runme" + buildah config --entrypoint '["/runme"]' "${WORKING_CONTAINER}" + buildah commit --manifest "${IMAGE_NAME}" "${WORKING_CONTAINER}" + + rm -f "main-${ARCH}" +done diff --git a/tests/images/simple-1/build.sh b/tests/images/simple-1/build.sh new file mode 120000 index 00000000..669ce2a6 --- /dev/null +++ b/tests/images/simple-1/build.sh @@ -0,0 +1 @@ +../build.sh \ No newline at end of file diff --git a/tests/images/simple-1/main.go b/tests/images/simple-1/main.go new file mode 100644 index 00000000..8af90a11 --- /dev/null +++ b/tests/images/simple-1/main.go @@ -0,0 +1,34 @@ +/* + Copyright (c) Ansible Project + GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) + SPDX-License-Identifier: GPL-3.0-or-later +*/ + +package main + +import ( + "fmt" + "os" + "time" +) + +func main() { + var delay time.Duration + if len(os.Args) > 2 { + fmt.Fprintf(os.Stderr, "%s must have 0 or 1 argument, not %d arguments\n", os.Args[0], len(os.Args)) + os.Exit(1) + } else if len(os.Args) == 2 { + var err error + delay, err = time.ParseDuration(os.Args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, "Cannot parse delay duration: %s\n", err) + os.Exit(1) + } + if delay.Microseconds() < 0 { + fmt.Fprintf(os.Stderr, "Delay must not be negative!\n") + os.Exit(1) + } + } + fmt.Println("Hello!") + time.Sleep(delay) +} diff --git a/tests/images/simple-2/build.sh b/tests/images/simple-2/build.sh new file mode 120000 index 00000000..669ce2a6 --- /dev/null +++ b/tests/images/simple-2/build.sh @@ -0,0 +1 @@ +../build.sh \ No newline at end of file diff --git a/tests/images/simple-2/main.go b/tests/images/simple-2/main.go new file mode 100644 index 00000000..d505090b --- /dev/null +++ b/tests/images/simple-2/main.go @@ -0,0 +1,34 @@ +/* + Copyright (c) Ansible Project + GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) + SPDX-License-Identifier: GPL-3.0-or-later +*/ + +package main + +import ( + "fmt" + "os" + "time" +) + +func main() { + var delay time.Duration + if len(os.Args) > 2 { + fmt.Fprintf(os.Stderr, "%s must have 0 or 1 argument, not %d arguments\n", os.Args[0], len(os.Args)) + os.Exit(1) + } else if len(os.Args) == 2 { + var err error + delay, err = time.ParseDuration(os.Args[1]) + if err != nil { + fmt.Fprintf(os.Stderr, "Cannot parse delay duration: %s\n", err) + os.Exit(1) + } + if delay.Microseconds() < 0 { + fmt.Fprintf(os.Stderr, "Delay must not be negative!\n") + os.Exit(1) + } + } + fmt.Println("World!") + time.Sleep(delay) +}