From 7880043ae89ecbd259be1cde81ce09b92574d707 Mon Sep 17 00:00:00 2001 From: Sven Ketelsen Date: Tue, 6 Jul 2021 20:40:44 +0200 Subject: [PATCH] spike: creating branches for projects on gitlab from ansible --- .gitignore | 3 ++- ansible-builder/bindep.txt | 1 - create-branch.yml | 24 ++++++++++++++++++++ templates/create-branch-maven.j2 | 39 ++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 create-branch.yml create mode 100755 templates/create-branch-maven.j2 diff --git a/.gitignore b/.gitignore index 7c95d47..a9b8bb4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .project +.idea image.tar.gz -.idea +checkout \ No newline at end of file diff --git a/ansible-builder/bindep.txt b/ansible-builder/bindep.txt index bb24848..db1a0d7 100644 --- a/ansible-builder/bindep.txt +++ b/ansible-builder/bindep.txt @@ -2,4 +2,3 @@ python38-devel [platform:rpm compile] subversion [platform:rpm] subversion [platform:dpkg] git-lfs [platform:rpm] - diff --git a/create-branch.yml b/create-branch.yml new file mode 100644 index 0000000..c2170bc --- /dev/null +++ b/create-branch.yml @@ -0,0 +1,24 @@ +- hosts: localhost + connection: local + gather_facts: false + vars: + repository: "git@git.dev-at.de:smardigo/maven/smardigo-maven-versions.git" + branch_type: "spike" + branch_ticket: "TST-0001" + + pre_tasks: + - name: "Checkout git repository" + ansible.builtin.git: + repo: "{{ repository }}" + dest: "./checkout" + version: "master" + + - name: "Insert/Update branch configuration" + template: + src: 'create-branch-maven.j2' + dest: './checkout/.ci_create-branch-maven' + + - name: "Branching project" + shell: ". .ci_create-branch-maven" + args: + chdir: "./checkout" diff --git a/templates/create-branch-maven.j2 b/templates/create-branch-maven.j2 new file mode 100755 index 0000000..11ae8f7 --- /dev/null +++ b/templates/create-branch-maven.j2 @@ -0,0 +1,39 @@ +#!/bin/bash + +if [ -n "$(git diff-index HEAD)" ]; +then + echo "branch creation isn't allowed with working tree changes" +else + BRANCH_CURRENT=$(git rev-parse --abbrev-ref HEAD) + git fetch -q + COMMIT_ID_BRANCH_CURRENT=$(git rev-parse $BRANCH_CURRENT) + COMMIT_ID_REMOTE_BRANCH_CURRENT=$(git rev-parse origin/$BRANCH_CURRENT) + if [ "$COMMIT_ID_BRANCH_CURRENT" != "$COMMIT_ID_REMOTE_BRANCH_CURRENT" ]; + then + echo "branch creation isn't allowed with changes between $BRANCH_CURRENT and origin/$BRANCH_CURRENT" + else + TYPE={{ branch_type }} + TICKET={{ branch_ticket }} + + BRANCH_NEXT=$TYPE/$TICKET + + VERSION_LAST=$(./mvnw help:evaluate -Dexpression=project.version -q -DforceStdout) + + VERSION=$(echo $VERSION_LAST | cut -d '-' -f 1) + VERSION_MAJOR=$(echo $VERSION_LAST | cut -d'.' -f 1) + VERSION_MINOR=$(echo $VERSION_LAST | cut -d'.' -f 2) + VERSION_BUGFIX=$(echo $VERSION_LAST | cut -d'.' -f 3) + + VERSION_NEXT="${VERSION}-${TICKET}-1-SNAPSHOT" + + git checkout -b $BRANCH_NEXT + ./mvnw versions:set -DnewVersion=$VERSION_NEXT -DgenerateBackupPoms=false + git commit -n -am "created branch ${BRANCH_NEXT}" + git checkout ${BRANCH_CURRENT} + git merge -s ours -m "merge branch ${BRANCH_NEXT} into ${BRANCH_CURRENT} [skip ci]" $BRANCH_NEXT + git checkout $BRANCH_NEXT + + git push -u origin $BRANCH_NEXT + git push origin $BRANCH_CURRENT + fi +fi