Commit 7caf69e3 authored by GridexX's avatar GridexX
Browse files

feat: new job template_release



Signed-off-by: default avatarGridexX <arsene582@gmail.com>
parent 925d6f26
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
## Objective

!!! error "This job is deprecated 🚨"
    This job has been moved to [`template_release`](https://r2devops.io/_/r2devops-bot/template_release) and must be use instead.
    The job is no more maintained and is now deprecated. Despites it still exists to keep working on pipelines.

Retrieve all job templates from a repository and create GitLab release (and git tag) for each versions listed in `CHANGELOG.md`

## How to use it
+5 −0
Original line number Diff line number Diff line
# Changelog
All notable changes to this job will be documented in this file.

## [0.1.0] - 2023-02-03
* Initial version
+88 −0
Original line number Diff line number Diff line
## Objective

Retrieve all templates from a repository and create GitLab release (and git tag) for each versions listed in `CHANGELOG.md`

## How to use it

1. Copy/paste job URL in `include` list of your `.gitlab-ci.yml` (see the
   [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed
   version](#changelog) instead of `latest`.
1. The job can be run "out of the box". If you need to personalize its
   behavior, check the [variables section](#variables)
1. Well done, your job is ready to work and will be execute on your main branch ! 😀


## Jobs files

**Here is an example of a job metadata file:**

```yaml
files: # mandatory
    template: ./jobs/docker_build/docker_build.yml  # mandatory
    documentation: ./jobs/docker_build/README.md # optional
    changelog: ./jobs/docker_build/CHANGELOG.md # mandatory
data: # optional
    description: "A ready-to-use docker job to push the image of your project repository to the GitLab registry"
    public: false
    deprecated: false
    license: MIT
    icon: 🐳
    labels:
      - Lint
      - Security
      - Test

```
A job metadata file is required to create the releases. It should follow these rules:

- The files must be named with an extension defined by the variable `METADATA_FILE_EXTENSION`. By default, it is set to `.r2.yml`
- The entries in this file must have relative paths linking the job metadata file to the job template, documentation, and changelog files.
- The changelog path must be defined and must be a valid file.
- The template path must be defined and must be a valid file.

For more information about job templates, check the [documentation](https://docs.r2devops.io/get-started/import-job-template#how-to-import-job-templates).


**Here's the example structure of a job template folder:**
```
docker_build/
  ├── README.md
  ├── CHANGELOG.md
  └── docker_build.yml (containing a job named docker_build )
```

A `CHANGELOG.md` file as explained in the previous example, is required to create releases. It should follow these rules:

- The structure of the `CHANGELOG.md` is based on [keepachangelog](https://keepachangelog.com/en/1.0.0/) structure.
- You could also checkout the [R2Devops documentation](https://docs.r2devops.io/job-structure/#job-changelogs) about it

**Here's an example of a `CHANGELOG.md`:**
```
# Changelog

## [1.1.0] - 2021-06-21
* Allows context different from root with new variable `DOCKER_CONTEXT_PATH`

## [1.0.0] - 2021-05-07
* Breaking change in the configuration of custom registry, see documentation
* Add support to push in multiple registries
* Add support to authentication in multiple registries

## [0.3.0] - 2020-11-25
* New variable `DOCKER_USE_CACHE` to be able to cache layers of build
* New variable `DOCKER_OPTIONS` to be able to add additional options

## [0.1.0] - 2020-10-21
* Initial version
```

## Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| `IMAGE_TAG` | The default tag for the docker image [alpine/httpie](https://hub.docker.com/r/alpine/httpie) | `3.2.1` |
| `GITLAB_API_URL` | The domain of GitLab instance. ⚠️ It should be changed if you using a self-hosted version | `gitlab.com` |
| `METADATA_FILE_EXTENSION` | The extension of metadata files | `.r2.yml` |

## Author
This resource is an **[official job](https://docs.r2devops.io/faq-labels/)** added in [**R2Devops repository**](https://gitlab.com/r2devops/hub) by [@GridexX](https://gitlab.com/GridexX)
+103 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io

stages:
  - deploy

template_release:
  stage: deploy
  image:
    name: alpine/httpie:${IMAGE_TAG}
    entrypoint: [""]
  variables:
    IMAGE_TAG: "3.2.1"
    GITLAB_API_URL: "${CI_SERVER_HOST}"
    METADATA_FILE_EXTENSION: ".r2.yml"
  before_script:
    - apk update && apk add --no-cache bash
  script:
    # Url encode the project name
    - PROJECT_ENCODED=$(/bin/bash -c "$(http --ignore-stdin --body https://gitlab.com/r2devops/hub/-/snippets/2462394/raw/92c7e820e5b7ce468d8031748e1a57c24c67f6a4/_encode.sh) && _encode '$CI_PROJECT_PATH'")
    # Search for all changelog files
    - METADATA_JOBS="$(find . -iname "*$METADATA_FILE_EXTENSION")"
    - for METADATA_JOB in $METADATA_JOBS; do
    # Get back to the project root
    -   cd ${CI_PROJECT_DIR}
    # Find the changelog file entry (if any)
    # Get only the part after the colon
    # Remove leading and trailing spaces, optional quotes and optional comments
    -   if CHANGELOG_PATH=$(grep "changelog:" $METADATA_JOB | cut -d ":" -f 2 | sed -e 's/#.*$//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/"//g' -e "s/'//g" ); then
    -     echo "Changelog entry found for $METADATA_JOB with path $CHANGELOG_PATH"
    -  else
    -     echo "No changelog entry found for $METADATA_JOB"
    -     continue
    -   fi
    
    -   if JOB_PATH_WITH_EXTENSION=$(grep "template:" $METADATA_JOB | cut -d ":" -f 2 | sed -e 's/#.*$//' -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/"//g' -e "s/'//g" -e 's/^\.\///'); then
    -     echo "Template entry found for $METADATA_JOB with path $JOB_PATH_WITH_EXTENSION"
    -   else
    -     echo "No template entry found for $METADATA_JOB"
    -     continue
    -   fi
    
    -   JOB_PATH=$(echo $JOB_PATH_WITH_EXTENSION | sed -e 's/\.ya*ml$//')

    # Go to the directory of the metadata file
    -   cd $(dirname $METADATA_JOB)

    # Check if the defined changelog and job_template files exists
    -   if [ -z "$CHANGELOG_PATH" ]; then
    -     echo "Changelog entry empty for $METADATA_JOB with path $CHANGELOG_PATH"
          continue
    -   fi

    -   if [ -f "$CHANGELOG_PATH" ]; then
    -     echo "Changelog file found for $METADATA_JOB with path $CHANGELOG_PATH"
    -   else
    -     echo "Changelog file not found for $METADATA_JOB with path $CHANGELOG_PATH"
    -   fi

    -   if [ -z "$JOB_PATH_WITH_EXTENSION" ]; then
    -     echo "Template entry empty for $METADATA_JOB with path $JOB_PATH_WITH_EXTENSION"
    -     continue
    -   fi

    -   if [ -f "$JOB_PATH_WITH_EXTENSION" ]; then
    -     echo "Template file found for $METADATA_JOB with path $JOB_PATH_WITH_EXTENSION"
    -   else
    -     echo "Template file not found for $METADATA_JOB with path $JOB_PATH_WITH_EXTENSION"
    -     continue
    -   fi
        # Extract the job path
        # Extract the job name from the path
    -   JOB_NAME=$(basename $JOB_PATH)

      # Retrieve all versions of the job inside the CHANGELOG file, ## [(0.2.0)] - 2021-04-20 => get only this part ()
    -   VERSIONS=$(sed -rn 's/^##\s*\[\s*([^\ ]*)\s*\]\s*-\s*[0-9]{4}-[0-9]{2}-[0-9]{2}\s*/\1/p' ${CHANGELOG_PATH})
    -   for VERSION in ${VERSIONS}; do
          #Retrieve the changes between two versions
    -     CHANGELOG=$(sed -n "/^##\s*\[\s*${VERSION}\s*\]/,/^##/p" $CHANGELOG_PATH | sed -e '/^$/d' | head -n -1 | tail -n +2)
          #For initial version, we don't have a previous version, so set to default message
    -   |
          [ -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}"
          result=$(http --ignore-stdin POST https://${GITLAB_API_URL}/api/v4/projects/$PROJECT_ENCODED/releases \
            "JOB-TOKEN: ${CI_JOB_TOKEN}" \
            tag_name=${JOB_RELEASE} \
            ref=${CI_COMMIT_SHA} \
            "description=${CHANGELOG}")
    -         if [ $(echo $result | grep "Release already exists\|${JOB_RELEASE}" | wc -l) -eq 0 ]; then
    -           echo "[ERROR] Problem when attempting to create release ${JOB_RELEASE}"
    -           echo "[ERROR] ${result}"
    -           exit 1;
    -         else
    -           if [ $(echo ${result} | grep "Release already exists" | wc -l) -eq 0 ]; then
    -             echo "New version detected for $JOB_NAME-$VERSION"
    -           fi
    -   |
              echo "Processed ${JOB_RELEASE} : ${result}"
    -         fi
    -   done
    - done
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'