Skip to content

Commit

Permalink
Add --branch option to specify default branch #14
Browse files Browse the repository at this point in the history
  • Loading branch information
undergroundwires committed Mar 6, 2022
1 parent 2579bbf commit 5d9c038
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 29 deletions.
54 changes: 36 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@

## Features

Allows you to automatically:
🤖 Allows you to automatically:

- Bump your sematic git tag by increasing the patch version
- Create & commit a changelog file
- If `npm` project then bump `package.json` version and commit
- Bump your semantic git tag by increasing the patch version.
- Create & commit a changelog file.
- If `npm` project then bump `package.json` version and commit.
- Check `README.md` file, if it has references to older version, update with never version.
- Create a release on GitHub with auto-generated release notes
- Create a release on GitHub with auto-generated release notes.

It supports safe re-runs, it means that if you can run it for an already bumped or tagged repository, it'll not increase the version as everything is still up-to-date. It protects against recursive runs.
✅ Other features include:

- Zero-configuration for most use-cases, but can still be customized.
- It supports safe re-runs, it means that if you can run it for an already bumped or tagged repository, it'll not increase the version as everything is still up-to-date. It protects against recursive runs.

## Usage

Expand Down Expand Up @@ -60,6 +63,12 @@ It supports safe re-runs, it means that if you can run it for an already bumped
# If you use default, it'll not trigger other actions, but your own PAT then it triggers new actions
# (Optional) Default: ${{ github.token }}
release-token: ''

# Git branch to push the changes such as version tags, CHANGELOG file, version changes...
# Configuring this should not be needed for most use-cases.
# Use this only if you DO NOT use a single main default branch (e.g. `master` or `main`).
# (Optional) Default: Default "git clone" behavior. Checks out to default branch of remote.
branch: ''
```
[↑](#bump-everywhere)
Expand All @@ -72,13 +81,18 @@ It supports safe re-runs, it means that if you can run it for an already bumped
- Run with arguments:

```sh
docker run undergroundwires/bump-everywhere \
--repository "undergroundwires/privacy.sexy" \
--user "bot-user" \
--git-token "GitHub PAT for pushes" \
--release-type "prerelease" \
--release-token "GitHub PAT for releases" \
args=(
# Required:
--repository "undergroundwires/privacy.sexy"
--user "bot-user"
--git-token "GitHub PAT for pushes"
--release-type "prerelease"
--release-token "GitHub PAT for releases"
--commit-message "⬆️ bump to {{version}}"
# Optional:
--branch 'custom branch name'
)
docker run undergroundwires/bump-everywhere "${args[@]}"
```

[↑](#bump-everywhere)
Expand Down Expand Up @@ -106,11 +120,15 @@ It supports safe re-runs, it means that if you can run it for an already bumped

## Updating minor & major versions

- You manually tag your last commit to update major & minor versions.
- E.g.
- `git commit -m "bumped version to 1.3.1" --allow-empty`
- `git tag 1.3.1`
- `git push && git push origin 1.3.1`
bump-everywhere only increases your patch versions. You manually tag your last commit to update major & minor versions.

E.g. :

```sh
git commit -m "bumped version to 1.3.1" --allow-empty
git tag 1.3.1
git push && git push origin 1.3.1
```

[↑](#bump-everywhere)

Expand Down Expand Up @@ -140,7 +158,7 @@ You can also use following scripts individually (check script files for usage, p

## Support

**Sponsor 💕**. Consider one time or recurring donation on [GitHub Sponsors](https://github.com/sponsors/undergroundwires) or [any other way (undrgroundwires.dev/donate)](https://undergroundwires.dev/donate), every penny you leave will help development and maintainance of the project .
**Sponsor 💕**. Consider one time or recurring donation on [GitHub Sponsors](https://github.com/sponsors/undergroundwires) or [any other way (undrgroundwires.dev/donate)](https://undergroundwires.dev/donate), every penny you leave will help development and maintenance of the project .

**Star 🤩**. If you do cannot do that you can just give it a star ⭐ . It helps me to see that the project is appreciated.

Expand Down
52 changes: 41 additions & 11 deletions scripts/bump-everywhere.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
# Bumps version, creates changelog and creates a release
# Example usage:
# bash "scripts/bump-everywhere.sh" \
# --repository "undergroundwires/privacy.sexy" \
# --user "bot-commiter-name" \
# --git-token "PAT_TOKEN" \
# --release-token "PAT_TOKEN"
# --repository 'undergroundwires/privacy.sexy' \
# --user 'bot-commiter-name' \
# --git-token 'PAT_TOKEN' \
# --release-token 'PAT_TOKEN' \
# --release-type 'prerelease' \
# --commit-message '⬆️ bump to {{version}}' \
# --branch 'master' # <- Optional parameter
# Prerequisites:
# - Ensure git is installed
# - Ensure you have curl and jq installed e.g. with apk add curl jq git
Expand All @@ -32,24 +35,37 @@ print_name() {
}

clone () {
local -r repository="$1" git_token="$2"
local -r repository="$1" git_token="$2" branch="$3"
echo "Cloning $repository"

local temp_directory
if ! temp_directory=$(mktemp -d) \
|| utilities::is_empty_or_null "$temp_directory"; then
echo "Could not create a temporary directory"
exit 1;
fi

git clone "https://x-access-token:$git_token@github.com/$repository.git" "$temp_directory" \
|| { echo "git clone failed for $repository"; exit 1; }
cd "$temp_directory" \
|| { echo "Could not locate folder $temp_directory"; exit 1; }

if utilities::has_value "$branch"; then
if [[ $(git branch --show-current) != "$branch" ]]; then
git switch "$branch" || {
>&2 echo "❌ Could not switch to specified branch: \"$branch\"."
exit 1
}
fi
fi

local latest_commit_sha
if ! latest_commit_sha=$(git log -1 --format="%H") \
|| utilities::is_empty_or_null "$latest_commit_sha"; then
echo "Could not retrieve latest commit sha"
exit 1
fi

echo "Latest commit sha: $latest_commit_sha"
}

Expand Down Expand Up @@ -148,20 +164,24 @@ create_release() {
}

validate_parameters() {
local -r repository="$1" git_user="$2" git_token="$3" release_type="$4" release_token="$5" commit_message="$6"
local -r repository="$1" git_user="$2" git_token="$3" release_type="$4" release_token="$5" commit_message="$6" branch="$7"
if utilities::is_empty_or_null "$repository"; then echo "Repository name is not set."; exit 1; fi;
if utilities::is_empty_or_null "$git_user"; then echo "Git user is not set."; exit 1; fi;
if utilities::is_empty_or_null "$git_token"; then echo "Git access token is not set."; exit 1; fi;
if utilities::is_empty_or_null "$release_type"; then echo "Release type is not set."; exit 1; fi;
if utilities::is_empty_or_null "$release_token"; then echo "Release access token is not set."; exit 1; fi;
if utilities::is_empty_or_null "$commit_message"; then echo "Commit message is not set."; exit 1; fi;
if utilities::has_value "$branch" && ! is_valid_branch_name "$branch"; then
>&2 echo "\"$branch\" is not a valid branch name."
exit 1
fi
}

main() {
local -r repository="$1" git_user="$2" git_token="$3" release_type="$4" release_token="$5" commit_message="$6"
validate_parameters "$repository" "$git_user" "$git_token" "$release_type" "$release_token" "$commit_message"
local -r repository="$1" git_user="$2" git_token="$3" release_type="$4" release_token="$5" commit_message="$6" branch="$7"
validate_parameters "$repository" "$git_user" "$git_token" "$release_type" "$release_token" "$commit_message" "$branch"
print_name
clone "$repository" "$git_token"
clone "$repository" "$git_token" "$branch"
if is_rerun "$commit_message"; then
echo "It's a re-run of the script, versioning will be skipped";
else
Expand All @@ -175,11 +195,20 @@ main() {
exit 1
fi
update_npm
commit_and_push "$version_tag" "$commit_message"
commit_and_push "$version_tag" "$commit_message" "$branch"
fi
create_release "$repository" "$release_token" "$release_type"
}

is_valid_branch_name() {
local -r branch_name="$1"
if git check-ref-format --branch "$branch_name" 2>/dev/null; then
return 0
else
return 1
fi
}

# Parse parameters
while [[ "$#" -gt 0 ]]; do case $1 in
--repository) REPOSITORY="$2"; shift;;
Expand All @@ -188,7 +217,8 @@ while [[ "$#" -gt 0 ]]; do case $1 in
--release-type) RELEASE_TYPE="$2"; shift;;
--release-token) RELEASE_TOKEN="$2"; shift;;
--commit-message) COMMIT_MESSAGE="$2"; shift;;
--branch) BRANCH="$2"; shift;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac; shift; done

main "$REPOSITORY" "$GIT_USER" "$GIT_TOKEN" "$RELEASE_TYPE" "$RELEASE_TOKEN" "$COMMIT_MESSAGE"
main "$REPOSITORY" "$GIT_USER" "$GIT_TOKEN" "$RELEASE_TYPE" "$RELEASE_TOKEN" "$COMMIT_MESSAGE" "$BRANCH"

0 comments on commit 5d9c038

Please sign in to comment.