diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 08b45268..21f7b6ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 @@ -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 @@ -46,3 +50,6 @@ jobs: run: | ./devel/tag-latest $TAG ./devel/push latest $TAG + + - if: always() + run: ./devel/stop-localhost-registry diff --git a/README.md b/README.md index ed32ff9e..437b6cf3 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/devel/build b/devel/build index a485760a..b0e498eb 100755 --- a/devel/build +++ b/devel/build @@ -1,10 +1,11 @@ #!/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 and specify a different +# registry with -r . # # 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 @@ -12,8 +13,18 @@ # 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 ] [-t ]" 1>&2; exit 1;; + esac +done export GIT_REVISION=$(git describe --tags --abbrev=40 --always --dirty || true) @@ -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 @@ -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 \ @@ -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 \ . diff --git a/devel/start-localhost-registry b/devel/start-localhost-registry new file mode 100755 index 00000000..9a92e17b --- /dev/null +++ b/devel/start-localhost-registry @@ -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 diff --git a/devel/stop-localhost-registry b/devel/stop-localhost-registry new file mode 100755 index 00000000..30919fb1 --- /dev/null +++ b/devel/stop-localhost-registry @@ -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