Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand All @@ -44,29 +44,29 @@ 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

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
Expand Down
85 changes: 68 additions & 17 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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",
Expand All @@ -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,
Expand All @@ -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
}

Expand Down Expand Up @@ -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,
Expand All @@ -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)
}
}

Expand Down
10 changes: 0 additions & 10 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions containerssh-test-authconfig/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}; \
Expand Down
4 changes: 3 additions & 1 deletion containerssh/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 \
Expand Down
7 changes: 6 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:-}
Expand All @@ -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:-}
Expand Down