Skip to content
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 version #3

Merged
merged 8 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# create-pr
PR_TICKET_LINK_PREFIX="https://github.com/Purpose-Green/deploy/issues"
PR_LINK_PREFIX_TEXT="Closes: "

# bashunit
BASHUNIT_DEFAULT_PATH="tests"
BASHUNIT_TESTS_ENV="tests/helpers.sh"

10 changes: 6 additions & 4 deletions .github/RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

This is a guide to know the steps to create a new release.

1. Update the version in [DEPLOY_VERSION](../deploy)
1. Update the version in [CHANGELOG.md](../CHANGELOG.md)
1. Build the project `./build.sh bin` - This generates `bin/main` & `bin/checksum`
1. Create a [new release](https://github.com/Purpose-Green/deploy/releases/new) from GitHub
1. Attach `bin/main` and `bin/checksum` to the release
1. Make sure the `DEPLOY_VERSION` is up to date in [deploy](./deploy) entry point
1. You can update this using `./build.sh bin --new major|minor|patch`
1. Update the version in [CHANGELOG.md](../CHANGELOG.md)
1. Run `bin/deploy`
1. Attach `bin/main` and `bin/checksum` to the latest release
1. https://github.com/Purpose-Green/deploy/releases
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.1](https://github.com/Chemaclass/bash-skeleton/compare/main...0.1) - 2024-09-15
## Unreleased

- Add semantic versioning

## [0.1](https://github.com/Purpose-Green/deploy/compare/main...0.1) - 2024-09-17

Initial release. Check README for instructions about installation and how to use it.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This script automates deployment using your local Git. It:

## How to use it?

```bash
```txt
./deploy --help
Usage: deploy [arguments] [options]

Expand All @@ -27,6 +27,7 @@ Options:
--debug Enable debug mode (set -x).
--dry-run Simulate the deployment process without making any changes.
--force Ignore that your current local branch has ahead commits.
-v|-version Display current version.
-s, --source Specify the source branch.
-t, --target Specify the target branch.
-d, --development Specify the development branch.
Expand Down
70 changes: 69 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,78 @@ function generate_checksum() {
echo "$checksum"
}

function get_current_version() {
local file_with_version=$1
grep "declare -r $VERSION_VAR_NAME=" "$file_with_version" \
| sed "s/declare -r $VERSION_VAR_NAME=\"\([0-9]*\.[0-9]*\.[0-9]*\)\"/\1/"
}

function update_version() {
local version_type=$1
local file_with_version=$2

local current_version
current_version=$(grep "declare -r $VERSION_VAR_NAME=" "$file_with_version" \
| sed "s/declare -r $VERSION_VAR_NAME=\"\([0-9]*\.[0-9]*\.[0-9]*\)\"/\1/")

IFS='.' read -r major minor patch <<< "$current_version"

# Increment the appropriate version part
case "$version_type" in
major)
major=$((major + 1))
;;
minor)
minor=$((minor + 1))
;;
patch)
patch=$((patch + 1))
;;
*)
echo "Invalid version increment option: $version_type. Use major, minor, or patch."
exit 1
;;
esac

local new_version="$major.$minor.$patch"
local search_pattern="declare -r $VERSION_VAR_NAME=\"[0-9]*\.[0-9]*\.[0-9]*\""
local replace_pattern="declare -r $VERSION_VAR_NAME=\"$new_version\""

sed -i.bak \
"s/$search_pattern/$replace_pattern/" \
"$file_with_version"

rm "${file_with_version}.bak"

echo "$new_version"
}

########################
# MAIN #
########################
OUT_DIR=${1:-"bin"}
VERSION_VAR_NAME="DEPLOY_VERSION"
OUT_DIR="bin"
NEW_VERSION_TYPE=""

# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--new)
shift
NEW_VERSION_TYPE="$1"
;;
*)
OUT_DIR="$1"
;;
esac
shift
done

if [[ "$NEW_VERSION_TYPE" != "" ]]; then
echo "Updated version: $(update_version "$NEW_VERSION_TYPE" "deploy")"
else
echo "Current version: $(get_current_version "deploy")"
fi

mkdir -p "$OUT_DIR"
build "$OUT_DIR/deploy"
4 changes: 4 additions & 0 deletions deploy
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ while [[ $# -gt 0 ]]; do
console_header::print_help
exit 0
;;
-v|--version)
console_header::print_version
exit 0
;;
-s|--source)
SOURCE_BRANCH="$2"
shift
Expand Down
5 changes: 5 additions & 0 deletions src/console_header.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/bin/bash
set -o allexport

function console_header::print_version() {
printf "%s\n" "$(cat "$(dirname "${BASH_SOURCE[0]}")"/version.txt)"
}

function console_header::print_help() {
cat <<EOL
Usage: deploy [arguments] [options]
Expand All @@ -14,6 +18,7 @@ Options:
--debug Enable debug mode (set -x).
--dry-run Simulate the deployment process without making any changes.
--force Ignore that your current local branch has ahead commits.
-v|--version Display current version.
-s, --source Specify the source branch.
-t, --target Specify the target branch.
-d, --development Specify the development branch.
Expand Down
1 change: 0 additions & 1 deletion src/main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ function main::update_development() {

main::force_checkout "$development"
main::merge_source_to_target "remotes/origin/$target" "$development"

}

function main::merge_source_to_target() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Options:
--debug Enable debug mode (set -x).
--dry-run Simulate the deployment process without making any changes.
--force Ignore that your current local branch has ahead commits.
-v|--version Display current version.
-s, --source Specify the source branch.
-t, --target Specify the target branch.
-d, --development Specify the development branch.
Expand Down