diff --git a/.github/workflows/push.yml b/.github/workflows/push.yml index 22cf42a..360253d 100644 --- a/.github/workflows/push.yml +++ b/.github/workflows/push.yml @@ -19,8 +19,9 @@ jobs: go-version: 1.21 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Install docker-compose - run: sudo apt-get install -y docker-compose + with: + platforms: linux/amd64,linux/arm64 + install: true - name: Build run: go run build.go env: diff --git a/.github/workflows/tag.yml b/.github/workflows/tag.yml index 783affa..d7f4990 100644 --- a/.github/workflows/tag.yml +++ b/.github/workflows/tag.yml @@ -18,8 +18,9 @@ jobs: go-version: 1.21 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - - name: Install docker-compose - run: sudo apt-get install -y docker-compose + with: + platforms: linux/amd64,linux/arm64 + install: true - name: Build & Push run: go run build.go --push env: diff --git a/README.md b/README.md index 323012e..601ec00 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This repository contains the scripts that build the ContainerSSH container image This repository contains a build script in Go called `build.go`. It can be invoked by running `go run build.go`. This script will read [build.yaml](build.yaml) and build the container image based on that revision. It uses the GitHub API to download release artifacts, so it may need the `GITHUB_TOKEN` environment variable set. The optional `--push` flag can be set to push the images to the corresponding registries. -Under the hood the build uses [`docker-compose`](https://docs.docker.com/compose/) to build, test, and push the images. The build steps can be performed manually. +Under the hood the build uses [`docker compose`](https://docs.docker.com/compose/) to build, test, and push the images. The build steps can be performed manually. Before you begin you must set several environment variables. These are the following: @@ -26,15 +26,15 @@ Before you begin you must set several environment variables. These are the follo For example, on Linux/MacOS: ```bash -CONTAINERSSH_VERSION="0.3.1" -CONTAINERSSH_TAG="0.3.1" +CONTAINERSSH_VERSION="v0.5.2" +CONTAINERSSH_TAG="v0.5.2" ``` On Windows/PowerShell: ```ps1 -$env:CONTAINERSSH_VERSION="0.3.1" -$env:CONTAINERSSH_TAG="0.3.1" +$env:CONTAINERSSH_VERSION="v0.5.2" +$env:CONTAINERSSH_TAG="v0.5.2" ``` ### Build @@ -44,7 +44,7 @@ The build step requires build arguments to function. At the very least it should Optionally, you can also specify a `GITHUB_TOKEN` to work around GitHub rate limits and `SOURCE_REPO` to point the build to a different source URL. ```bash -docker-compose build +docker compose build ``` ### Test @@ -52,21 +52,21 @@ docker-compose build Testing is done via a container called `sut`. This container will wait for ContainerSSH to come up and then run a simple SSH connection to it to test that it works correctly. This is not a comprehensive test, but checks if the image build was successful. ``` -docker-compose up --abort-on-container-exit --exit-code-from=sut +docker compose up --abort-on-container-exit --exit-code-from=sut ``` ### Clean up after test ``` -docker-compose down +docker compose down ``` ### Push -Finally, pushing container images can also be done from `docker-compose`. After a `docker login` command this can be simply done using the following command: +Finally, pushing container images can also be done from `docker compose`. After a `docker login` command this can be simply done using the following command: ``` -docker-compose push +docker compose push ``` ## Versioning diff --git a/build.go b/build.go index 923ecf7..374e462 100644 --- a/build.go +++ b/build.go @@ -14,6 +14,7 @@ import ( type registry struct { UserVariable string `yaml:"user_variable"` PasswordVariable string `yaml:"password_variable"` + OrganisationVariable string `yaml:"organisation_variable,omitempty"` } func runExternalProgram( @@ -100,12 +101,25 @@ func buildVersion( fmt.Sprintf("CONTAINERSSH_VERSION=%s", version), fmt.Sprintf("CONTAINERSSH_TAG=%s", tag), fmt.Sprintf("GITHUB_TOKEN=%s", githubToken), - fmt.Sprintf("REGISTRY=%s/", registryName), } + registryPrefix := fmt.Sprintf("%s/containerssh", registryName) + if registry.OrganisationVariable != "" { + organisation := os.Getenv(registry.OrganisationVariable) + if organisation == "" { + return fmt.Errorf( + "cannot push: no organisation set in the %s environment variable", + registry.OrganisationVariable, + ) + } + registryPrefix = fmt.Sprintf("%s/%s/containerssh", registryName, organisation) + } + env = append(env, fmt.Sprintf("REGISTRY=%s/", registryPrefix)) + if err := runExternalProgram( - "docker-compose", + "docker", []string{ + "compose", "build", }, env, @@ -116,17 +130,18 @@ func buildVersion( err := fmt.Errorf( "build failed for version %s registry %s tag %s (%w)", version, - registryName, + registryPrefix, tag, err, ) - writeOutput(version, registryName, tag, stdout, err) + writeOutput(version, registryPrefix, tag, stdout, err) return err } if err := runExternalProgram( - "docker-compose", + "docker", []string{ + "compose", "up", "--abort-on-container-exit", "--exit-code-from=sut", @@ -139,17 +154,18 @@ func buildVersion( err := fmt.Errorf( "tests failed for version %s registry %s tag %s (%w)", version, - registryName, + registryPrefix, tag, err, ) - writeOutput(version, registryName, tag, stdout, err) + writeOutput(version, registryPrefix, tag, stdout, err) return err } if err := runExternalProgram( - "docker-compose", + "docker", []string{ + "compose", "down", }, env, @@ -159,11 +175,11 @@ func buildVersion( err := fmt.Errorf( "cleanup failed for version %s registry %s tag %s (%w)", version, - registryName, + registryPrefix, tag, err, ) - writeOutput(version, registryName, tag, stdout, err) + writeOutput(version, registryPrefix, tag, stdout, err) return err } @@ -200,16 +216,23 @@ func buildVersion( "push failed for version %s tag %s registry %s (%w)", version, tag, - registryName, + registryPrefix, err, ) - writeOutput(version, registryName, tag, stdout, err) + writeOutput(version, registryPrefix, tag, stdout, err) return err } if err := runExternalProgram( - "docker-compose", + "docker", []string{ - "push", + "buildx", + "build", + "--push", + "--platform", "linux/amd64,linux/arm64", + "--build-arg", fmt.Sprintf("CONTAINERSSH_VERSION=%s", version), + "--build-arg", fmt.Sprintf("CONTAINERSSH_TAG=%s", tag), + "-t", fmt.Sprintf("%s/containerssh:%s", registryPrefix, tag), + "containerssh", }, env, nil, @@ -220,14 +243,42 @@ func buildVersion( "push failed for version %s tag %s registry %s (%w)", version, tag, - registryName, + registryPrefix, + err, + ) + writeOutput(version, registryPrefix, tag, stdout, err) + return err + } + if err := runExternalProgram( + "docker", + []string{ + "buildx", + "build", + "--push", + "--platform", "linux/amd64,linux/arm64", + "--build-arg", fmt.Sprintf("CONTAINERSSH_VERSION=%s", version), + "--build-arg", fmt.Sprintf("CONTAINERSSH_TAG=%s", tag), + "-t", fmt.Sprintf("%s/containerssh-test-authconfig:%s", registryPrefix, tag), + "containerssh-test-authconfig", + }, + env, + nil, + stdout, + stdout, + ); err != nil { + err := fmt.Errorf( + "push failed for version %s tag %s registry %s (%w)", + version, + tag, + registryPrefix, err, ) - writeOutput(version, registryName, tag, stdout, err) + writeOutput(version, registryPrefix, tag, stdout, err) return err } + } - writeOutput(version, registryName, tag, stdout, nil) + writeOutput(version, registryPrefix, tag, stdout, nil) } } diff --git a/build.yaml b/build.yaml index 585cf99..91c6ebb 100644 --- a/build.yaml +++ b/build.yaml @@ -8,16 +8,6 @@ versions: - v0.5.1 v0.5.0: - v0.5.0 - v0.5.0-alpha.1: - - 0.5.0-alpha.1 - v0.4.1: - - 0.4.1 - - 0.4 - v0.4.0: - - 0.4.0 - 0.3.1: - - 0.3.1 - - 0.3 registries: docker.io: user_variable: DOCKER_USERNAME diff --git a/containerssh-test-authconfig/Dockerfile b/containerssh-test-authconfig/Dockerfile index f8fb6e9..541c55c 100644 --- a/containerssh-test-authconfig/Dockerfile +++ b/containerssh-test-authconfig/Dockerfile @@ -6,6 +6,8 @@ FROM alpine AS download ARG CONTAINERSSH_VERSION ARG GITHUB_TOKEN ARG SOURCE_REPO +ARG TARGETOS +ARG TARGETARCH RUN if [ -z "${CONTAINERSSH_VERSION}" ]; then echo "Error: No CONTAINERSSH_VERSION specified." >&2; exit 1; fi RUN if [ -z "${GITHUB_TOKEN}" ]; then echo "Warning: No GITHUB_TOKEN specified, build may fail." >&2; fi RUN apk add --no-cache curl @@ -17,9 +19,9 @@ RUN mkdir -p /containerssh && \ USER 1022:1022 RUN cd /containerssh && \ if [ "${CONTAINERSSH_VERSION}" = "0.3.0" -o "${CONTAINERSSH_VERSION}" = "0.3.1" ]; then \ - URL=${SOURCE_REPO}/releases/download/${CONTAINERSSH_VERSION}/containerssh-authconfig_${CONTAINERSSH_VERSION}_linux_amd64.tar.gz; \ + URL=${SOURCE_REPO}/releases/download/${CONTAINERSSH_VERSION}/containerssh-authconfig_${CONTAINERSSH_VERSION}_${TARGETOS}_${TARGETARCH}.tar.gz; \ else \ - URL=${SOURCE_REPO}/releases/download/${CONTAINERSSH_VERSION}/containerssh_${CONTAINERSSH_VERSION/v/}_linux_amd64.tar.gz; \ + URL=${SOURCE_REPO}/releases/download/${CONTAINERSSH_VERSION}/containerssh_${CONTAINERSSH_VERSION/v/}_${TARGETOS}_${TARGETARCH}.tar.gz; \ fi && \ if [ -n "${CONTAINERSSH_VERSION}" ]; then \ curl -L -o containerssh-authconfig.tar.gz --header 'authorization: Bearer ${GITHUB_TOKEN}' ${URL}; \ diff --git a/containerssh/Dockerfile b/containerssh/Dockerfile index c891557..44475a4 100644 --- a/containerssh/Dockerfile +++ b/containerssh/Dockerfile @@ -6,6 +6,8 @@ FROM alpine AS download ARG CONTAINERSSH_VERSION ARG GITHUB_TOKEN ARG SOURCE_REPO +ARG TARGETOS +ARG TARGETARCH RUN if [ -z "${CONTAINERSSH_VERSION}" ]; then echo "Error: No CONTAINERSSH_VERSION specified." >&2; exit 1; fi RUN if [ -z "${GITHUB_TOKEN}" ]; then echo "Warning: No GITHUB_TOKEN specified, build may fail." >&2; fi RUN apk add --no-cache curl @@ -16,7 +18,7 @@ RUN mkdir -p /containerssh && \ # Drop privileges for download USER 1022:1022 RUN cd /containerssh && \ - URL=${SOURCE_REPO}/releases/download/${CONTAINERSSH_VERSION}/containerssh_${CONTAINERSSH_VERSION/v/}_linux_amd64.tar.gz && \ + URL=${SOURCE_REPO}/releases/download/${CONTAINERSSH_VERSION}/containerssh_${CONTAINERSSH_VERSION/v/}_${TARGETOS}_${TARGETARCH}.tar.gz && \ if [ -n "${GITHUB_TOKEN}" ]; then \ curl -L -o containerssh.tar.gz --header 'authorization: Bearer ${GITHUB_TOKEN}' ${URL}; \ else \ diff --git a/docker-compose.yaml b/docker-compose.yaml index 6c4e2f7..a542199 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,10 +1,12 @@ --- -version: '3.9' services: containerssh: image: ${REGISTRY:-}containerssh/containerssh:${CONTAINERSSH_TAG:?CONTAINERSSH_TAG variable must be set} build: context: containerssh + platforms: + - "linux/amd64" + - "linux/arm64" args: CONTAINERSSH_VERSION: ${CONTAINERSSH_VERSION:?CONTAINERSSH_VERSION variable must be set.} GITHUB_TOKEN: ${GITHUB_TOKEN:-} @@ -29,6 +31,9 @@ services: image: ${REGISTRY:-}containerssh/containerssh-test-authconfig:${CONTAINERSSH_TAG:?CONTAINERSSH_TAG variable must be set} build: context: containerssh-test-authconfig + platforms: + - "linux/amd64" + - "linux/arm64" args: CONTAINERSSH_VERSION: ${CONTAINERSSH_VERSION:?CONTAINERSSH_VERSION variable must be set.} GITHUB_TOKEN: ${GITHUB_TOKEN:-}