From d88808616b1e6a6daa86ddf703860f2967ccfe61 Mon Sep 17 00:00:00 2001 From: HaneenT Date: Tue, 7 May 2024 14:13:30 -0400 Subject: [PATCH] KPMP-5200: add build actions --- .github/workflows/build-gradle-project.yml | 49 ++++++++++++++++++++++ build.gradle | 27 +++++++++++- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/build-gradle-project.yml diff --git a/.github/workflows/build-gradle-project.yml b/.github/workflows/build-gradle-project.yml new file mode 100644 index 0000000..e967576 --- /dev/null +++ b/.github/workflows/build-gradle-project.yml @@ -0,0 +1,49 @@ +name: Build user-auth docker image + +on: + push: + +jobs: + build-gradle-project: + env: + IMAGE_TAG: 2.2 + runs-on: ubuntu-latest + steps: + - name: Get branch names + id: branch-names + uses: tj-actions/branch-names@v8 + + - name: Get current branch name + if: steps.branch-names.outputs.is_default == 'false' + run: | + echo "Running on branch: ${{ steps.branch-names.outputs.current_branch }}" + - name: Checkout project sources + uses: actions/checkout@v3 + - uses: actions/setup-java@v3 + with: + java-version: '8' + distribution: 'temurin' + - name: Setup Gradle + uses: gradle/gradle-build-action@v2 + with: + gradle-version: 7.4 + + - name: Login to Docker Hub + uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a + with: + username: ${{ secrets.ENV_DOCKER_USER }} + password: ${{ secrets.ENV_DOCKER_PASS }} + + - name: Run build with Gradle Wrapper + run: | + ./gradlew build docker + + - name: Push to Docker Hub if branch is develop + if: steps.branch-names.outputs.current_branch == 'develop' + run: | + docker push "kingstonduo/user-auth:$IMAGE_TAG" + + - name: Push to Docker Hub if branch is not develop + if: steps.branch-names.outputs.current_branch != 'develop' + run: | + docker push "kingstonduo/user-auth:${{ steps.branch-names.outputs.current_branch }}" \ No newline at end of file diff --git a/build.gradle b/build.gradle index e4ad290..a3ff560 100755 --- a/build.gradle +++ b/build.gradle @@ -42,7 +42,7 @@ springBoot { jar { baseName='user-auth' - version='1.3' + version='2.2' } task unpack(type: Copy) { @@ -51,8 +51,31 @@ task unpack(type: Copy) { into("build/dependency") } +def getCurrentGitBranch() { + if (project.hasProperty('tag')) { + def tagValue = project.property('tag') + return tagValue + } else { + def gitBranch = "Unknown branch" + try { + def workingDir = new File("${project.projectDir}") + def result = 'git rev-parse --abbrev-ref HEAD'.execute(null, workingDir) + result.waitFor() + if (result.exitValue() == 0) { + gitBranch = result.text.trim() + } + } catch (e) { + } + if (gitBranch == "develop" || gitBranch == "master"){ + return jar.version + }else{ + return gitBranch + } + } +} + docker { - name "${project.group}/${jar.baseName}" + name "kingstonduo/${jar.baseName}:" + getCurrentGitBranch() copySpec.from(tasks.unpack.outputs).into("dependency") buildArgs(['DEPENDENCY': "dependency"]) }