From 1b0bb678ab8d6609776ecadad09d38c247464af9 Mon Sep 17 00:00:00 2001 From: "Bobby R. Bruce" Date: Thu, 31 Aug 2023 02:16:35 -0700 Subject: [PATCH] util-docker: Proof-of-concept using Docker buildx Introduced in https://github.com/gem5/gem5/pull/236 the "docker-build.yaml" file will allow us to build and push docker images to the GitHub Container Registry. This allows for both automation of docker image building and allows us to utilize Github's zero-cost pulling policy for downloads to GitHub Actions runners. In this PR https://github.com/gem5/gem5/pull/236 has been altered to use Docker `buildx` which allows for multi-platform Docker Image builds. A multi-platform Docker image pull automatically pull the correct image for your platform from a single URL. In this prototype the images are build to both `linux/arm64` and `linux/amd64` have been set. Docker `buildx` has it's own file format for specifying image builds called `bake`. "util/dockerfiles/docker-bake.hcl" has been added with the goal of replacing "util/dockerfiles/docker-compose.yaml". In this proof-of-concept doesn't build all our docker images, just enough to ensure it works inside our actions as intended. Change-Id: Id0debed216c91ec514aa4fce3bc2ff4fc2ea669b --- .github/workflows/docker-build.yaml | 47 +++++------ util/dockerfiles/docker-bake.hcl | 83 +++++++++++++++++++ .../ubuntu-20.04_all-dependencies/Dockerfile | 2 +- .../ubuntu-20.04_clang-version/Dockerfile | 2 +- .../ubuntu-22.04_all-dependencies/Dockerfile | 2 +- 5 files changed, 108 insertions(+), 28 deletions(-) create mode 100644 util/dockerfiles/docker-bake.hcl diff --git a/.github/workflows/docker-build.yaml b/.github/workflows/docker-build.yaml index cbacaad9aa..c46c7f7fb8 100644 --- a/.github/workflows/docker-build.yaml +++ b/.github/workflows/docker-build.yaml @@ -1,42 +1,39 @@ -# name: Docker images build and push on: - push: - branches: - - develop - paths: - - 'util/docker/ubuntu-20.04_all-depenencies' workflow_dispatch: - -env: - IMAGE_NAME: ubuntu-20.04_all-depenencies -# jobs: # This builds and pushes the docker image. - push: + build-and-push: runs-on: [self-hosted, linux, x64, run] permissions: packages: write contents: read + container: gcr.io/gem5-test/ubuntu-22.04_all-dependencies:latest steps: - uses: actions/checkout@v3 + name: Checkout the develop branch + with: + # Scheduled workflows run on the default branch by default. We + # therefore need to explicitly checkout the develop branch. + ref: develop - - name: Build image - run: | - cd util/docker/ubuntu-20.04_all-depenencies - docker build . --file Dockerfile --tag $IMAGE_NAME --label "runnumber=${GITHUB_RUN_ID}" + - uses: docker/setup-qemu-action@v2 + name: Setup QEMU - - name: Log in to registry - run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u $ --password-stdin + - uses: docker/setup-buildx-action@v2 + name: Set up Docker Buildx - - name: Push image - run: | - IMAGE_ID=ghcr.io/${{ github.repository_owner }}/$IMAGE_NAME + - uses: docker/login-action@v2 + name: Login to the GitHub Container Registry + with: + registry: ghcr.io + username: ${{ github.repository_owner }} + password: ${{ secrets.GITHUB_TOKEN }} - # This changes all uppercase characters to lowercase. - IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]') - - docker tag $IMAGE_NAME $IMAGE_ID:latest - docker push $IMAGE_ID::latest + - name: Build and push with bake + uses: docker/bake-action@v3 + with: + files: util/dockerfiles/docker-bake.hcl + push: true diff --git a/util/dockerfiles/docker-bake.hcl b/util/dockerfiles/docker-bake.hcl new file mode 100644 index 0000000000..d508ecd45e --- /dev/null +++ b/util/dockerfiles/docker-bake.hcl @@ -0,0 +1,83 @@ +# Copyright (c) 2023 The Regents of the University of California +# All Rights Reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are +# met: redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer; +# redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution; +# neither the name of the copyright holders nor the names of its +# contributors may be used to endorse or promote products derived from +# this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +# docker buildx bake --push +# https://docs.docker.com/build/bake/reference + +variable "IMAGE_URI" { + default = "ghcr.io/gem5" # The gem5 GitHub container registry. +} + +variable "TAG" { + default = "latest" +} + +# A group of targets to be built. Note: groups can contain other groups. +# Any target or group can be build individually. I.e.: +# `docker buildx bake --push ubuntu-20-04_all-dependencies` or +# `docker buildx bake --push ubuntu-releases`. +group "default" { + targets=["clang-compilers", "ubuntu-releases"] +} + +group "ubuntu-releases" { + targets=["ubuntu-22-04_all-dependencies", "ubuntu-20-04_all-dependencies"] +} + +# Common attributes across all targets. Note: these can be overwritten. +target "common" { + # Here we are enabling multi-platform builds. We are compiling to both ARM + # amd X86. + platforms = ["linux/amd64", "linux/arm64"] +} + +target "clang-compilers" { + name="clang-compilers-${replace(ver, ".", "-")}" + inherits = ["common"] + context = "ubuntu-20.04_clang-version" + dockerfile = "Dockerfile" + matrix = { + ver = ["6.0","7","8","9","10","11"] + } + args = { + version=ver + } + tags = ["${IMAGE_URI}/clang-version-${ver}:${TAG}"] +} + +target "ubuntu-22-04_all-dependencies" { + inherits = ["common"] + dockerfile = "Dockerfile" + context = "ubuntu-22.04_all-dependencies" + tags = ["${IMAGE_URI}/ubuntu-22.04_all-dependencies:${TAG}"] +} + +target "ubuntu-20-04_all-dependencies" { + inherits = ["common"] + dockerfile = "Dockerfile" + context = "ubuntu-20.04_all-dependencies" + tags = ["${IMAGE_URI}/ubuntu-20.04_all-dependencies:${TAG}"] +} diff --git a/util/dockerfiles/ubuntu-20.04_all-dependencies/Dockerfile b/util/dockerfiles/ubuntu-20.04_all-dependencies/Dockerfile index c838a06dda..449f8b1b06 100644 --- a/util/dockerfiles/ubuntu-20.04_all-dependencies/Dockerfile +++ b/util/dockerfiles/ubuntu-20.04_all-dependencies/Dockerfile @@ -24,7 +24,7 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -FROM ubuntu:20.04 +FROM --platform=${BUILDPLATFORM} ubuntu:20.04 ENV DEBIAN_FRONTEND=noninteractive RUN apt -y update && apt -y upgrade && \ diff --git a/util/dockerfiles/ubuntu-20.04_clang-version/Dockerfile b/util/dockerfiles/ubuntu-20.04_clang-version/Dockerfile index f1ecf89105..2ea9413c7d 100644 --- a/util/dockerfiles/ubuntu-20.04_clang-version/Dockerfile +++ b/util/dockerfiles/ubuntu-20.04_clang-version/Dockerfile @@ -23,7 +23,7 @@ # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -FROM ubuntu:20.04 +FROM --platform=${BUILDPLATFORM} ubuntu:20.04 # Valid version values: # 6.0 diff --git a/util/dockerfiles/ubuntu-22.04_all-dependencies/Dockerfile b/util/dockerfiles/ubuntu-22.04_all-dependencies/Dockerfile index e5afc63be6..9e2580e642 100644 --- a/util/dockerfiles/ubuntu-22.04_all-dependencies/Dockerfile +++ b/util/dockerfiles/ubuntu-22.04_all-dependencies/Dockerfile @@ -24,7 +24,7 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -FROM ubuntu:22.04 +FROM --platform=${BUILDPLATFORM} ubuntu:22.04 ENV DEBIAN_FRONTEND=noninteractive RUN apt -y update && apt -y upgrade && \