From 5440c0622a38b390fa94decf21f1a5aa4d81130c Mon Sep 17 00:00:00 2001 From: Oscar Ferrer Date: Wed, 2 Oct 2024 10:34:35 +0200 Subject: [PATCH] Build the image in GHA Context ------- Added the workflow to build the image in GHA and modified the Dockerfile to work on it. The build action is the same as https://github.com/adevinta/noe but with golang 1.23 Change-Id: I30584aba676613cde91987aedc021a1773916d5e --- .github/workflows/build.yaml | 55 ++++++++++++++++++++++++++++++++++++ Dockerfile | 3 +- 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/build.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml new file mode 100644 index 0000000..c94dfba --- /dev/null +++ b/.github/workflows/build.yaml @@ -0,0 +1,55 @@ +name: build images + +on: + workflow_call: + push: + branches: [ "*", "**/*" ] + +jobs: + + buildImages: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-go@v3 + with: + go-version: '^1.23' + + - name: Set up Docker context for Buildx + id: buildx-context + run: | + docker context create builders + - name: Set up Docker Buildx + id: buildx + uses: docker/setup-buildx-action@v2 + with: + version: latest + endpoint: builders + + - name: Login to Container Registry + uses: docker/login-action@v2 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build images + id: buildimages + run: | + img=ghcr.io/${{github.repository}}:${{github.sha}} + echo "::set-output name=IMAGE_NAME::${img}" + docker pull ${img} || ( + docker buildx build --push --cache-to type=gha,mode=max --cache-from type=gha --progress plain --platform linux/arm64/v8,linux/amd64 --build-arg BUILDKIT_INLINE_CACHE=1 -t ${img} . + ) + + - name: Build main image images + id: buildmainimage + if: ${{ github.ref == 'refs/heads/main' && github.event_name != 'pull_request' }} + run: | + img=ghcr.io/${{github.repository}}:latest + echo "::set-output name=IMAGE_NAME::${img}" + docker buildx build --push --cache-to type=gha,mode=max --cache-from type=gha --progress plain --platform linux/arm64/v8,linux/amd64 --build-arg BUILDKIT_INLINE_CACHE=1 -t ${img} . + diff --git a/Dockerfile b/Dockerfile index 37350c2..eecc595 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,6 @@ WORKDIR /workspace # Copy the Go Modules manifests COPY go.mod go.mod COPY go.sum go.sum -COPY vendor/ vendor/ # Copy the go source COPY cmd cmd @@ -13,7 +12,7 @@ COPY cmd cmd COPY pkg/ pkg/ # Build -RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -mod=vendor -a -o manager ./cmd/traffic-controller +RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager ./cmd/traffic-controller # Use distroless as minimal base image to package the manager binary # Refer to https://github.com/GoogleContainerTools/distroless for more details