Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add env check job definition #66

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/env-check.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
name: Env check
on:
workflow_call:
inputs:
prefix:
description: "Env parameter prefix"
required: true
default: "NSM_"
type: string
jobs:
check-env:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
repository: networkservicemesh/.github
path: .github
- uses: actions/checkout@v4
with:
path: cmd
- name: run-envcheck
run: .github/scripts/env-check.sh cmd ${{ inputs.prefix }}
79 changes: 79 additions & 0 deletions scripts/env-check.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env bash

set -e

if [[ "$#" -lt 1 ]]; then
echo "Usage: $(basename "$0") <repo> [env_prefix (default: NSM_)]"
exit 1
fi

repo="$(readlink -e "$1")"
env_prefix="${2:-NSM_}"
cmd_name="cmd"


is_in_order() {
local -a arr
mapfile -t arr < <(echo -en "${1}\n${2}" | sort)

if [[ "${arr[0]}" == "$1" ]]; then
return 0
fi
return 1
}

print_mismatch() {
echo "Mismatch, no ${1} environment variable found in ${2}"
}

build_cmd() {
local -r repo="$1"
local -r output="$2"
pushd . &>/dev/null
cd "$repo"
go build -o "$output" .
popd &>/dev/null
}
Comment on lines +29 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can be done as a step of the github job

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my though here was that it would be more convenient to have the script do the build, and that way you have a contained script that can be called on any repo manually without the job, but I don't really have any strong feelings about it.
I guess in the unit_test approach its a bit cleaner to have this in the job itself.



declare -a readme_envs
mapfile -t readme_envs < <(grep -Eo "${env_prefix}\w+" "${repo}/README.md" | uniq | sort)


build_cmd "$repo" "$cmd_name"
declare -a cmd_envs
# Since this runs on "ubuntu-runner" I can be sure that no env vars are set and cmd fails
mapfile -t cmd_envs < <( "${repo}/${cmd_name}" 2>&1 | grep -Eo "${env_prefix}\w+" | uniq | sort)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will the cmd application run here forever?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I've tested the script on every cmd, and each one of them has terminated. It is probably not the most future proof approach though.
Anyway, I think that in the unit_test approach this will not be an issue, as that does not run the cmd itself only includes the config, I assume.



i=0
j=0
fail=0
while true; do

if [[ "$i" -ge "${#cmd_envs[@]}" ]]; then
break
elif [[ "$j" -ge "${#readme_envs[@]}" ]]; then
break
elif [[ "${cmd_envs[$i]}" == "${readme_envs[$j]}" ]]; then
((i+=1));((j+=1))
elif is_in_order "${cmd_envs[$i]}" "${readme_envs[$j]}" ; then
print_mismatch "${cmd_envs[$i]}" "README.md"
((i+=1))
fail=1
else
print_mismatch "${readme_envs[$j]}" "$cmd_name"
((j+=1))
fail=1
fi
done

for ((;i<${#cmd_envs[@]};++i)); do
print_mismatch "${cmd_envs[$i]}" "README.md"
done
for ((;j<${#readme_envs[@]};++j)); do
print_mismatch "${readme_envs[$j]}" "$cmd_name"
done


exit "$fail"