goreleaser / goreleaser-cross

Docker image for Golang cross-compiling with CGO
MIT License
134 stars 23 forks source link

go1.20 #26

Closed troian closed 1 year ago

caarlos0 commented 1 year ago

fwiw incoming in goreleaser v1.15.2 in a few hours

maraino commented 1 year ago

I've locally built a goreleaser-cross with Go 1.20 using the Makefile, but there is one extra change that I needed to do. In scripts/build-base.sh we have this:

https://github.com/goreleaser/goreleaser-cross/blob/f12e6938e532c0250a7396cccca31f01e1101375/scripts/build-base.sh#L11

But goreleaser-cross-toolchains is not tagged with v0.1.7-$arch. See https://github.com/goreleaser/goreleaser-cross-toolchains/pkgs/container/goreleaser-cross-toolchains

I'm not a docker expert but I believe it is clever enough to get the right image when you build with --platform=linux/amd64 or --platform=linux/arm64 so I don't think the sed in that file is necessary. This should be enough:

#!/usr/bin/env bash

set -x

arch=$1
image=$2

docker build --platform=linux/${arch} -t ${image} \
$3 -f Dockerfile.base .

We could also get rid of the sed in scripts/build-cross.sh, but we would also need to change the parameter image in the base-% goal, this part -$(@:base-%=%) in the second argument.

As you are waiting for the release of goreleaser I won't send a PR, but if you want I can submit a PR with all those changes. I believe the GitHub actions release won't be affected by this.

Tagging goreleaser-cross-toolchains with the architectures would work too, but I don't think is necessary.

troian commented 1 year ago

the release does not use make targets, but github action. makefile is for building images locally and the reason for this weird substitution is exactly due to the arch is not needed for images located in registry. however, as we cannot create a manifest locally until image is pushed to registry, we add tag to image name to distinguish host images built locally.

if you want to build locally, pull ghcr.io/goreleaser/gorelaser-cross-toolchains:v0.1.7 and tag it ghcr.io/goreleaser/gorelaser-cross-toolchains:v0.1.7-arm64 and ghcr.io/goreleaser/gorelaser-cross-toolchains:v0.1.7-amd64.

troian commented 1 year ago

@maraino just committed helper target pull-toolchains

run:

make pull-toolchains
make base
make goreleaser

little hint. if you need image only for your host arch (for example amd64), add SUBIMAGES=amd64 prior each make, it will build just one

bear in mind, that locally built goreleaser-cross will have -<arch> suffix to it as well

maraino commented 1 year ago

I still need to edit things:

$ docker images
REPOSITORY      TAG               IMAGE ID       CREATED        SIZE
moby/buildkit   buildx-stable-1   fb2b14fffd09   12 days ago    168MB
registry        2                 81c944c2288b   2 months ago   24.1MB

$ make pull-toolchains SUBIMAGES=amd64
pulling toolchain ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7-amd64
docker pull --platform=linux/amd64 ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7
v0.1.7: Pulling from ghcr.io/goreleaser/goreleaser-cross-toolchains
699c8a97647f: Already exists
6729b14fd460: Already exists
25d0ebebb108: Already exists
Digest: sha256:565ac74e6da91abb8f8c5add08f4b0a8a068fd198886ac8e690e3b3f3491eca8
Status: Downloaded newer image for ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7
ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7
docker tag ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7 ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7-amd64

$ docker images
REPOSITORY                                               TAG               IMAGE ID       CREATED        SIZE
ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains   v0.1.7            911216523664   18 hours ago   7.16GB
ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains   v0.1.7-amd64      911216523664   18 hours ago   7.16GB
moby/buildkit                                            buildx-stable-1   fb2b14fffd09   12 days ago    168MB
registry                                                 2                 81c944c2288b   2 months ago   24.1MB

$ make base SUBIMAGES=amd64
building ghcr.io/goreleaser/goreleaser-cross-base:v1.19.1-amd64
./scripts/build-base.sh amd64 ghcr.io/goreleaser/goreleaser-cross-base:v1.19.1-amd64 \
        "--build-arg GO_VERSION=1.20 \
        --build-arg TINI_VERSION=v0.19.0 \
        --build-arg COSIGN_VERSION=1.13.1 \
        --build-arg COSIGN_SHA256=fd5b09be23ef1027e1bdd490ce78dcc65d2b15902e1f4ba8e04f3b4019cc1057 \
        --build-arg DEBIAN_FRONTEND=noninteractive \
        --build-arg TOOLCHAINS_VERSION=v0.1.7"
+ arch=amd64
+ image=ghcr.io/goreleaser/goreleaser-cross-base:v1.19.1-amd64
+ docker build --platform=linux/amd64 -t ghcr.io/goreleaser/goreleaser-cross-base:v1.19.1-amd64 --build-arg GO_VERSION=1.20 --build-arg TINI_VERSION=v0.19.0 --build-arg COSIGN_VERSION=1.13.1 --build-arg COSIGN_SHA256=fd5b09be23ef1027e1bdd490ce78dcc65d2b15902e1f4ba8e04f3b4019cc1057 --build-arg DEBIAN_FRONTEND=noninteractive --build-arg TOOLCHAINS_VERSION=v0.1.7 . -f-
++ cat Dockerfile.base
++ sed 's/goreleaser-cross-toolchains:\$TOOLCHAINS_VERSION/goreleaser-cross-toolchains:\$TOOLCHAINS_VERSION-\$TARGETARCH/'
[+] Building 0.5s (4/4) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                                                                                                                                              0.0s
 => => transferring dockerfile: 1.94kB                                                                                                                                                                                                                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                                                                                 0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                                                                                                   0.0s
 => ERROR [internal] load metadata for ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7-amd64                                                                                                                                                                                                                                                                0.5s
 => [internal] load metadata for gcr.io/projectsigstore/cosign:v1.13.1@sha256:fd5b09be23ef1027e1bdd490ce78dcc65d2b15902e1f4ba8e04f3b4019cc1057                                                                                                                                                                                                                    0.0s
------
 > [internal] load metadata for ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7-amd64:
------
failed to solve with frontend dockerfile.v0: failed to create LLB definition: ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7-amd64: not found
make: *** [base-amd64] Error 1

$  docker pull ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7-amd64
Error response from daemon: manifest unknown

$ docker pull ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7
v0.1.7: Pulling from goreleaser/goreleaser-cross-toolchains
Digest: sha256:565ac74e6da91abb8f8c5add08f4b0a8a068fd198886ac8e690e3b3f3491eca8
Status: Downloaded newer image for ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7
ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7

My guess is the daemon is looking for the "manifest" in the registry, but it's not tagged there.

And yes, I know the Makefile is not used in the GH actions. When I pushed PRs for other Go versions I tested it without issues, and with the current version I need to edit at build-base.sh to remove the -amd64, or run docker build manually:

$ docker build --platform=linux/amd64 -t ghcr.io/goreleaser/goreleaser-cross-base:1.20 \
> --build-arg GO_VERSION=1.20 \
> --build-arg TINI_VERSION=v0.19.0 \
> --build-arg COSIGN_VERSION=1.13.1 
> --build-arg COSIGN_SHA256=fd5b09be23ef1027e1bdd490ce78dcc65d2b15902e1f4ba8e04f3b4019cc1057 \
> --build-arg DEBIAN_FRONTEND=noninteractive \
> --build-arg TOOLCHAINS_VERSION=v0.1.7 \
> -f Dockerfile.base .
[+] Building 1.8s (14/14) FINISHED
 => [internal] load build definition from Dockerfile.base                                                                                                                                                                                                                                                                                                         0.0s
 => => transferring dockerfile: 1.93kB                                                                                                                                                                                                                                                                                                                            0.0s
 => [internal] load .dockerignore                                                                                                                                                                                                                                                                                                                                 0.0s
 => => transferring context: 2B                                                                                                                                                                                                                                                                                                                                   0.0s
 => [internal] load metadata for ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7                                                                                                                                                                                                                                                                            0.0s
 => [internal] load metadata for gcr.io/projectsigstore/cosign:v1.13.1@sha256:fd5b09be23ef1027e1bdd490ce78dcc65d2b15902e1f4ba8e04f3b4019cc1057                                                                                                                                                                                                                    0.3s
 => [internal] load build context                                                                                                                                                                                                                                                                                                                                 0.0s
 => => transferring context: 35B                                                                                                                                                                                                                                                                                                                                  0.0s
 => [cosign 1/1] FROM gcr.io/projectsigstore/cosign:v1.13.1@sha256:fd5b09be23ef1027e1bdd490ce78dcc65d2b15902e1f4ba8e04f3b4019cc1057                                                                                                                                                                                                                               0.0s
 => [stage-1 1/5] FROM ghcr.io/goreleaser/goreleaser-cross-toolchains:v0.1.7                                                                                                                                                                                                                                                                                      0.0s
 => FROM docker.io/docker/buildx-bin:latest                                                                                                                                                                                                                                                                                                                       1.4s
 => => resolve docker.io/docker/buildx-bin:latest                                                                                                                                                                                                                                                                                                                 1.4s
 => [auth] docker/buildx-bin:pull token for registry-1.docker.io                                                                                                                                                                                                                                                                                                  0.0s
 => CACHED [stage-1 2/5] COPY entrypoint.sh /                                                                                                                                                                                                                                                                                                                     0.0s
 => CACHED [stage-1 3/5] RUN     echo "Starting image build for Debian"  && sed -ri "s/(httpredir|deb).debian.org/${APT_MIRROR:-deb.debian.org}/g" /etc/apt/sources.list  && sed -ri "s/(security).debian.org/${APT_MIRROR:-security.debian.org}/g" /etc/apt/sources.list  && apt-get update  && apt-get install --no-install-recommends -y -q     software-prop  0.0s
 => CACHED [stage-1 4/5] COPY --from=cosign /ko-app/cosign /usr/local/bin/cosign                                                                                                                                                                                                                                                                                  0.0s
 => CACHED [stage-1 5/5] COPY --from=docker/buildx-bin:latest /buildx /usr/libexec/docker/cli-plugins/docker-buildx                                                                                                                                                                                                                                               0.0s
 => exporting to image                                                                                                                                                                                                                                                                                                                                            0.0s
 => => exporting layers                                                                                                                                                                                                                                                                                                                                           0.0s
 => => writing image sha256:2287e4f6610f78e017989f7310b6bbd39741959f4d3fb13441eaf329aa99ab9d                                                                                                                                                                                                                                                                      0.0s
 => => naming to ghcr.io/goreleaser/goreleaser-cross-base:1.20

$ docker images
REPOSITORY                                               TAG               IMAGE ID       CREATED        SIZE
ghcr.io/goreleaser/goreleaser-cross-base                 1.20              2287e4f6610f   2 hours ago    7.82GB
ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains   v0.1.7            911216523664   19 hours ago   7.16GB
ghcr.io/ghcr.io/goreleaser/goreleaser-cross-toolchains   v0.1.7-amd64      911216523664   19 hours ago   7.16GB
ghcr.io/goreleaser/goreleaser-cross-toolchains           v0.1.7            911216523664   19 hours ago   7.16GB
moby/buildkit                                            buildx-stable-1   fb2b14fffd09   12 days ago    168MB
registry                                                 2                 81c944c2288b   2 months ago   24.1MB
troian commented 1 year ago

there was extra ghcr.io in image name which resulted in ghcr.io/ghcr.io/ fixed in master

troian commented 1 year ago

go1.20 released. closing this

@maraino please open another issue if you have more questions about makefile build process