mirror of
https://github.com/RetroDECK/RetroDECK.git
synced 2025-04-10 19:15:12 +00:00
PRE_BUILD_AUTOMATION: testing a different script
This commit is contained in:
parent
b48f25cc86
commit
5ae3556020
|
@ -1,5 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
|
# Set script to exit immediately on any error
|
||||||
|
set -e
|
||||||
|
|
||||||
# For the file paths to work correctly, call this script with this command from the cloned repo folder root:
|
# For the file paths to work correctly, call this script with this command from the cloned repo folder root:
|
||||||
# sh automation_tools/pre_build_automation.sh
|
# sh automation_tools/pre_build_automation.sh
|
||||||
# Different actions need different information in the task list file
|
# Different actions need different information in the task list file
|
||||||
|
@ -31,16 +34,22 @@
|
||||||
# This command does not need a PLACEHOLDERTEXT field in the task list, so needs to be in this syntax: custom_command^^$COMMAND
|
# This command does not need a PLACEHOLDERTEXT field in the task list, so needs to be in this syntax: custom_command^^$COMMAND
|
||||||
# url: This is used to calculate a dynamic URL and the value to the $caluculated_url environmental variable, for use in other subsequent commands.
|
# url: This is used to calculate a dynamic URL and the value to the $caluculated_url environmental variable, for use in other subsequent commands.
|
||||||
|
|
||||||
rd_manifest=${GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml
|
|
||||||
automation_task_list=${GITHUB_WORKSPACE}/automation_tools/automation_task_list.cfg
|
|
||||||
current_branch=$(git rev-parse --abbrev-ref HEAD)
|
|
||||||
|
|
||||||
# During the PR automated tests instead of the branch name is returned "HEAD", fixing it
|
# Define paths
|
||||||
if [ $current_branch == "HEAD" ]; then
|
rd_manifest="${GITHUB_WORKSPACE}/net.retrodeck.retrodeck.yml"
|
||||||
echo "Looks like we are on a PR environment, retrieving the branch name from which the PR is raised."
|
automation_task_list="${GITHUB_WORKSPACE}/automation_tools/automation_task_list.cfg"
|
||||||
current_branch=$(echo $GITHUB_REF | sed 's@refs/heads/@@')
|
|
||||||
echo "The branch name from which the PR is raised is \"$current_branch\"."
|
# Retrieve current git branch
|
||||||
|
get_current_branch() {
|
||||||
|
local branch=$(git rev-parse --abbrev-ref HEAD)
|
||||||
|
if [ "$branch" == "HEAD" ]; then
|
||||||
|
echo "$GITHUB_REF" | sed 's@refs/heads/@@'
|
||||||
|
else
|
||||||
|
echo "$branch"
|
||||||
fi
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
current_branch=$(get_current_branch)
|
||||||
|
|
||||||
echo "Manifest location: $rd_manifest"
|
echo "Manifest location: $rd_manifest"
|
||||||
echo "Automation task list location: $automation_task_list"
|
echo "Automation task list location: $automation_task_list"
|
||||||
|
@ -49,103 +58,100 @@ echo "Task list contents:"
|
||||||
cat "$automation_task_list"
|
cat "$automation_task_list"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
# Update all collected information
|
# Functions to handle different actions
|
||||||
while IFS="^" read -r action placeholder url branch || [[ -n "$action" ]];
|
handle_branch() {
|
||||||
do
|
local placeholder="$1"
|
||||||
if [[ ! $action == "#"* ]] && [[ ! -z "$action" ]]; then
|
echo "Replacing placeholder $placeholder with branch $current_branch"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$current_branch"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_hash() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local url="$2"
|
||||||
|
local calculated_url=$(eval echo "$url")
|
||||||
|
local hash=$(curl -sL "$calculated_url" | sha256sum | cut -d ' ' -f1)
|
||||||
|
echo "Replacing placeholder $placeholder with hash $hash"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$hash"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_latestcommit() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local url="$2"
|
||||||
|
local branch="$3"
|
||||||
|
local commit=$(git ls-remote "$url" "$branch" | cut -f1)
|
||||||
|
echo "Replacing placeholder $placeholder with latest commit $commit"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$commit"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_latestghtag() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local url="$2"
|
||||||
|
local tag=$(git ls-remote --tags "$url" | tail -n 1 | cut -f2 | sed 's|refs/tags/||')
|
||||||
|
echo "Replacing placeholder $placeholder with latest tag $tag"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$tag"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_latestghrelease() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local url="$2"
|
||||||
|
local suffix="$3"
|
||||||
|
local release_data=$(curl -s "$url")
|
||||||
|
local ghreleaseurl=$(echo "$release_data" | jq -r ".assets[] | select(.name | endswith(\"$suffix\")).browser_download_url")
|
||||||
|
local ghreleasehash=$(curl -sL "$ghreleaseurl" | sha256sum | cut -d ' ' -f1)
|
||||||
|
echo "Replacing placeholder $placeholder with URL $ghreleaseurl and hash $ghreleasehash"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$ghreleaseurl"'^g' "$rd_manifest"
|
||||||
|
/bin/sed -i 's^'"HASHFOR$placeholder"'^'"$ghreleasehash"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_outside_file() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local file_path="$2"
|
||||||
|
if [[ "$file_path" == \$* ]]; then
|
||||||
|
eval file_path="$file_path"
|
||||||
|
fi
|
||||||
|
local content=$(cat "$file_path")
|
||||||
|
echo "Replacing placeholder $placeholder with content of file $file_path"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$content"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_outside_env_var() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local var_name="$2"
|
||||||
|
if [[ "$var_name" == \$* ]]; then
|
||||||
|
eval var_name="$var_name"
|
||||||
|
fi
|
||||||
|
local value=$(echo "$var_name")
|
||||||
|
echo "Replacing placeholder $placeholder with environment variable $value"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$value"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_custom_command() {
|
||||||
|
local command="$1"
|
||||||
|
echo "Executing custom command: $command"
|
||||||
|
eval "$command"
|
||||||
|
}
|
||||||
|
|
||||||
|
handle_url() {
|
||||||
|
local placeholder="$1"
|
||||||
|
local url="$2"
|
||||||
|
local calculated_url=$(eval echo "$url")
|
||||||
|
echo "Replacing placeholder $placeholder with calculated URL $calculated_url"
|
||||||
|
/bin/sed -i 's^'"$placeholder"'^'"$calculated_url"'^g' "$rd_manifest"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Process the task list
|
||||||
|
while IFS="^" read -r action placeholder url branch || [[ -n "$action" ]]; do
|
||||||
|
if [[ ! "$action" == "#"* ]] && [[ -n "$action" ]]; then
|
||||||
case "$action" in
|
case "$action" in
|
||||||
|
"branch" ) handle_branch "$placeholder" ;;
|
||||||
"branch" )
|
"hash" ) handle_hash "$placeholder" "$url" ;;
|
||||||
echo
|
"latestcommit" ) handle_latestcommit "$placeholder" "$url" "$branch" ;;
|
||||||
echo "Placeholder text: $placeholder"
|
"latestghtag" ) handle_latestghtag "$placeholder" "$url" ;;
|
||||||
echo "Current branch:" "$current_branch"
|
"latestghrelease" ) handle_latestghrelease "$placeholder" "$url" "$branch" ;;
|
||||||
echo
|
"outside_file" ) handle_outside_file "$placeholder" "$url" ;;
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$current_branch"'^g' $rd_manifest
|
"outside_env_var" ) handle_outside_env_var "$placeholder" "$url" ;;
|
||||||
;;
|
"custom_command" ) handle_custom_command "$url" ;;
|
||||||
|
"url" ) handle_url "$placeholder" "$url" ;;
|
||||||
"hash" )
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
calculated_url=$(eval echo "$url") # in case the url has to be calculated from an expression
|
|
||||||
echo "URL to hash: $calculated_url"
|
|
||||||
echo
|
|
||||||
hash=$(curl -sL "$calculated_url" | sha256sum | cut -d ' ' -f1)
|
|
||||||
echo "Hash found: $hash"
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$hash"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
"latestcommit" )
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
echo "Repo to get latest commit from: $url branch: $branch"
|
|
||||||
echo
|
|
||||||
commit=$(git ls-remote "$url" "$branch" | cut -f1)
|
|
||||||
echo "Commit found: $commit"
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$commit"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
"latestghtag" )
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
echo "Repo to get the latest tag from: $url"
|
|
||||||
echo
|
|
||||||
tag=$(git ls-remote "$url" | tail -n 1 | cut -f2 | sed 's|refs/tags/||')
|
|
||||||
echo "Tag found: $tag"
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$tag"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
"latestghrelease" )
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
echo "Repo to look for latest releases: $url"
|
|
||||||
echo
|
|
||||||
ghreleaseurl=$(curl -s "$url" | grep browser_download_url | grep "$branch\""$ | cut -d : -f 2,3 | tr -d \" | sed -n 1p | tr -d ' ')
|
|
||||||
echo "GitHub release URL found: $ghreleaseurl"
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$ghreleaseurl"'^' $rd_manifest
|
|
||||||
ghreleasehash=$(curl -sL "$ghreleaseurl" | sha256sum | cut -d ' ' -f1)
|
|
||||||
echo "GitHub release hash found: $ghreleasehash"
|
|
||||||
/bin/sed -i 's^'"HASHFOR$placeholder"'^'"$ghreleasehash"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
"outside_file" )
|
|
||||||
if [[ "$url" = \$* ]]; then # If value is a reference to a variable name
|
|
||||||
eval url="$url"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
echo "Information being injected: $(cat $url)"
|
|
||||||
echo
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$(cat $url)"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
"outside_env_var" )
|
|
||||||
if [[ "$url" = \$* ]]; then # If value is a reference to a variable name
|
|
||||||
eval url="$url"
|
|
||||||
fi
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
echo "Information being injected: $(echo $url)"
|
|
||||||
echo
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$(echo $url)"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
"custom_command" )
|
|
||||||
echo
|
|
||||||
echo "Command to run: $url"
|
|
||||||
echo
|
|
||||||
eval "$url"
|
|
||||||
;;
|
|
||||||
|
|
||||||
"url" )
|
|
||||||
# this is used to calculate a dynamic url
|
|
||||||
echo
|
|
||||||
echo "Placeholder text: $placeholder"
|
|
||||||
calculated_url=$(eval echo "$url")
|
|
||||||
echo "Information being injected: $calculated_url"
|
|
||||||
echo
|
|
||||||
/bin/sed -i 's^'"$placeholder"'^'"$calculated_url"'^' $rd_manifest
|
|
||||||
;;
|
|
||||||
|
|
||||||
esac
|
esac
|
||||||
fi
|
fi
|
||||||
done < "$automation_task_list"
|
done < "$automation_task_list"
|
||||||
|
|
Loading…
Reference in a new issue