Skip to content

Commit

Permalink
build: Push directly to a local registry
Browse files Browse the repository at this point in the history
This is a stepping stone to push directly to another registry in the CI
workflow, while maintaining ease of local development. Reasons to push
directly:

1. Allows breaking up the CI workflow by persisting the built images
2. Allows multi-platform images to be published

This breaks the CI workflow since the unchanged push mechanisms rely on
tags being available locally. That will be fixed in a following commit.
  • Loading branch information
victorlin committed Nov 1, 2022
1 parent 09c73ba commit 92fdf62
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 17 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ on: [push, pull_request, workflow_dispatch]
jobs:
run:
runs-on: ubuntu-latest
env:
REGISTRY_PORT: 5000
steps:

- uses: actions/checkout@v2
Expand All @@ -20,7 +22,9 @@ jobs:
# and hyphens.
run: echo "TAG=branch-${GITHUB_REF_NAME//[^A-Za-z0-9._-]/-}" | tee -a $GITHUB_ENV

- run: ./devel/build $TAG
- run: ./devel/start-localhost-registry $REGISTRY_PORT

- run: ./devel/build -r localhost:$REGISTRY_PORT -t $TAG

- if: github.event_name != 'pull_request'
uses: docker/login-action@v1
Expand All @@ -46,3 +50,6 @@ jobs:
run: |
./devel/tag-latest $TAG
./devel/push latest $TAG
- if: always()
run: ./devel/stop-localhost-registry
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,27 @@ This is most helpful when you want the image to contain the latest version of a

### Building

To build this image locally, run:
To build this image locally,

```
./devel/build
```
1. Start a local Docker registry.

By default, this tags the image with `latest`. If you want to use a custom tag
name, you can specify it as the first argument to the script.
```
./devel/start-localhost-registry
```

It will be served at `localhost:5000`. Optionally, specify another port as
an argument. Running a local Docker registry allows us to mimic direct push
to a registry done in the GitHub Actions CI workflow.

2. Build the image.

```
./devel/build
```

By default, this tags the image with `latest` and pushes to
`localhost:5000`. See instructions at the top of the script for additional
options.

On each subsequent change during your development iterations, you can run just
the `./devel/build` command again.
Expand Down
31 changes: 21 additions & 10 deletions devel/build
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
#!/bin/bash
#
# Builds the nextstrain/base and nextstrain/base-builder images with useful
# caching.
# caching and pushes to a registry.
#
# By default this tags images using "latest", but you can provide a custom tag
# name.
# By default this tags images using "latest" and pushes to localhost:5000, but
# you can provide a custom tag with -t <tag> and specify a different
# registry with -r <registry>.
#
# Set CACHE_DATE in your environment to force layers after our custom cache
# point to be re-built. See the ARG CACHE_DATE line in the Dockerfile for more
# information.
#
set -euo pipefail

# If the tag is not provided, default to "latest".
tag="${1:-latest}"
# Set default values.
registry=localhost:5000
tag=latest

# Read command-line arguments.
while getopts "r:t:" opt; do
case "$opt" in
r) registry="$OPTARG";;
t) tag="$OPTARG";;
*) echo "Usage: $0 [-r <registry>] [-t <tag>]" 1>&2; exit 1;;
esac
done

export GIT_REVISION=$(git describe --tags --abbrev=40 --always --dirty || true)

Expand All @@ -36,7 +47,7 @@ if ! docker buildx inspect "$builder" &>/dev/null; then
# However, if this is changed and it was previously run on your machine,
# you may need to remove the builder manually before running the script:
# docker builder rm "nextstrain-builder"
docker buildx create --name "$builder" --driver docker-container
docker buildx create --name "$builder" --driver docker-container --driver-opt network=host
fi

BUILDER_IMAGE=nextstrain/base-builder
Expand All @@ -51,8 +62,8 @@ docker buildx build \
--cache-from "$BUILDER_IMAGE:latest" \
--cache-from "$BUILDER_IMAGE:$tag" \
--cache-to type=inline \
--tag "$BUILDER_IMAGE:$tag" \
--load \
--tag "$registry/$BUILDER_IMAGE:$tag" \
--push \
.

docker buildx build \
Expand All @@ -66,6 +77,6 @@ docker buildx build \
--cache-from "$FINAL_IMAGE:latest" \
--cache-from "$FINAL_IMAGE:$tag" \
--cache-to type=inline \
--tag "$FINAL_IMAGE:$tag" \
--load \
--tag "$registry/$FINAL_IMAGE:$tag" \
--push \
.
16 changes: 16 additions & 0 deletions devel/start-localhost-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
#
# Starts a Docker registry on localhost using a Docker container.
#
set -euo pipefail

# Port to run the registry on. If not provided, default to 5000.
PORT="${1:-5000}"

# Name of the docker container.
NAME=nextstrain-local-registry

# Docker image that provides the registry service.
IMAGE=registry:2

docker run -d -p "$PORT":"$PORT" --restart always --name "$NAME" "$IMAGE" > /dev/null
10 changes: 10 additions & 0 deletions devel/stop-localhost-registry
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash
#
# Stops a local Docker registry created by start-localhost-registry.
#
set -euo pipefail

NAME=nextstrain-local-registry

docker stop "$NAME" > /dev/null
docker rm "$NAME" > /dev/null

0 comments on commit 92fdf62

Please sign in to comment.