feat: enhance stacklist handling

- add support for passing stacklist as cli parameters
- refactor iteration over stacklist entries
- improve comments for better code understanding
- update README.md

🤖
This commit is contained in:
Sebastian Mark 2023-12-15 22:34:34 +01:00
parent 813e203e35
commit 41d3d04fe7
2 changed files with 15 additions and 9 deletions

View file

@ -5,15 +5,15 @@ It aims to simplify continuous delivery (CD) processes and infrastructure manage
## How does it work?
* The script iterates through each line of the `stacklist` file, skipping comments
* The script iterates through each line of the `stacklist` file (skipping comments) or through all cli parameters
* It fetches the latest changes from the remote git repository and checks if there are new commits
* If there is a change it pulls the updates from the remote git repository, otherwise the entry is skipped
* Following this, it runs `docker compose up` which builds, (re)creates and starts the containers
The `stacklist` file can list either a directory containing a `docker-compose.yml` file or a precise `docker-compose` file.
(see `stacklist.example`)
The `stacklist` file can list either a directory containing a `docker-compose.yml` file or a precise `docker-compose` file (see `stacklist.example`).
Alternatively, the stacklist can also be passed as a list of parameters.
## Usage
1. Ensure that your 'stacklist' file is up-to-date
2. Run the script manually or create a crontab entry
* Pass list of directory or docker-compose files as parameters to `dc-ops` or
* Ensure that your `stacklist` file is up-to-date and run the script (or create a crontab entry)

14
dc-ops
View file

@ -11,12 +11,16 @@ msg() { echo "$*"; }
BASEDIR=$(dirname $0)
while read -r LINE; do
# use list from file or passed parameters
STACKLIST=$(cat $BASEDIR/stacklist)
[[ $# -gt 0 ]] && STACKLIST=$*
# iterate list
for LINE in $STACKLIST; do
grep -q "^[[:space:]]*#" <<<$LINE && continue # skip comments
# determine if passed a directory or a file
STACKDIR=$LINE
if [[ -f $LINE ]]; then
STACKDIR=$(dirname $LINE)
COMPOSEFILE=$LINE
@ -24,15 +28,17 @@ while read -r LINE; do
h1 "processing $STACKDIR"
# skip if directroy not found
cd $STACKDIR || continue
# fetch from repo and check for new commits
git fetch --quiet
if [[ $(git rev-parse HEAD) == $(git rev-parse "@{u}") ]]; then
msg "no changes - skipping"
continue
fi
# pull new commits and run docker compose
git pull
docker compose --file ${COMPOSEFILE:=docker-compose.yml} up --build --detach --remove-orphans
done <$BASEDIR/stacklist
done