From 52cdc2423b961603a7090abad90755f3392f10a5 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Thu, 12 Oct 2023 18:43:18 +0300 Subject: [PATCH 1/2] added container build workflow --- .dockerignore | 1 + .github/container-matrix.yml | 25 +++++++ .github/workflows/container-build.yml | 100 ++++++++++++++++++++++++++ .github/workflows/release-event.json | 9 +++ Dockerfile | 17 +++++ run.sh | 10 +++ 6 files changed, 162 insertions(+) create mode 100644 .dockerignore create mode 100644 .github/container-matrix.yml create mode 100644 .github/workflows/container-build.yml create mode 100644 .github/workflows/release-event.json create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..a988499 --- /dev/null +++ b/.dockerignore @@ -0,0 +1 @@ +**/*.log diff --git a/.github/container-matrix.yml b/.github/container-matrix.yml new file mode 100644 index 0000000..c5ce59a --- /dev/null +++ b/.github/container-matrix.yml @@ -0,0 +1,25 @@ +# Copyright 2023 Nokia +# Licensed under the BSD 3-Clause License. +# SPDX-License-Identifier: BSD-3-Clause + +# a matrix list of variables used in the container build process +# +# python version are taken from ansible's support matrix - https://docs.ansible.com/ansible/latest/reference_appendices/release_and_maintenance.html#support-life +include: + # 2.14.10 + - ansible-core-image: "2.14.10:pypy3.10" + runs-on: "ubuntu-22.04" + + - ansible-core-image: "2.14.10:py3.11" + runs-on: "ubuntu-22.04" + addional-tags: "latest" + + # 2.15.4 + # - base-image: "pypy:3.10-slim" + # ansible-core-version: "2.15.4" + # runs-on: "ubuntu-22.04" + + # - base-image: "python:3.11-slim" + # ansible-core-version: "2.15.4" + # runs-on: "ubuntu-22.04" + # addional-tags: "latest" diff --git a/.github/workflows/container-build.yml b/.github/workflows/container-build.yml new file mode 100644 index 0000000..b0677ca --- /dev/null +++ b/.github/workflows/container-build.yml @@ -0,0 +1,100 @@ +name: Container build +"on": + release: + types: + - released + workflow_dispatch: + +jobs: + prepare-matrix: + runs-on: ubuntu-22.04 + outputs: + matrix: ${{ steps.matrix.outputs.output }} + steps: + - name: Dump GitHub context + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + run: | + echo "$GITHUB_CONTEXT" + - uses: actions/checkout@v3 + - uses: fabasoad/data-format-converter-action@main + id: matrix + with: + input: ".github/container-matrix.yml" + from: "yaml" + to: "json" + + build: + name: Build container + runs-on: ubuntu-22.04 + needs: prepare-matrix + strategy: + # fail-fast: false + matrix: ${{ fromJson(needs.prepare-matrix.outputs.matrix) }} + + steps: + - name: Dump GitHub context + env: + GITHUB_CONTEXT: ${{ toJson(github) }} + run: | + echo "$GITHUB_CONTEXT" + + - name: Checkout + uses: actions/checkout@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Process ansible core image version + id: core-image + run: | + CORE_VERSION=$(./run.sh _transformAnsibleCoreVersion "${{ matrix.ansible-core-image }}") + echo "::set-output name=core-version::$CORE_VERSION" + + - name: Docker meta + id: meta + uses: docker/metadata-action@v5 + with: + images: | + ghcr.io/${{ github.repository }}/${{ steps.core-image.outputs.core-version }} + tags: | + type=ref,event=tag + type=ref,event=branch + # git short commit + type=sha + + - name: Build + uses: docker/build-push-action@v4 + with: + context: . + load: true + build-args: | + BASE_IMAGE=ghcr.io/srl-labs/ansible-core/${{ matrix.ansible-core-image }} + tags: app:latest + + - name: Test + run: | + sudo docker run --rm app:latest ansible-galaxy collection list | grep -q 'nokia.srlinux' || exit 1 + + - name: Login to GitHub Container Registry + if: "!github.event.act" + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Set up QEMU + if: "!github.event.act" + uses: docker/setup-qemu-action@v3 + + - name: Build and Push + if: "!github.event.act" + uses: docker/build-push-action@v4 + with: + context: . + push: true + platforms: linux/amd64,linux/arm64 + build-args: | + BASE_IMAGE=ghcr.io/srl-labs/ansible-core/${{ matrix.ansible-core-image }} + tags: ${{ steps.meta.outputs.tags }} diff --git a/.github/workflows/release-event.json b/.github/workflows/release-event.json new file mode 100644 index 0000000..fdb0606 --- /dev/null +++ b/.github/workflows/release-event.json @@ -0,0 +1,9 @@ +{ + "act": true, + "action": "created", + "ref": "refs/tags/v0.0.1", + "sha": "b7e12928f13caf61af40d3e8788649a1a8f24c22", + "release": { + "tag_name": "v0.0.1" + } +} \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b698d33 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +ARG BASE_IMAGE=ghcr.io/srl-labs/ansible-core/2.14.10:pypy3.10 + +FROM ${BASE_IMAGE} as builder + +COPY . /work + +WORKDIR /work + +RUN ansible-galaxy collection install --no-cache .. + +FROM ${BASE_IMAGE} + +COPY --from=builder /root/.ansible/collections /root/.ansible/collections + +WORKDIR /ansible + +CMD [ "ansible-playbook" ] \ No newline at end of file diff --git a/run.sh b/run.sh index ca9f4ff..f533b4b 100755 --- a/run.sh +++ b/run.sh @@ -27,6 +27,11 @@ function _cdTests() { fi } +# transoforms ansible core version by swapping : with / +function _transformAnsibleCoreVersion() { + echo "${1}" | sed 's/:/\//g' +} + # ----------------------------------------------------------------------------- # Install functions. # ----------------------------------------------------------------------------- @@ -311,6 +316,11 @@ function sanity-test { remove-local-collection } +# testing gh workflow +function test-act-release { + gh act release -W '.github/workflows/container-build.yml' -e .github/workflows/release-event.json -s GITHUB_TOKEN="$(gh auth token)" --matrix ansible-core-image:2.14.10:py3.11 +} + # ----------------------------------------------------------------------------- # Publish functions. # ----------------------------------------------------------------------------- From 9a3c077c0fa7915f0be4659f266ded65353595e6 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Thu, 12 Oct 2023 19:48:53 +0300 Subject: [PATCH 2/2] remove matrix entries with older ansible-core --- .github/matrix.yml | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/.github/matrix.yml b/.github/matrix.yml index 8a35a02..b72a656 100644 --- a/.github/matrix.yml +++ b/.github/matrix.yml @@ -6,20 +6,7 @@ # kind of a manual way of creating a testing matrix with a flexibility of selecting permuatations # support matrix for ansible control node - https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html#node-requirement-summary include: - # Python 3.6 - - python-version: "3.6.15" - ansible-core-version: "2.11.11" - runs-on: "ubuntu-20.04" - - # Python 3.7 - - python-version: "3.7" - ansible-core-version: "2.11.11" - runs-on: "ubuntu-22.04" - # Python 3.8 - - python-version: "3.8" - ansible-core-version: "2.11.11" - runs-on: "ubuntu-22.04" - python-version: "3.8" ansible-core-version: "2.12.10" runs-on: "ubuntu-22.04" @@ -28,9 +15,6 @@ include: runs-on: "ubuntu-22.04" # Python 3.9 - - python-version: "3.9" - ansible-core-version: "2.11.11" - runs-on: "ubuntu-22.04" - python-version: "3.9" ansible-core-version: "2.12.10" runs-on: "ubuntu-22.04"