|
| 1 | +# SPDX-FileCopyrightText: 2026 INDUSTRIA DE DISEÑO TEXTIL S.A. (INDITEX S.A.) |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +name: code-gomod-go-PR-verify |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, ready_for_review, reopened] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + |
| 13 | +concurrency: |
| 14 | + group: code-gomod-go-pr-verify-${{ github.repository }}-${{ github.event.pull_request.number }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +env: |
| 18 | + WORKING_DIRECTORY: ${{ vars.WORKING_DIRECTORY }} |
| 19 | + # The project-owned verify command travels in the managed component descriptor, |
| 20 | + # never in an Actions variable: the repo owns its build, governance owns only the |
| 21 | + # envelope that invokes it. |
| 22 | + COMPONENT_DESCRIPTOR: .github/inditextech-ci-go.json |
| 23 | + ASDF_BRANCH_VERSION: '0.18.0' |
| 24 | + |
| 25 | +jobs: |
| 26 | + verify: |
| 27 | + name: Code / Verify |
| 28 | + runs-on: ubuntu-24.04 |
| 29 | + timeout-minutes: 30 |
| 30 | + steps: |
| 31 | + - name: Check out pull request head |
| 32 | + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 |
| 33 | + with: |
| 34 | + fetch-depth: 0 |
| 35 | + persist-credentials: false |
| 36 | + ref: ${{ github.event.pull_request.head.sha }} |
| 37 | + |
| 38 | + - name: Read governed tool versions |
| 39 | + id: tool-versions |
| 40 | + working-directory: ${{ env.WORKING_DIRECTORY }} |
| 41 | + shell: bash |
| 42 | + run: | |
| 43 | + set -euo pipefail |
| 44 | + resolved="" |
| 45 | + if [[ -f .tool-versions ]]; then |
| 46 | + # Validate only real "<plugin> <version>" lines; asdf allows # comments (SPDX/REUSE) and blank lines. |
| 47 | + if grep -vE '^[[:space:]]*(#|$)' .tool-versions | grep -qvE '^[a-zA-Z0-9_-]+ [a-zA-Z0-9._+-]+$'; then |
| 48 | + echo "::error title=Invalid tool-versions::${WORKING_DIRECTORY}/.tool-versions is malformed." |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | + # Emit only the governed golang pin so asdf ignores project extra pins (e.g. kind). |
| 52 | + resolved="$(grep -E '^golang ' .tool-versions || true)" |
| 53 | + fi |
| 54 | + if [[ -z "$resolved" ]]; then |
| 55 | + # No committed golang pin: derive it from the module's own go directive so the |
| 56 | + # governed lane follows the product's declared toolchain instead of failing or |
| 57 | + # clamping it. A committed pin always wins over the derived one. |
| 58 | + if [[ ! -f go.mod ]]; then |
| 59 | + echo "::error title=Missing go toolchain::${WORKING_DIRECTORY} has no golang pin in .tool-versions and no go.mod to derive one from." |
| 60 | + exit 1 |
| 61 | + fi |
| 62 | + version="$(sed -nE 's/^toolchain[[:space:]]+go([0-9]+\.[0-9]+(\.[0-9]+)?)[[:space:]]*$/\1/p' go.mod | head -n 1)" |
| 63 | + if [[ -z "$version" ]]; then |
| 64 | + version="$(sed -nE 's/^go[[:space:]]+([0-9]+\.[0-9]+(\.[0-9]+)?)[[:space:]]*$/\1/p' go.mod | head -n 1)" |
| 65 | + fi |
| 66 | + if [[ -z "$version" ]]; then |
| 67 | + echo "::error title=Missing go toolchain::${WORKING_DIRECTORY}/go.mod declares no usable go version." |
| 68 | + exit 1 |
| 69 | + fi |
| 70 | + # asdf installs an exact patch release; a two-part directive (go 1.21) means .0. |
| 71 | + if [[ "$version" != *.*.* ]]; then |
| 72 | + version="${version}.0" |
| 73 | + fi |
| 74 | + resolved="golang ${version}" |
| 75 | + echo "::notice title=Derived go toolchain::Using ${resolved} from ${WORKING_DIRECTORY}/go.mod." |
| 76 | + fi |
| 77 | + { |
| 78 | + echo "tool_versions<<EOF" |
| 79 | + echo "$resolved" |
| 80 | + echo "EOF" |
| 81 | + } >> "$GITHUB_OUTPUT" |
| 82 | +
|
| 83 | + - name: Set up asdf-managed Go |
| 84 | + uses: asdf-vm/actions/install@b7bcd026f18772e44fe1026d729e1611cc435d47 # v4.0.1 |
| 85 | + with: |
| 86 | + tool_versions: ${{ steps.tool-versions.outputs.tool_versions }} |
| 87 | + asdf_version: ${{ env.ASDF_BRANCH_VERSION }} |
| 88 | + |
| 89 | + - name: Resolve governed verify command |
| 90 | + id: descriptor |
| 91 | + shell: bash |
| 92 | + run: | |
| 93 | + set -euo pipefail |
| 94 | + if [[ ! -f "$COMPONENT_DESCRIPTOR" ]]; then |
| 95 | + echo "::error title=Missing descriptor::${COMPONENT_DESCRIPTOR} not found." |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | + # The descriptor is governance-owned and schema-validated; verify_command |
| 99 | + # always resolves, but fail closed if it is somehow blank. |
| 100 | + verify_command="$(jq -er '.verify_command // "make verify"' "$COMPONENT_DESCRIPTOR")" |
| 101 | + if [[ -z "$verify_command" ]]; then |
| 102 | + echo "::error title=Empty verify command::descriptor verify_command must not be blank." |
| 103 | + exit 1 |
| 104 | + fi |
| 105 | + { |
| 106 | + echo "verify_command<<EOF" |
| 107 | + echo "$verify_command" |
| 108 | + echo "EOF" |
| 109 | + } >> "$GITHUB_OUTPUT" |
| 110 | +
|
| 111 | + - name: Verify and test |
| 112 | + working-directory: ${{ env.WORKING_DIRECTORY }} |
| 113 | + shell: bash |
| 114 | + env: |
| 115 | + VERIFY_COMMAND: ${{ steps.descriptor.outputs.verify_command }} |
| 116 | + run: | |
| 117 | + set -euo pipefail |
| 118 | + # The project owns its build; governance only invokes the governed |
| 119 | + # verify_command inside the pinned working directory. |
| 120 | + bash -c "$VERIFY_COMMAND" |
0 commit comments