Files
cpd-cli-updater/push_to_gitea.sh

86 lines
2.3 KiB
Bash
Executable File

#!/bin/env bash
#
# push_to_gitea.sh - Push script to Gitea instance
#
# Setup once: export GITEA_USER="your-username" and GITEA_TOKEN="your-git-token"
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_DIR="${SCRIPT_DIR}"
# Default Gitea URL
# Get it from your IBM Gitea instance
GITEA_URL="${GITEA_URL:-https://gitea.corp-softy.ibm.com}"
GITEA_USER="${GITEA_USER:-michael}"
GITEA_TOKEN="${GITEA_TOKEN:-}"
# Create .gitconfig for this repo
create_config() {
cat > "${REPO_DIR}/.gitconfig" << 'EOF'
[remote "gitea"]
url = https://${GITEA_TOKEN}@${GITEA_URL}/corporate-softy/cpd-cli-update-script.git
fetch = +refs/heads/*:refs/remotes/origin/*
# If you want to push to personal fork:
# url = https://${GITEA_TOKEN}@${GITEA_URL}/${GITEA_USER}/cpd-cli-update-script.git
EOF
}
# Setup
setup() {
if [[ ${GITEA_TOKEN} == "" ]]; then
echo "No GITEA_TOKEN set" | tee -a /tmp/cpd-cli-push-$(date +"%Y-%m-%d-%H-%M-%S").log 1
read -p "Press enter to cancel, or paste GITEA_TOKEN:" token
export GITEA_TOKEN="${token:-}"
fi
if [[ ${GITEA_TOKEN} == "" ]]; then
echo "Exiting without GITEA_TOKEN. Please set export GITEA_TOKEN='your-token'"
exit 1
fi
config_exists="${REPO_DIR}/.gitconfig"
if [[ ! -f "${config_exists}" ]] || [[ ! $(grep -i ${GITEA_URL} "${config_exists}" 2>/dev/null || echo "") ]]; then
echo "Creating .gitconfig" | tee -a /tmp/cpd-cli-push-$(date +"%Y-%m-%d-%H-%M-%S").log 1
create_config
fi
}
# Fetch first
fetch() {
git fetch gitea
echo "Fetched from ${GITEA_URL}" | tee -a /tmp/cpd-cli-push-$(date +"%Y-%m-%d-%H-%M-%S").log 1
}
# Add and push
add_push() {
git add -A
local changes
changes=$(git diff-index --quiet HEAD -- || git status -s 2>/dev/null || echo '')
if [[ -z "$changes" ]]; then
echo "No changes to commit" | tee -a /tmp/cpd-cli-push-$(date +"%Y-%m-%d-%H-%M-%S").log 1
exit 0
fi
git commit -m "Automated update from script"
git push gitea master
echo "Pushed to gitea!" | tee -a /tmp/cpd-cli-push-$(date +"%Y-%m-%d-%H-%M-%S").log 1
}
# Main
main() {
echo "Pushing to GitHub: ${GITEA_URL}/${GITEA_USER}/cpd-cli-update-script"
echo ""
setup
fetch
add_push
echo ""
echo "Done!" | tee -a /tmp/cpd-cli-push-$(date +"%Y-%m-%d-%H-%M-%S").log 1
}
main "$@"