From ee449f4753d6ee9c04b0ebe936a39550e0fc3561 Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Sun, 18 Feb 2024 22:18:14 +0100 Subject: [PATCH] feat: build and publish docker images --- .github/scripts/get-docker-tags.sh | 55 +++++++++++++++++++++++++ .github/workflows/docker.yml | 65 ++++++++++++++++++++++++++++++ README.md | 21 ++++++++++ 3 files changed, 141 insertions(+) create mode 100755 .github/scripts/get-docker-tags.sh create mode 100644 .github/workflows/docker.yml diff --git a/.github/scripts/get-docker-tags.sh b/.github/scripts/get-docker-tags.sh new file mode 100755 index 0000000..cf56920 --- /dev/null +++ b/.github/scripts/get-docker-tags.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# get-docker-tags.sh produces Docker tags for the current build +# +# Usage: +# ./get-docker-tags.sh [git tag name] +# +# Example: +# +# # get tag for the main branch +# ./get-docker-tags.sh $(date -u +%F) testingsha main +# +# # get tag for a release tag +# ./get-docker-tags.sh $(date -u +%F) testingsha release v0.5.0 +# +# # Serving suggestion in CI +# ./get-docker-tags.sh $(date -u +%F) "$CI_SHA1" "$CI_BRANCH" "$CI_TAG" +# +set -euo pipefail + +if [[ $# -lt 1 ]] ; then + echo 'At least 1 arg required.' + echo 'Usage:' + echo './get-docker-tags.sh [git commit sha1] [git branch name] [git tag name]' + exit 1 +fi + +BUILD_NUM=$1 +GIT_SHA1=${2:-$(git rev-parse HEAD)} +GIT_SHA1_SHORT=$(echo "$GIT_SHA1" | cut -c 1-7) +GIT_BRANCH=${3:-$(git symbolic-ref -q --short HEAD || echo "unknown")} +GIT_TAG=${4:-$(git describe --tags --exact-match 2> /dev/null || echo "")} + +IMAGE_NAME=${IMAGE_NAME:-ipfs/someguy} + +echoImageName () { + local IMAGE_TAG=$1 + echo "$IMAGE_NAME:$IMAGE_TAG" +} + +if [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+-rc ]]; then + echoImageName "$GIT_TAG" + +elif [[ $GIT_TAG =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then + echoImageName "$GIT_TAG" + echoImageName "latest" + +elif [ "$GIT_BRANCH" = "main" ] || [ "$GIT_BRANCH" = "staging" ]; then + echoImageName "${GIT_BRANCH}-${BUILD_NUM}-${GIT_SHA1_SHORT}" + echoImageName "${GIT_BRANCH}-latest" + +else + echo "Nothing to do. No docker tag defined for branch: $GIT_BRANCH, tag: $GIT_TAG" + +fi diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..415fa30 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,65 @@ +name: Create and publish a Docker image + +on: + push: + branches: ['main', 'staging'] + tags: ['v*'] + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push-image: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Checkout repository + uses: actions/checkout@v4 + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Cache Docker layers + uses: actions/cache@v4 + with: + path: /tmp/.buildx-cache + key: ${{ runner.os }}-buildx-${{ github.sha }} + restore-keys: | + ${{ runner.os }}-buildx- + - name: Log in to the Container registry + uses: docker/login-action@v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Get tags + id: tags + env: + IMAGE_NAME: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + run: | + echo "value<> $GITHUB_OUTPUT + ./.github/scripts/get-docker-tags.sh "$(date -u +%F)" >> $GITHUB_OUTPUT + echo "EOF" >> $GITHUB_OUTPUT + shell: bash + - name: Build Docker image and publish to Docker Hub + uses: docker/build-push-action@v5 + with: + platforms: linux/amd64,linux/arm/v7,linux/arm64/v8 + context: . + push: true + file: ./Dockerfile + tags: "${{ steps.tags.outputs.value }}" + cache-from: type=local,src=/tmp/.buildx-cache + cache-to: type=local,dest=/tmp/.buildx-cache-new + + # https://github.com/docker/build-push-action/issues/252 + # https://github.com/moby/buildkit/issues/1896 + - name: Move cache to limit growth + run: | + rm -rf /tmp/.buildx-cache + mv /tmp/.buildx-cache-new /tmp/.buildx-cache diff --git a/README.md b/README.md index b1d685f..6bc2443 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,27 @@ A [Delegated Routing V1](https://specs.ipfs.tech/routing/http-routing-v1/) serve go install github.com/ipfs/someguy@latest ``` +### Docker + +Automated Docker container releases are available from the [Github container registry](https://github.com/ipfs/someguy/pkgs/container/someguy): + +- 🟢 Releases + - `latest` always points at the latest stable release + - `vN.N.N` point at a specific [release tag](https://github.com/ipfs/someguy/releases) +- 🟠 Unreleased developer builds + - `main-latest` always points at the `HEAD` of the `main` branch + - `main-YYYY-DD-MM-GITSHA` points at a specific commit from the `main` branch +- ⚠️ Experimental, unstable builds + - `staging-latest` always points at the `HEAD` of the `staging` branch + - `staging-YYYY-DD-MM-GITSHA` points at a specific commit from the `staging` branch + - This tag is used by developers for internal testing, not intended for end users + +When using Docker, make sure to pass necessary config via `-e`: +```console +$ docker pull ghcr.io/ipfs/someguy:main-latest +$ docker run --rm -it --net=host -e ghcr.io/ipfs/someguy:main-latest +``` + ## Build ```bash