|
| 1 | +name: Go checks for images |
| 2 | + |
| 3 | +env: |
| 4 | + GO_BUILD_TAGS: "ce ee se seplus csepro" |
| 5 | + |
| 6 | +on: |
| 7 | + pull_request: |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - main |
| 11 | + |
| 12 | +jobs: |
| 13 | + go_linter: |
| 14 | + name: Go linter for images |
| 15 | + runs-on: [self-hosted, regular] |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout repository |
| 19 | + uses: actions/checkout@v2 |
| 20 | + |
| 21 | + - name: Setup Go environment |
| 22 | + uses: actions/setup-go@v5 |
| 23 | + with: |
| 24 | + go-version: "1.24" |
| 25 | + |
| 26 | + - name: Install golangci-lint |
| 27 | + run: go install github.com/golangci/golangci-lint/cmd/[email protected] |
| 28 | + |
| 29 | + - name: Run Go lint |
| 30 | + run: | |
| 31 | + basedir=$(pwd) |
| 32 | + failed='false' |
| 33 | + for i in $(find images -type f -name go.mod);do |
| 34 | + dir=$(echo $i | sed 's/go.mod$//') |
| 35 | + cd $basedir/$dir |
| 36 | + # check all editions |
| 37 | + for edition in $GO_BUILD_TAGS ;do |
| 38 | + echo "Running linter in $dir (edition: $edition)" |
| 39 | + golangci-lint run --build-tags $edition |
| 40 | + if [ $? -ne 0 ]; then |
| 41 | + echo "Linter failed in $dir (edition: $edition)" |
| 42 | + failed='true' |
| 43 | + fi |
| 44 | + done |
| 45 | + done |
| 46 | + if [ $failed == 'true' ]; then |
| 47 | + exit 1 |
| 48 | + fi |
| 49 | +
|
| 50 | + go_tests: |
| 51 | + name: Go tests for images |
| 52 | + runs-on: [self-hosted, regular] |
| 53 | + |
| 54 | + steps: |
| 55 | + - name: Checkout repository |
| 56 | + uses: actions/checkout@v2 |
| 57 | + |
| 58 | + - name: Setup Go environment |
| 59 | + uses: actions/setup-go@v5 |
| 60 | + with: |
| 61 | + go-version: "1.24" |
| 62 | + |
| 63 | + - name: Run Go tests |
| 64 | + run: | |
| 65 | + basedir=$(pwd) |
| 66 | + failed='false' |
| 67 | + for i in $(find images -type f -name '*_test.go');do |
| 68 | + dir=$(echo $i | sed 's/[a-z_A-Z0-9-]*_test.go$//') |
| 69 | + cd $basedir/$dir |
| 70 | + # check all editions |
| 71 | + for edition in $GO_BUILD_TAGS ;do |
| 72 | + echo "Running tests in $dir (edition: $edition)" |
| 73 | + go test -v -tags $edition |
| 74 | + if [ $? -ne 0 ]; then |
| 75 | + echo "Tests failed in $dir (edition: $edition)" |
| 76 | + failed='true' |
| 77 | + fi |
| 78 | + done |
| 79 | + done |
| 80 | + if [ $failed == 'true' ]; then |
| 81 | + exit 1 |
| 82 | + fi |
| 83 | +
|
| 84 | + go_test_coverage: |
| 85 | + name: Go test coverage for images |
| 86 | + runs-on: [self-hosted, regular] |
| 87 | + |
| 88 | + steps: |
| 89 | + - name: Checkout repository |
| 90 | + uses: actions/checkout@v2 |
| 91 | + |
| 92 | + - name: Setup Go environment |
| 93 | + uses: actions/setup-go@v5 |
| 94 | + with: |
| 95 | + go-version: "1.24" |
| 96 | + |
| 97 | + - name: Run Go test coverage count |
| 98 | + run: | |
| 99 | + if [ ! -d "images" ]; then |
| 100 | + echo "No images/ directory found. Please run this script from the root of the repository." |
| 101 | + exit 1 |
| 102 | + fi |
| 103 | +
|
| 104 | + find images/ -type f -name "go.mod" | while read -r gomod; do |
| 105 | + dir=$(dirname "$gomod") |
| 106 | + |
| 107 | + echo "Test coverage in $dir" |
| 108 | + |
| 109 | + cd "$dir" || continue |
| 110 | + |
| 111 | + for tag in $GO_BUILD_TAGS; do |
| 112 | + echo " Build tag: $tag" |
| 113 | + |
| 114 | + go test ./... -cover -tags "$tag" |
| 115 | + done |
| 116 | + |
| 117 | + cd - > /dev/null |
| 118 | + |
| 119 | + echo "----------------------------------------" |
| 120 | + done |
| 121 | +
|
| 122 | + go_modules_check: |
| 123 | + name: Go modules version |
| 124 | + runs-on: [self-hosted, regular] |
| 125 | + |
| 126 | + steps: |
| 127 | + - name: Checkout repository |
| 128 | + uses: actions/checkout@v2 |
| 129 | + |
| 130 | + - name: Setup Go environment |
| 131 | + uses: actions/setup-go@v5 |
| 132 | + with: |
| 133 | + go-version: "1.24" |
| 134 | + |
| 135 | + - name: Run Go modules version check |
| 136 | + run: | |
| 137 | + search_dir=$(pwd)"/images" |
| 138 | +
|
| 139 | + if [ ! -d "$search_dir" ]; then |
| 140 | + echo "Directory $search_dir does not exist." |
| 141 | + exit 1 |
| 142 | + fi |
| 143 | +
|
| 144 | + temp_dir=$(mktemp -d) |
| 145 | + touch "$temp_dir/incorrect_alert" |
| 146 | +
|
| 147 | + trap 'rm -rf "$temp_dir"' EXIT |
| 148 | +
|
| 149 | + find images/ -type f -name "go.mod" | while read -r gomod; do |
| 150 | + dir=$(dirname "$gomod") |
| 151 | + |
| 152 | + echo "Checking $dir" |
| 153 | + |
| 154 | + cd "$dir" || continue |
| 155 | + |
| 156 | + go list -m all | grep deckhouse | grep -v '=>' | while IFS= read -r line; do |
| 157 | + module_name=$(echo "$line" | awk '{print $1}') |
| 158 | + module_version=$(echo "$line" | awk '{print $2}') |
| 159 | +
|
| 160 | + if [ -z "$module_version" ]; then |
| 161 | + echo " Checking module name $module_name" |
| 162 | + correct_module_name="github.com"/"$GITHUB_REPOSITORY"/"$dir" |
| 163 | + if [ "$module_name" != "$correct_module_name" ]; then |
| 164 | + echo " Incorrect module name: $module_name, expected: $correct_module_name" |
| 165 | + echo " Incorrect module name: $module_name, expected: $correct_module_name" >> "$temp_dir/incorrect_alert" |
| 166 | + else |
| 167 | + echo " Correct module name: $module_name" |
| 168 | + fi |
| 169 | + else |
| 170 | + echo " Checking module tag $module_name" |
| 171 | + repository=$(echo "$line" | awk '{print $1}' | awk -F'/' '{ print "https://"$1"/"$2"/"$3".git" }') |
| 172 | + pseudo_tag=$(echo "$line" | awk '{print $2}') |
| 173 | +
|
| 174 | + echo " Cloning repo $repository into $temp_dir" |
| 175 | + git clone "$repository" "$temp_dir/$repository" >/dev/null 2>&1 |
| 176 | +
|
| 177 | + cd "$temp_dir/$repository" || continue |
| 178 | + |
| 179 | + commit_info=$(git log -1 --pretty=format:"%H %cd" --date=iso-strict -- api/*) |
| 180 | + short_hash=$(echo "$commit_info" | awk '{print substr($1,1,12)}') |
| 181 | + commit_date=$(echo "$commit_info" | awk '{print $2}') |
| 182 | + commit_date=$(date -u -d "$commit_date" +"%Y%m%d%H%M%S") |
| 183 | + actual_pseudo_tag="v0.0.0-"$commit_date"-"$short_hash |
| 184 | + pseudo_tag_date=$(echo $pseudo_tag | awk -F'-' '{ print $2 }') |
| 185 | + echo " Latest commit in $repository: $short_hash $commit_date" |
| 186 | + |
| 187 | + if [[ "$module_version" != "$commit_date" ]]; then |
| 188 | + echo " Incorrect pseudo tag for repo $repository in file "$go_mod_file" (current: "$pseudo_tag", actual:"$actual_pseudo_tag")" |
| 189 | + echo " Incorrect pseudo tag for repo $repository in file "$go_mod_file" (current: "$pseudo_tag", actual:"$actual_pseudo_tag")" >> $temp_dir"/incorrect_alert" |
| 190 | + fi |
| 191 | + |
| 192 | + cd - >/dev/null 2>&1 |
| 193 | + fi |
| 194 | + done |
| 195 | + |
| 196 | + cd - > /dev/null |
| 197 | + |
| 198 | + echo "----------------------------------------" |
| 199 | + done |
| 200 | +
|
| 201 | + alert_lines_count=$(cat $temp_dir"/incorrect_alert" | wc -l) |
| 202 | +
|
| 203 | + if [ $alert_lines_count != 0 ]; then |
| 204 | + echo "We have non-actual pseudo-tags or modules names in repository's go.mod files" |
| 205 | + exit 1 |
| 206 | + fi |
0 commit comments