Skip to content

Commit

Permalink
fix: script release broke while using git with the backup strategy (#104
Browse files Browse the repository at this point in the history
)
  • Loading branch information
levisingularity authored Sep 18, 2024
1 parent f66058c commit 019b814
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 52 deletions.
12 changes: 6 additions & 6 deletions scripts/deployment/definitions.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
[
{
"name": "das-metta-parser",
"repository": "git@github.com:singnet/das-metta-parser.git",
"repository": "https://github.com/singnet/das-metta-parser.git",
"workflow": "build.yml",
"ref": "master",
"hooks": {},
"dependencies": []
},
{
"name": "das-toolbox",
"repository": "git@github.com:singnet/das-toolbox.git",
"repository": "https://github.com/singnet/das-toolbox.git",
"workflow": "build.yml",
"ref": "master",
"hooks": {
Expand All @@ -20,7 +20,7 @@
},
{
"name": "hyperon-das-atomdb",
"repository": "git@github.com:singnet/das-atom-db.git",
"repository": "https://github.com/singnet/das-atom-db.git",
"workflow": "publish-pypi.yml",
"ref": "master",
"hooks": {
Expand All @@ -30,7 +30,7 @@
},
{
"name": "hyperon-das",
"repository": "git@github.com:singnet/das-query-engine.git",
"repository": "https://github.com/singnet/das-query-engine.git",
"workflow": "publish-pypi.yml",
"ref": "master",
"hooks": {
Expand All @@ -41,7 +41,7 @@
},
{
"name": "das-serverless-functions",
"repository": "git@github.com:singnet/das-serverless-functions.git",
"repository": "https://github.com/singnet/das-serverless-functions.git",
"workflow": "vultr-build.yml",
"ref": "master",
"hooks": {
Expand All @@ -51,7 +51,7 @@
},
{
"name": "das",
"repository": "git@github.com:singnet/das.git",
"repository": "https://github.com/singnet/das.git",
"workflow": "publish.yml",
"ref": "master",
"hooks": {
Expand Down
4 changes: 2 additions & 2 deletions scripts/deployment/hooks/common/dependency-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ if [ "$is_dependency_updated" -eq 0 ]; then
if boolean_prompt "$dependency hasn't been updated during this release process. Do you want to fetch the latest version from the repository? [yes/no]"; then
dependency_definition=$(retrieve_json_object_with_property_value "$definitions" "name" "$dependency")

IFS='|' read -r dependency_package_name package_repository package_workflow package_repo_ref package_hooks <<<"$(extract_package_details "$dependency_definition")"
IFS='|' read -r dependency_package_name dependency_repository dependency_workflow dependency_repo_ref dependency_hooks <<<"$(extract_package_details "$dependency_definition")"

local package_repository_folder=$(clone_repo_to_temp_dir "$package_repository" "$package_repo_ref")
local package_repository_folder=$(clone_repo_to_temp_dir "$dependency_repository" "$dependency_repo_ref")

cd "$package_repository_folder"

Expand Down
9 changes: 5 additions & 4 deletions scripts/deployment/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ set -e
workdir=$(pwd)
hooks_path="$workdir/scripts/deployment/hooks"
definitions_path="$workdir/scripts/deployment/definitions.json"
github_token_path="$workdir/scripts/deployment/gh_token"

source "$workdir/scripts/deployment/utils.sh"
source "$workdir/scripts/deployment/workflow.sh"

# GLOBAL VARIABLES
required_commands=(git jq curl)
required_commands=(git jq curl gh)
packages_pending_update=""
github_token=$(load_or_request_github_token "$github_token_path")
definitions=$(jq -c '.' "$definitions_path")

function verify_dependencies_updated() {
Expand Down Expand Up @@ -191,7 +189,7 @@ function update_package() {

cd "$repository_path"
show_git_diff
commit_and_push_changes "Chores: create a new release version $new_version" $repository_ref
commit_and_push_changes "Chores: create a new release version $new_version" "$repository_ref"
trigger_package_workflow "$package_name" "$workflow_inputs" "$repository_owner" "$repository_name" "$repository_workflow" "$repository_ref"
cd - &>/dev/null
print ":green:Package $package_name updated successfully:/green:"
Expand Down Expand Up @@ -246,6 +244,9 @@ function main() {
empty_backup_answers
}


setup_gh_auth

if has_backup_answers; then
main "$@" < "$backup_answers"
fi
Expand Down
83 changes: 43 additions & 40 deletions scripts/deployment/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,29 @@

# PATHS
workdir=$(pwd)
github_token_path="$workdir/scripts/deployment/gh_token"

# GLOBAL VARIABLES
colors=("reset" "red" "green" "blue" "yellow")
color_codes=("\033[0m" "\033[0;31m" "\033[0;32m" "\033[0;34m" "\033[0;33m")
backup_answers="$workdir/.output~"
backup_enabled=true


function print() {
local text="$1"

for i in "${!colors[@]}"; do
local color="${colors[$i]}"
local code="${color_codes[$i]}"
text="${text//:$color:/$code}"
text="${text//:\/$color:/${color_codes[0]}}"
done

echo -e "$text"
}


function requirements() {
local required_commands=("$@")

Expand Down Expand Up @@ -49,7 +65,7 @@ function prompt() {
read_input "$1"
else
if read -r input; then
echo $input
echo "$input"
else
exec < /dev/tty
read_input "$1"
Expand Down Expand Up @@ -111,26 +127,6 @@ function verify_file_exists() {
fi
}

function configure_git_identity() {
local git_user="${GIT_USER_NAME:-$(git config user.name)}"
local git_email="${GIT_USER_EMAIL:-$(git config user.email)}"

if [[ -z "$git_user" ]]; then
git_user=$(text_prompt "Your Git username is not configured. Please provide your Git name: ")
export GIT_USER_NAME="$git_user"
fi

if [[ -z "$git_email" ]]; then
git_email=$(text_prompt "Your Git email is not configured. Please provide your Git email: ")
export GIT_USER_EMAIL="$git_email"
fi

git config user.name "$git_user"
git config user.email "$git_email"

print "Git configured with username: :green:$git_user:/green: and email: :green:$git_email:/green:"
}

function check_for_uncommitted_changes() {
if git diff --quiet && git diff --staged --quiet; then
print ":yellow:Skipping commit changes because no files were changed to be committed:/yellow:"
Expand All @@ -150,6 +146,15 @@ function show_git_diff() {

}

function set_git_identity() {
local gh_user=$(gh api user | jq -r '.login')
local gh_email=$(gh api user/emails | jq -r '.[0].email')

print ":green:Setting git identity to $gh_user:$gh_email:/green:"
git config user.name "$gh_user"
git config user.email "$gh_email"
}

function commit_and_push_changes() {
local commit_msg="$1"
local target_branch="${2:-master}"
Expand All @@ -159,40 +164,38 @@ function commit_and_push_changes() {
return
fi

configure_git_identity
set_git_identity

git add $files_to_add

git commit -m "$commit_msg"

print ":green:Sending changes to branch '$target_branch'.:/green:"
git push origin "$target_branch"

print ":green:Changes committed and pushed to the branch '$target_branch'.:/green:"
print ":green:Changes sent to branch '$target_branch'.:/green:"
}

function setup_gh_auth() {
local github_token=$(load_or_request_github_token "$github_token_path")

gh auth login --web

gh auth setup-git
}

function clone_repo_to_temp_dir() {
local package_repository="$1"
local target_branch="${2:-master}"
local tmp_folder=$(mktemp -d)

git clone "$package_repository" "$tmp_folder" &>/dev/null
gh repo clone "$package_repository" "$tmp_folder"

git -C "$tmp_folder" checkout "$target_branch" &>/dev/null
git -C "$tmp_folder" checkout "$target_branch" --quiet

echo "$tmp_folder"
}

function print() {
local text="$1"

for i in "${!colors[@]}"; do
local color="${colors[$i]}"
local code="${color_codes[$i]}"
text="${text//:$color:/$code}"
text="${text//:\/$color:/${color_codes[0]}}"
done

echo -e "$text"
}

function load_or_request_github_token() {
local secret_path="$1"
local secret=""
Expand Down Expand Up @@ -295,8 +298,8 @@ function execute_ssh_commands() {

function validate_repository_url() {
local package_repository="$1"
if ! [[ $package_repository =~ git@github.com:([^/]+)/([^/.]+)\.git ]]; then
print ":red:Invalid repository SSH URL format.:/red:"
if ! [[ $package_repository =~ ^https://github\.com/([^/]+)/([^/.]+)\.git$ ]]; then
print ":red:Invalid repository URL format.:/red:"
exit 1
fi
}
Expand Down

0 comments on commit 019b814

Please sign in to comment.