Commit 7b977da1 authored by Sebastien LONGO's avatar Sebastien LONGO Committed by Thomas Boni
Browse files

feat(template_release): add semantic tagging option

parent 036af50f
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
# Changelog
All notable changes to this job will be documented in this file.

## [0.5.0] - 2024-04-24
* introduce semantic tagging as disabled by default (`SEMANTIC_TAGGING_ENABLED` variable)

## [0.4.0] - 2024-04-22
* Release only the latest version by default (`RELEASE_ONLY_LATEST_VERSION` variable)

+23 −1
Original line number Diff line number Diff line
@@ -75,6 +75,26 @@ A `CHANGELOG.md` file as explained in the previous example, is required to creat
* Initial version
```

## Semantic tagging feature

This job usually create when a new R2 template has been created or modified, a **gitlab release** for the detected R2 template.
This release has the following form :
```
<<R2template_name>>@<<MAJOR>>.<<MINOR>>.<<PATCH>>
```

When the semantic tagging is enabled, in addition, this job will add **git tag** in the form of :
```
<<R2template_name>>@<<MAJOR>>
```
and
```
<<R2template_name>>@<<MAJOR>>.<<MINOR>>
```

This will allow your users to include in their pipelines the latest MAJOR version of a job or the latest MAJOR.MINOR version of a job without to go back in the CI/CD editor each time a PATCH or MINOR version of a R2 template is published reducing the total cost of ownership of the R2DevOps solution


## Variables

| Name | Description | Default |
@@ -83,4 +103,6 @@ A `CHANGELOG.md` file as explained in the previous example, is required to creat
| `GITLAB_API_URL` | The domain of GitLab instance | `${CI_SERVER_HOST}` |
| `METADATA_FILE_EXTENSION` | The extension of metadata files | `.r2.yml` |
| `RELEASE_PATH` | The path where releases files are exposed as artifacts | `${CI_PROJECT_DIR}/releases` |
| `RELEASE_ONLY_LATEST_VERSION` | Release only the latest version from changelog files | `true`
| `RELEASE_ONLY_LATEST_VERSION` | Release only the latest version from changelog files | `true` |
| `SEMANTIC_TAGGING_ENABLED` | Semantic tagging add MAJOR and MAJOR.MINOR tagging of the repo | `false` |
| `PRIVATE_TOKEN` | A personal or repo token required only when `SEMANTIC_TAGGING_ENABLED` is set to true to be able to update git tags | ` ` |
+55 −15
Original line number Diff line number Diff line
@@ -11,8 +11,9 @@ template_release:
    METADATA_FILE_EXTENSION: ".r2.yml"
    RELEASE_PATH: "${CI_PROJECT_DIR}/releases"
    RELEASE_ONLY_LATEST_VERSION: "true"
    SEMANTIC_TAGGING_ENABLED: "false"
  before_script:
    - apk update && apk add --no-cache bash
    - apk update && apk add --no-cache bash jq
  script:
    # Url encode the project name
    - mkdir -p $RELEASE_PATH
@@ -81,6 +82,10 @@ template_release:
          [ -z "${CHANGELOG}" ] && CHANGELOG=$(sed -n "/^##\s*\[\s*$VERSION\s*\]/,/^[^##]/p" $CHANGELOG_PATH | sed -e '/^$/d' | tail -n +2)
          TEMPLATE_PATH=$(echo $METADATA_JOB | sed -e "s/${METADATA_FILE_EXTENSION=}//g" -e 's/\.\///g' )
          JOB_RELEASE="${TEMPLATE_PATH}@${VERSION}"
          if [ "${SEMANTIC_TAGGING_ENABLED}" = "true" ]; then
            release_exist=$(http --ignore-stdin GET https://${GITLAB_API_URL}/api/v4/projects/${PROJECT_ENCODED}/releases/${JOB_RELEASE} \
              "JOB-TOKEN:${CI_JOB_TOKEN}" )
          fi
          result=$(http --ignore-stdin POST https://${GITLAB_API_URL}/api/v4/projects/$PROJECT_ENCODED/releases \
            "JOB-TOKEN: ${CI_JOB_TOKEN}" \
            tag_name=${JOB_RELEASE} \
@@ -99,12 +104,47 @@ template_release:
    -   |
            echo "Processed ${JOB_RELEASE} : ${result}"
    -     fi

    # If we want to use semantic tagging:
    -     if [ "${SEMANTIC_TAGGING_ENABLED}" = "true" ]; then
    # Determine major and minor versions
    -       MAJOR_VERSION=`echo $VERSION | cut -d. -f1`
    -       MINOR_VERSION=`echo $VERSION | cut -d. -f2`
    -       PATCH_VERSION=`echo $VERSION | cut -d. -f3`
    -       |
            TEMPLATE_PATH=$(echo $METADATA_JOB | sed -e "s/${METADATA_FILE_EXTENSION=}//g" -e 's/\.\///g' )
            JOB_RELEASE="${TEMPLATE_PATH}@${VERSION}"
            JOB_TAGS_MAJOR="${TEMPLATE_PATH}@${MAJOR_VERSION}"
            JOB_TAGS_MINOR="${TEMPLATE_PATH}@${MAJOR_VERSION}.${MINOR_VERSION}"
    -       if [ $(echo $release_exist | grep -v "404 Not Found" | wc -l) -eq 0 ]; then
              # release previously existed so no need to tag 1 and 2 digits tags
    -         release_created=$(http --ignore-stdin GET https://${GITLAB_API_URL}/api/v4/projects/${PROJECT_ENCODED}/releases/${JOB_RELEASE} "JOB-TOKEN:${CI_JOB_TOKEN}" )
              # Get commit hash associated with created release
    -         |
              COMMIT_HASH=$(echo $release_created | jq -r '.commit|.id')
              result=$(http --ignore-stdin DELETE https://${GITLAB_API_URL}/api/v4/projects/${CI_PROJECT_ID}/repository/tags/${JOB_TAGS_MAJOR} \
                "PRIVATE-TOKEN:${PRIVATE_TOKEN}" )
              echo "Deleting git's 1-digit tag ${JOB_TAGS_MAJOR} : ${result}"
              result=$(http --ignore-stdin POST https://${GITLAB_API_URL}/api/v4/projects/${PROJECT_ENCODED}/repository/tags \
                "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" \
                tag_name==${JOB_TAGS_MAJOR} \
                ref==${COMMIT_HASH} )
              echo "Processed git's 1-digit tag ${JOB_TAGS_MINOR} : ${result}"
                result=$(http --ignore-stdin DELETE https://${GITLAB_API_URL}/api/v4/projects/${CI_PROJECT_ID}/repository/tags/${JOB_TAGS_MINOR} \
                "PRIVATE-TOKEN:${PRIVATE_TOKEN}" )
              echo "Deleting git's 2-digit tag ${JOB_TAGS_MINOR} : ${result}"
              result=$(http --ignore-stdin POST https://${GITLAB_API_URL}/api/v4/projects/${PROJECT_ENCODED}/repository/tags \
                "PRIVATE-TOKEN: ${PRIVATE_TOKEN}" \
                tag_name==${JOB_TAGS_MINOR} \
                ref==${COMMIT_HASH} )
              echo "Processed git's 2-digit tag ${JOB_TAGS_MINOR} : ${result}"
            else
              echo "Skipping tagging because ${JOB_RELEASE} is not the latest release"
            fi
    -     fi
          # If we want to release only latest version: break
    -     if [ "${RELEASE_ONLY_LATEST_VERSION}" = "true" ]; then
    -       break
    -     fi

    -   done
    - done
  rules: