From 4008949fc405a9fa9e325c19cbe3ed9277db56d1 Mon Sep 17 00:00:00 2001 From: Matithieu Date: Tue, 2 Sep 2025 20:58:24 +0200 Subject: [PATCH 1/4] feat: automatic tagging system --- .github/workflows/action.yml | 51 +++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 665f79d..e6de739 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -21,13 +21,58 @@ jobs: - name: Checkout source code uses: actions/checkout@v3 + - name: Get the last Git tag + id: get_last_tag + run: | + git fetch --tags --force + LAST_TAG=$(git tag --sort=-creatordate | head -n 1) + if [ -z "$LAST_TAG" ]; then + LAST_TAG="0.0.0" + fi + echo "LAST_TAG=${LAST_TAG}" >> $GITHUB_ENV + + - name: Calculate new version + id: calculate_version + run: | + LAST_TAG=${{ env.LAST_TAG }} + IFS='.' read -r MAJOR MINOR PATCH <<< "$LAST_TAG" + + if [[ "${{ github.event_name }}" == "pull_request" ]]; then + PATCH=$((PATCH + 1)) + else + MINOR=$((MINOR + 1)) + PATCH=0 + fi + + NEW_VERSION="${MAJOR}.${MINOR}.${PATCH}" + + if [[ "${{ github.event.inputs.MANUAL_TAG }}" != "" ]]; then + NEW_VERSION="${{ github.event.inputs.MANUAL_TAG}}" + fi + echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_ENV + - name: Build and push API image run: | - DOCKER_TAG=$(date +%Y%m%d%H%M%S) - IMAGE="${NEXUS_DOCKER_URL}/${DOCKER_API_NAME}:${DOCKER_TAG}" + IMAGE="${NEXUS_DOCKER_URL}/${DOCKER_API_NAME}:${NEW_VERSION}" docker build -f Dockerfile -t $IMAGE . echo "${NEXUS_PASSWORD}" | docker login "${NEXUS_DOCKER_URL}" -u "${NEXUS_USERNAME}" --password-stdin docker push $IMAGE - docker logout "${NEXUS_DOCKER_URL}" \ No newline at end of file + docker logout "${NEXUS_DOCKER_URL}" + + - name: Create and push Git tag + if: github.event_name == 'pull_request' + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + git config user.name "GitHub Actions" + git config user.email "actions@github.com" + git tag -a "${NEW_VERSION}" -m "Release ${NEW_VERSION}" + git push origin "${NEW_VERSION}" --no-verify + + - name: Remove SSH keys + run: rm -rf ~/.ssh + + - name: Output new version + run: echo "New version is ${{ env.NEW_VERSION }}" From bc3a46790bdcfcf47dcb34326acac532506ea8ae Mon Sep 17 00:00:00 2001 From: Matithieu Date: Tue, 2 Sep 2025 21:01:57 +0200 Subject: [PATCH 2/4] fix: add DEPLOY_KEY secret --- .github/workflows/action.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index e6de739..8b5424c 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -61,6 +61,13 @@ jobs: docker push $IMAGE docker logout "${NEXUS_DOCKER_URL}" + - name: Set up SSH for CI + run: | + mkdir -p ~/.ssh + echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + ssh-keyscan github.com >> ~/.ssh/known_hosts + - name: Create and push Git tag if: github.event_name == 'pull_request' env: From 15dd351597155c397ff1fc4471c18aad0b0565c0 Mon Sep 17 00:00:00 2001 From: Matithieu Date: Tue, 2 Sep 2025 21:14:00 +0200 Subject: [PATCH 3/4] fix: permissions and manual tag --- .github/workflows/action.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 8b5424c..5396222 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -1,6 +1,12 @@ -name: Build Java App +name: Build and Push Docker Image on: + workflow_dispatch: + inputs: + MANUAL_TAG: + description: 'Optional manual tag to override versioning' + required: false + default: '' push: branches: - develop @@ -8,6 +14,9 @@ on: branches: - develop +permissions: + contents: write + jobs: build: runs-on: ubuntu-latest From b803edfe546e0a728c85f2df0c7bfcea1d2706f9 Mon Sep 17 00:00:00 2001 From: Matithieu Date: Tue, 2 Sep 2025 21:16:01 +0200 Subject: [PATCH 4/4] fix: revert condition in order to merge --- .github/workflows/action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/action.yml b/.github/workflows/action.yml index 5396222..076e86c 100644 --- a/.github/workflows/action.yml +++ b/.github/workflows/action.yml @@ -78,7 +78,7 @@ jobs: ssh-keyscan github.com >> ~/.ssh/known_hosts - name: Create and push Git tag - if: github.event_name == 'pull_request' + if: github.event_name != 'pull_request' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: |