From e97a236529d2766760997b46b8ccc9b9018585c8 Mon Sep 17 00:00:00 2001 From: zimbatm Date: Wed, 13 Feb 2019 18:30:15 +0100 Subject: [PATCH] update DockerHub info create a script that automatically updates the DockerHub project pages --- .gitlab-ci.sh | 16 ++++++++++++++-- .travis.sh | 15 +++++++++++++-- README.md | 12 ++++++------ docker-login | 14 ++++++++++---- push-all | 13 +++++++++---- update-dockerhub | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 95 insertions(+), 18 deletions(-) create mode 100755 update-dockerhub diff --git a/.gitlab-ci.sh b/.gitlab-ci.sh index cd3101d..ce09541 100755 --- a/.gitlab-ci.sh +++ b/.gitlab-ci.sh @@ -6,9 +6,21 @@ set -euo pipefail ./build +# default to the Gitlab registry +: "${REGISTRY:=$CI_REGISTRY}" +: "${REGISTRY_USER:=$CI_REGISTRY_USER}" +: "${REGISTRY_PASSWORD:=$CI_REGISTRY_PASSWORD}" +: "${IMAGE_PREFIX:=$CI_PROJECT_PATH}" + +# IMAGE_TAG is provided by .gitlab-ci.yml + + if [[ "$CI_COMMIT_REF_NAME" = master ]]; then - ./docker-login "$CI_REGISTRY" "$CI_REGISTRY_USER" "$CI_REGISTRY_PASSWORD" - ./push-all "$CI_REGISTRY_IMAGE" "$IMAGE_TAG" + ./docker-login "$REGISTRY_USER" "$REGISTRY_PASSWORD" "$REGISTRY" + ./push-all "$REGISTRY" "$IMAGE_PREFIX" "$IMAGE_TAG" + if [[ $REGISTRY = *docker.io ]]; then + ./update-docker-hub "$REGISTRY_USER" "$REGISTRY_PASSWORD" "$IMAGE_PREFIX" + fi else echo "=== not pushing on non-master ===" fi diff --git a/.travis.sh b/.travis.sh index 1c104ca..dca29de 100755 --- a/.travis.sh +++ b/.travis.sh @@ -6,9 +6,20 @@ set -euo pipefail ./build +# default to Docker Hub +: "${REGISTRY:=docker.io}" +: "${IMAGE_PREFIX:=nixpkgs}" + +# IMAGE_TAG is provided by .travis.yml + +# the user has to set REGISTRY_USER and REGISTRY_PASSWORD + if [[ "$TRAVIS_BRANCH" = master && -z "$TRAVIS_PULL_REQUEST_BRANCH" ]]; then - ./docker-login "$CI_REGISTRY" "$CI_REGISTRY_USER" "$CI_REGISTRY_PASSWORD" - ./push-all "$CI_REGISTRY_PREFIX" "$IMAGE_TAG" + ./docker-login "$REGISTRY_USER" "$REGISTRY_PASSWORD" "$REGISTRY" + ./push-all "$REGISTRY" "$IMAGE_PREFIX" "$IMAGE_TAG" + if [[ $REGISTRY = *docker.io ]]; then + ./update-dockerhub "$REGISTRY_USER" "$REGISTRY_PASSWORD" "$IMAGE_PREFIX" + fi else echo "=== not pushing on non-master ===" fi diff --git a/README.md b/README.md index afdbbe7..64ff479 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # docker-nixpkgs: docker images from nixpkgs This project is a collection of docker images automatically produced with Nix -and the latest nixpkgs package set. It even refreshes every morning a 4:00 UTC -thanks to the [Gitlab CI schedules][gitlab-schedules]. +and the latest nixpkgs package set. All the images are refreshed daily with +the latest versions of nixpkgs. It's also a good demonstration on how to build and publish Docker images with Nix. @@ -24,10 +24,8 @@ Here is an example of using one of the docker images. Usage will change from image to image. ``` -# the user must have an account at gitlab -$ docker login registry.gitlab.com # run the curl image which has curl as an entry-point -$ docker run -ti --rm registry.gitlab.com/zimbatm/docker-nixpkgs/curl http://ifconfig.co +$ docker run -ti --rm nixpkgs/curl http://ifconfig.co 180.52.248.114 ``` @@ -49,6 +47,8 @@ $ docker run -ti --rm registry.gitlab.com/zimbatm/docker-nixpkgs/curl http://ifc | nixos-unstable | the :latest version | | nixos-18.09 | automatic security updates | +## License -[gitlab-schedules]: https://gitlab.com/zimbatm/docker-nixpkgs/pipeline_schedules +Copyright (c) 2019 zimbatm and contributors. +Licensed under the MIT. diff --git a/docker-login b/docker-login index 50deeb3..f190a85 100755 --- a/docker-login +++ b/docker-login @@ -2,12 +2,18 @@ # # A simplified docker login approach that doesn't depends on the docker binary # -# Usage: ./docker-login +# Usage: ./docker-login [registry] set -euo pipefail -registry=$1 -username=$2 -password=$3 +username=$1 +password=$2 +registry=${3:-docker.io} + +# Encode some funky docker heuristic +if [[ $registry = *docker.io ]]; then + # use the v2 registry so that skopeo can do noop layer copies + registry=https://index.docker.io/v2/ +fi mkdir ~/.docker diff --git a/push-all b/push-all index 20ed89d..2782e23 100755 --- a/push-all +++ b/push-all @@ -1,15 +1,20 @@ #!/usr/bin/env bash # -# Usage: ./push-all +# Usage: ./push-all set -euo pipefail -registry_prefix=${1:-nixpkgs} -image_tag=${2:-latest} +registry=${1:-docker.io} +image_prefix=${2:-nixpkgs} +image_tag=${3:-latest} releases_json=$(nix-instantiate ./release.nix --strict --eval --json) +echo "=== Pushing images to $registry" + for attr in $(echo "$releases_json" | jq -r "keys[]") ; do file=$(echo "$releases_json" | jq -r ".\"$attr\"") echo "--- $attr -> $file" - skopeo copy "docker-archive://$file" "docker://$registry_prefix/$attr:$image_tag" + skopeo copy "docker-archive://$file" "docker://$registry/$image_prefix/$attr:$image_tag" done + +echo OK diff --git a/update-dockerhub b/update-dockerhub new file mode 100755 index 0000000..f5acfcd --- /dev/null +++ b/update-dockerhub @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +# +# Update docker hub image descriptions. The API is not documented and might +# break in the future. +# +# Usage: ./update-dockerhub [org] +set -euo pipefail + +username=$1 +password=$2 +org=${3:-nixpkgs} +user=$username:$password + +releases_json=$(nix-instantiate ./release.nix --strict --eval --json) + +to_json() { + local desc=$1 full_desc=$2 + jq -n \ + --arg desc "$desc" \ + --arg full_desc "$full_desc" \ + '.description=$desc | .full_description=$full_desc' +} + +echo "=== Updating Docker Hub project descriptions" + +for attr in $(echo "$releases_json" | jq -r "keys[]") ; do + echo "--- $attr" + desc="$attr is automatically built from nix-community/docker-nixpkgs" + + if [[ -f "$attr/README.md" ]]; then + full_desc=$(< "$attr/README.md") + else + full_desc=$(< "README.md") + fi + + data=$(to_json "$desc" "$full_desc") + echo "data: $data" + url=https://cloud.docker.com/v2/repositories/$org/$attr/ + + curl -XPATCH -H "Content-Type: application/json" --user "$user" --data "$data" "$url" +done + +echo OK