|
| 1 | +#!/bin/bash |
| 2 | +set -euxo pipefail |
| 3 | + |
| 4 | +# Install Go by extracting it from the msft-go container image. |
| 5 | +# The golang image reference is read directly from the source Dockerfile for the |
| 6 | +# current image (identified by $name), keeping the pipeline in sync with the build. |
| 7 | +# |
| 8 | +# Priority: |
| 9 | +# 1. MSFT_GO_IMAGE env var (explicit override) |
| 10 | +# 2. Parsed from the source Dockerfile for $name |
| 11 | +# 3. Hardcoded fallback digest below |
| 12 | +# |
| 13 | +# To update the fallback, run: |
| 14 | +# skopeo inspect docker://mcr.microsoft.com/oss/go/microsoft/golang:1.24-cbl-mariner2.0 --format "{{.Name}}@{{.Digest}}" |
| 15 | +DEFAULT_IMAGE="mcr.microsoft.com/oss/go/microsoft/golang@sha256:b05a9bbf50a8ccfdd0ebe9f673ef29dca7c1d5e209434b35a560a4e8ae5f72b2" |
| 16 | + |
| 17 | +# Resolves the golang image from the source Dockerfile for the given $name. |
| 18 | +# Echoes the image reference, or empty string if it cannot be determined. |
| 19 | +resolve_go_image() { |
| 20 | + if [[ "${name:-}" == "npm" ]]; then |
| 21 | + # npm uses OS-specific Dockerfiles with a tag-based reference. |
| 22 | + # The image may be field 2 (no --platform) or field 3 (with --platform), |
| 23 | + # so extract the mcr.* token directly. |
| 24 | + # e.g. FROM mcr.../golang:1.25.5 AS builder |
| 25 | + # e.g. FROM --platform=linux/amd64 mcr.../golang:1.25.5 AS builder |
| 26 | + local buildfile="${REPO_ROOT}/npm/${OS:-linux}.Dockerfile" |
| 27 | + grep -m1 '^FROM.*golang' "${buildfile}" 2>/dev/null | grep -o 'mcr[^ ]*' || true |
| 28 | + |
| 29 | + else |
| 30 | + # All other images use a digest-pinned reference and always have --platform, |
| 31 | + # making the image consistently field 3: FROM --platform=X IMAGE AS alias |
| 32 | + local buildfile |
| 33 | + if [[ "${name:-}" == "ipv6-hp-bpf" ]]; then |
| 34 | + buildfile="${REPO_ROOT}/bpf-prog/ipv6-hp-bpf/linux.Dockerfile" |
| 35 | + elif [[ -n "${name:-}" ]]; then |
| 36 | + buildfile="${REPO_ROOT}/${name}/Dockerfile" |
| 37 | + fi |
| 38 | + |
| 39 | + if [[ -n "${buildfile:-}" && -f "${buildfile}" ]]; then |
| 40 | + grep -m1 '^FROM.*golang' "${buildfile}" | awk '{print $3}' || true |
| 41 | + fi |
| 42 | + fi |
| 43 | +} |
| 44 | + |
| 45 | +if [[ -z "${MSFT_GO_IMAGE:-}" ]]; then |
| 46 | + MSFT_GO_IMAGE="$(resolve_go_image)" |
| 47 | + MSFT_GO_IMAGE="${MSFT_GO_IMAGE:-$DEFAULT_IMAGE}" |
| 48 | +fi |
| 49 | + |
| 50 | +ARCH="${ARCH:-amd64}" |
| 51 | + |
| 52 | +# Extract /usr/local/go from the image without needing a Docker daemon. |
| 53 | +# crane export streams the full image filesystem; we extract just usr/local/go. |
| 54 | +crane export --platform "linux/${ARCH}" "$MSFT_GO_IMAGE" - | sudo tar -xf - -C / usr/local/go |
| 55 | + |
| 56 | +echo "##vso[task.prependpath]/usr/local/go/bin" |
0 commit comments