-
Notifications
You must be signed in to change notification settings - Fork 12
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 }} |
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 | ||
} | ||
|
||
|
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will the cmd application run here forever? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
|
||
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" |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.