|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +name: Build binary and container for single arch |
| 5 | + |
| 6 | +permissions: |
| 7 | + contents: read |
| 8 | + |
| 9 | +on: |
| 10 | + workflow_call: |
| 11 | + inputs: |
| 12 | + publishType: |
| 13 | + required: true |
| 14 | + type: string |
| 15 | + arch: |
| 16 | + required: true |
| 17 | + type: string |
| 18 | + |
| 19 | +env: |
| 20 | + EXPECTED_GO_VERSION: "1.24.1" |
| 21 | + |
| 22 | +jobs: |
| 23 | + build: |
| 24 | + name: go build and validate |
| 25 | + runs-on: ${{ inputs.arch == 'amd64' && 'ubuntu-24.04' || 'ubuntu-24.04-arm' }} |
| 26 | + permissions: |
| 27 | + contents: read |
| 28 | + steps: |
| 29 | + - name: checkout |
| 30 | + uses: actions/checkout@v4 |
| 31 | + with: |
| 32 | + path: repo |
| 33 | + |
| 34 | + - name: setup go 1.x |
| 35 | + uses: actions/setup-go@v5 |
| 36 | + with: |
| 37 | + go-version: "${{ env.EXPECTED_GO_VERSION }}" |
| 38 | + id: go |
| 39 | + |
| 40 | + - name: check active go version |
| 41 | + run: | |
| 42 | + go version && which go |
| 43 | +
|
| 44 | + - name: check go.mod |
| 45 | + run: | |
| 46 | + set -x |
| 47 | +
|
| 48 | + if grep -q "go $EXPECTED_GO_VERSION" ./repo/toolkit/tools/go.mod; then |
| 49 | + echo "go.mod has correct version ($EXPECTED_GO_VERSION)" |
| 50 | + else |
| 51 | + actual_version="$(grep -E '^go [0-9]+\.[0-9]+' ./repo/toolkit/tools/go.mod)" |
| 52 | + echo "go.mod has bad version expected:$EXPECTED_GO_VERSION, found: $actual_version" |
| 53 | + echo "UPDATE ./github/workflows/go-test-coverage.yml AND prerequisite documentation if minimum go version changed" |
| 54 | + exit 1 |
| 55 | + fi |
| 56 | +
|
| 57 | + - name: Check for bad go formatting |
| 58 | + run: | |
| 59 | + set -x |
| 60 | +
|
| 61 | + pushd repo/toolkit |
| 62 | + sudo env "PATH=$PATH" make go-fmt-all |
| 63 | + changes=$(git diff *.go) |
| 64 | + if [ -n "$changes" ]; then |
| 65 | + echo Unformatted go files! |
| 66 | + git diff *.go |
| 67 | + exit 1 |
| 68 | + fi |
| 69 | +
|
| 70 | + - name: check for out-of-date go modules |
| 71 | + run: | |
| 72 | + set -x |
| 73 | +
|
| 74 | + pushd repo/toolkit |
| 75 | + sudo env "PATH=$PATH" make go-mod-tidy |
| 76 | + modchanges=$(git diff tools/go.mod) |
| 77 | + sumchanges=$(git diff tools/go.sum) |
| 78 | + if [ -n "$modchanges$sumchanges" ]; then |
| 79 | + echo Module files out of date! |
| 80 | + git diff tools/go.mod |
| 81 | + git diff tools/go.sum |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +
|
| 85 | + - name: check schema.json is up-to-date |
| 86 | + run: | |
| 87 | + set -x |
| 88 | +
|
| 89 | + pushd repo |
| 90 | + make -C toolkit/tools/imagecustomizerschemacli/ |
| 91 | +
|
| 92 | + # Use git diff to check if the schema has changed |
| 93 | + schema_changes=$(git diff toolkit/tools/imagecustomizerapi/schema.json) |
| 94 | + if [ -n "$schema_changes" ]; then |
| 95 | + echo "Schema has changed. Please update the schema using `make -C toolkit/tools/imagecustomizerschemacli/` before committing." |
| 96 | + exit 1 |
| 97 | + else |
| 98 | + echo "Schema is up-to-date!" |
| 99 | + fi |
| 100 | +
|
| 101 | + - name: Build binary |
| 102 | + run: | |
| 103 | + set -x |
| 104 | +
|
| 105 | + # Create version suffix. |
| 106 | + case "${{ inputs.publishType }}" in |
| 107 | + "official") |
| 108 | + PRERELEASE_PARAM="IMAGE_CUSTOMIZER_VERSION_PREVIEW=" |
| 109 | + ;; |
| 110 | + "preview") |
| 111 | + PRERELEASE_PARAM="IMAGE_CUSTOMIZER_VERSION_PREVIEW=-preview.${{github.run_id}}" |
| 112 | + ;; |
| 113 | + "main") |
| 114 | + PRERELEASE_PARAM="IMAGE_CUSTOMIZER_VERSION_PREVIEW=-main.${{github.run_id}}" |
| 115 | + ;; |
| 116 | + *) |
| 117 | + PRERELEASE_PARAM="IMAGE_CUSTOMIZER_VERSION_PREVIEW=-dev.${{github.run_id}}" |
| 118 | + ;; |
| 119 | + esac |
| 120 | +
|
| 121 | + pushd repo/toolkit |
| 122 | +
|
| 123 | + # Build binary. |
| 124 | + sudo env "PATH=$PATH" make imagecustomizer-targz go-imager go-osmodifier $PRERELEASE_PARAM |
| 125 | +
|
| 126 | + # Write version to file. |
| 127 | + PACKAGE_VERSION="$(make --silent printvar-image_customizer_full_version $PRERELEASE_PARAM)" |
| 128 | +
|
| 129 | + popd |
| 130 | +
|
| 131 | + echo "$PACKAGE_VERSION" > "version.txt" |
| 132 | +
|
| 133 | + # Print version. |
| 134 | + echo "Version: $PACKAGE_VERSION" |
| 135 | +
|
| 136 | + - name: Build container |
| 137 | + run: | |
| 138 | + set -x |
| 139 | +
|
| 140 | + CONTAINER_TAG="imagecustomizer:build" |
| 141 | + ./repo/toolkit/tools/imagecustomizer/container/build-container.sh -t "$CONTAINER_TAG" -a "${{ inputs.arch }}" |
| 142 | +
|
| 143 | + docker image save "$CONTAINER_TAG" | gzip > "imagecustomizer.tar.gz" |
| 144 | +
|
| 145 | + - name: Upload version artifact |
| 146 | + if: inputs.arch == 'amd64' |
| 147 | + uses: actions/upload-artifact@v4 |
| 148 | + with: |
| 149 | + name: version |
| 150 | + path: version.txt |
| 151 | + |
| 152 | + - name: Upload binary artifact |
| 153 | + uses: actions/upload-artifact@v4 |
| 154 | + with: |
| 155 | + name: binary-${{ inputs.arch }} |
| 156 | + path: repo/toolkit/out/imagecustomizer.tar.gz |
| 157 | + |
| 158 | + - name: Upload container artifact |
| 159 | + uses: actions/upload-artifact@v4 |
| 160 | + with: |
| 161 | + name: container-${{ inputs.arch }} |
| 162 | + path: imagecustomizer.tar.gz |
0 commit comments