1
0
Fork 0
mirror of https://github.com/zhaofengli/attic.git synced 2024-12-14 11:57:30 +00:00

Build and push multi-arch images

Fixes #147.
This commit is contained in:
Zhaofeng Li 2024-09-11 09:59:49 -04:00
parent 444ea808d5
commit b45885e67b
3 changed files with 141 additions and 21 deletions

60
.ci/build-and-push-images.sh Executable file
View file

@ -0,0 +1,60 @@
#!/usr/bin/env bash
set -euo pipefail
if [[ "$#" -lt "2" ]]; then
>&2 echo "Usage: $0 <image name> <tag1> ..."
>&2 echo "Example: $0 ghcr.io/zhaofengli/attic main abcd123"
exit 1
fi
cleanup() {
if [[ -f "${manifest_spec}" ]]; then
rm "${manifest_spec}"
fi
}
trap cleanup EXIT
image_name="$1"
tags=("${@:2}")
manifest_spec="$(mktemp -t attic-manifest-spec.XXXXXXXXXX)"
declare -a digests
emit_header() {
echo "image: ${image_name}"
echo "tags:"
for tag in "${tags[@]}"; do
echo "- ${tag}"
done
echo "manifests:"
}
push_digest() {
source_image="docker-archive:$1"
digest="$(skopeo inspect "${source_image}" | jq -r .Digest)"
target_image="docker://${image_name}@${digest}"
>&2 echo "${source_image}${target_image}"
>&2 skopeo copy --insecure-policy "${source_image}" "${target_image}"
echo -n "- "
skopeo inspect "${source_image}" | \
jq '{platform: {architecture: .Architecture, os: .Os}, image: ($image_name + "@" + .Digest)}' \
--arg image_name "${image_name}"
}
>>"${manifest_spec}" emit_header
nix build .#attic-server-image .#attic-server-image-aarch64 -L --print-out-paths | \
while read -r output; do
>>"${manifest_spec}" push_digest "${output}"
done
>&2 echo "----------"
>&2 echo "Generated manifest-tool spec:"
>&2 echo "----------"
cat "${manifest_spec}"
>&2 echo "----------"
manifest-tool push from-spec "${manifest_spec}"

View file

@ -4,7 +4,7 @@ on:
push: push:
env: env:
REGISTRY: ghcr.io REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }} IMAGE_NAME: ghcr.io/${{ github.repository }}
jobs: jobs:
tests: tests:
strategy: strategy:
@ -17,9 +17,6 @@ jobs:
- "2.24" - "2.24"
- "default" - "default"
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
permissions:
contents: read
packages: write
steps: steps:
- uses: actions/checkout@v4.1.1 - uses: actions/checkout@v4.1.1
@ -38,6 +35,7 @@ jobs:
fi fi
- name: Configure Attic - name: Configure Attic
continue-on-error: true
run: | run: |
: "${ATTIC_SERVER:=https://staging.attic.rs/}" : "${ATTIC_SERVER:=https://staging.attic.rs/}"
: "${ATTIC_CACHE:=attic-ci}" : "${ATTIC_CACHE:=attic-ci}"
@ -75,30 +73,88 @@ jobs:
.#internalMatrix."$system".\"${{ matrix.nix }}\".cargoArtifacts \ .#internalMatrix."$system".\"${{ matrix.nix }}\".cargoArtifacts \
| xargs attic push "ci:$ATTIC_CACHE" | xargs attic push "ci:$ATTIC_CACHE"
fi fi
image:
runs-on: ubuntu-latest
if: github.event_name == 'push'
needs:
- tests
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4.1.1
- name: Install current Bash on macOS
if: runner.os == 'macOS'
run: |
command -v brew && brew install bash || true
- uses: DeterminateSystems/nix-installer-action@v9
continue-on-error: true # Self-hosted runners already have Nix installed
- name: Install Attic
run: |
if ! command -v attic &> /dev/null; then
./.github/install-attic-ci.sh
fi
- name: Configure Attic
continue-on-error: true
run: |
: "${ATTIC_SERVER:=https://staging.attic.rs/}"
: "${ATTIC_CACHE:=attic-ci}"
echo ATTIC_CACHE=$ATTIC_CACHE >>$GITHUB_ENV
export PATH=$HOME/.nix-profile/bin:$PATH # FIXME
attic login --set-default ci "$ATTIC_SERVER" "$ATTIC_TOKEN"
attic use "$ATTIC_CACHE"
env:
ATTIC_SERVER: ${{ secrets.ATTIC_SERVER }}
ATTIC_CACHE: ${{ secrets.ATTIC_CACHE }}
ATTIC_TOKEN: ${{ secrets.ATTIC_TOKEN }}
- name: Cache dev shell
run: |
.ci/cache-shell.sh
system=$(nix-instantiate --eval -E 'builtins.currentSystem')
echo system=$system >>$GITHUB_ENV
- name: Log in to the Container registry - name: Log in to the Container registry
uses: docker/login-action@v3.0.0 uses: docker/login-action@v3.0.0
if: runner.os == 'Linux' && github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
with: with:
registry: ${{ env.REGISTRY }} registry: ${{ env.REGISTRY }}
username: ${{ github.actor }} username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }} password: ${{ secrets.GITHUB_TOKEN }}
- name: Push build container image - name: Build and push container images
if: runner.os == 'Linux' && github.event_name == 'push' && github.ref == format('refs/heads/{0}', github.event.repository.default_branch)
continue-on-error: true continue-on-error: true
run: | run: |
IMAGE_ID=ghcr.io/${IMAGE_NAME} declare -a tags
TARBALL=$(nix build --json .#attic-server-image | jq -r '.[].outputs.out') tags+=("${{ github.sha }}")
BRANCH=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
TAG="${{ github.sha }}" branch=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
[[ "${{ github.ref }}" == "refs/tags/"* ]] && TAG=$(echo $BRANCH | sed -e 's/^v//') if [[ "${{ github.ref }}" == "refs/tags/"* ]]; then
docker load < ${TARBALL} tags+=("$(echo $branch | sed -e 's/^v//')")
echo IMAGE_ID=$IMAGE_ID else
echo TAG=$TAG tags+=("${branch}")
docker tag attic-server:main "${IMAGE_ID}:${TAG}" fi
docker push ${IMAGE_ID}:${TAG}
if [ "$BRANCH" == "main" ]; then if [ "$branch" == "${{ github.event.repository.default_branch }}" ]; then
TAG="latest" tags+=("latest")
docker tag attic-server:main "${IMAGE_ID}:${TAG}" fi
docker push ${IMAGE_ID}:${TAG}
>&2 echo "Image: ${IMAGE_NAME}"
>&2 echo "Tags: ${tags[@]}"
.ci/run just ci-build-and-push-images "${IMAGE_NAME}" "${tags[@]}"
# TODO: Just take a diff of the list of store paths, also abstract all of this out
- name: Push build artifacts
run: |
export PATH=$HOME/.nix-profile/bin:$PATH # FIXME
if [ -n "$ATTIC_TOKEN" ]; then
nix build --no-link --print-out-paths -L \
.#attic-server-image \
.#attic-server-image-aarch64 \
| xargs attic push "ci:$ATTIC_CACHE"
fi fi

View file

@ -45,3 +45,7 @@ ci-unit-tests matrix:
# (CI) Run rustfmt check # (CI) Run rustfmt check
ci-rustfmt: ci-rustfmt:
cargo fmt --check cargo fmt --check
# (CI) Build and push images
ci-build-and-push-images *args:
.ci/build-and-push-images.sh {{ args }}