From 88ef72ac21f66152c9073a06d4845ccd90f52acd Mon Sep 17 00:00:00 2001 From: Divyanshu Agrawal Date: Tue, 19 Oct 2021 10:24:09 +0530 Subject: [PATCH] Add test workflow for multi-arch build from scratch (#84) Signed-off-by: divyansh42 --- .github/workflows/multiarch.yml | 120 ++++++++++++++++++++++++++++++-- 1 file changed, 115 insertions(+), 5 deletions(-) diff --git a/.github/workflows/multiarch.yml b/.github/workflows/multiarch.yml index e72f9f1..a05507e 100644 --- a/.github/workflows/multiarch.yml +++ b/.github/workflows/multiarch.yml @@ -7,12 +7,15 @@ on: - cron: '0 0 * * *' # every day at midnight env: - IMAGE_NAME: hello-world-multiarch + PROJECT_DIR: spring-petclinic + MVN_REPO_DIR: ~/.m2/repository IMAGE_TAG: latest jobs: - build-multiarch: - name: Build multi-architecture image using Buildah + build-multiarch-containerfile: + name: Build multi-architecture image using Containerfile + env: + IMAGE_NAME: hello-world-multiarch runs-on: ubuntu-20.04 strategy: fail-fast: false @@ -80,8 +83,10 @@ jobs: run: | podman run --rm ${{ steps.build_image_multiarch.outputs.image }}:${{ env.IMAGE_TAG }} - build-multiplatform: - name: Build multi-platform image using Buildah + build-multiplatform-containerfile: + name: Build multi-platform image using Containerfile + env: + IMAGE_NAME: hello-world-multiplatform runs-on: ubuntu-20.04 strategy: fail-fast: false @@ -146,3 +151,108 @@ jobs: - name: Run image run: | podman run --rm ${{ steps.build_image_multiplatform.outputs.image }}:${{ env.IMAGE_TAG }} + + build-multiarch-scratch: + name: Build multi-architecture image from scratch + env: + IMAGE_NAME: spring-petclinic-multiarch + runs-on: ubuntu-20.04 + strategy: + fail-fast: false + matrix: + arch: [ amd64, i386, arm64v8 ] + install_latest: [ true, false ] + + steps: + + # Checkout buildah action github repository + - name: Checkout Buildah action + uses: actions/checkout@v2 + with: + path: "buildah-build" + + - name: Install latest buildah + if: matrix.install_latest + run: | + bash buildah-build/.github/install_latest_buildah.sh + + - name: Install qemu dependency + run: | + sudo apt-get update + sudo apt-get install -y qemu-user-static + + # Checkout spring-petclinic github repository + - name: Checkout spring-petclinic project + uses: actions/checkout@v2 + with: + repository: "spring-projects/spring-petclinic" + path: ${{ env.PROJECT_DIR }} + + # If none of these files has changed, we assume that the contents of + # .m2/repository can be fetched from the cache. + - name: Hash Maven files + working-directory: ${{ env.PROJECT_DIR }} + run: | + echo "MVN_HASH=${{ hashFiles('**/pom.xml', '.mvn/**/*', 'mvnw*') }}" >> $GITHUB_ENV + + # Download the m2 repository from the cache to speed up the build. + - name: Check for Maven cache + id: check-mvn-cache + uses: actions/cache@v2 + with: + path: ${{ env.MVN_REPO_DIR }} + key: ${{ env.MVN_HASH }} + + # Setup java. + - name: Setup Java + uses: actions/setup-java@v1 + with: + java-version: 11 + + # Run maven to build the project + - name: Maven + working-directory: ${{ env.PROJECT_DIR }} + run: | + mvn package -ntp -B + + # If there was no cache hit above, store the output into the cache now. + - name: Save Maven repo into cache + if: ${{ steps.check-mvn-cache.outputs.cache-hit }} != 'true' + uses: actions/cache@v2 + with: + path: ${{ env.MVN_REPO_DIR }} + key: ${{ env.MVN_HASH }} + + - name: Build Image + id: build_image_multiarch + uses: ./buildah-build/ + with: + image: ${{ env.IMAGE_NAME }} + tags: ${{ env.IMAGE_TAG }} + base-image: 'registry.access.redhat.com/openjdk/openjdk-11-rhel7' + arch: ${{ matrix.arch }} + build-args: ARCH=${{ matrix.arch }} + # To avoid hardcoding a particular version of the binary. + content: | + ./spring-petclinic/target/spring-petclinic-*.jar + entrypoint: | + java + -jar + spring-petclinic-*.jar + port: 8080 + workdir: "." + + - name: Echo Outputs + run: | + echo "Image: ${{ steps.build_image_multiarch.outputs.image }}" + echo "Tags: ${{ steps.build_image_multiarch.outputs.tags }}" + echo "Tagged Image: ${{ steps.build_image_multiarch.outputs.image-with-tag }}" + + - name: Check images created + run: buildah images | grep '${{ env.IMAGE_NAME }}' + + - name: Check image metadata + run: | + set -x + buildah inspect ${{ steps.build_image_multiarch.outputs.image }}:${{ env.IMAGE_TAG }} | jq ".OCIv1.architecture" + buildah inspect ${{ steps.build_image_multiarch.outputs.image }}:${{ env.IMAGE_TAG }} | jq ".Docker.architecture"