Commit 7b7a393c authored by Thomas Boni's avatar Thomas Boni
Browse files

Merge branch '663-job_release-allow' into 'latest'

Resolve "[job_release] - allow nested directory for jobs"

Closes #663

See merge request r2devops/hub!419
parents d67cdb95 60e574e9
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
# Changelog
All notable changes to this job will be documented in this file.

## [1.0.0] - 2022-11-23
* Allow to fetch jobs from nested directories
* Use snippet maintained by R2Devops instead of external resource

## [0.1.0] - 2022-10-14
* Initial version
+27 −31
Original line number Diff line number Diff line
## Objective

Retrieve all versions of multiple jobs and create release for each entry inside their `CHANGELOG.md` files 
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

@@ -11,42 +11,38 @@ Retrieve all versions of multiple jobs and create release for each entry inside
   behavior, check the [variables section](#variables)
1. Well done, your job is ready to work and will be execute on your main branch ! 😀

## Behavior

By default, this job will be run on the `jobs` directory located at the root of your project and will create releases for each directory containing jobs files.

## Jobs files

Here's the structure of a job files :

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

A `CHANGELOG.md` is required to create the releases
- 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

A `CHANGELOG.md` is require for creating git tags release.
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` :

**Here's an example of a `CHANGELOG.md`:**
```
# Changelog
All notable changes to this job will be documented in this file.

## [0.1.0] - 2022-10-14
## [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
```

@@ -54,10 +50,10 @@ All notable changes to this job will be documented in this file.

| Name | Description | Default |
| ---- | ----------- | ------- |
| `JOBS_DIRECTORY` <img width=100/> | The directory containing the jobs folders | `jobs` <img width=100/>|
| `JOBS_DIRECTORY` | The root directory containing the job folders | `.` |
| `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 url used to call the GitLab API. ⚠️ It should be changed if you using a self-hosted version | `gitlab.com` |

| `GITLAB_API_URL` | The domain of GitLab instance. ⚠️ It should be changed if you using a self-hosted version | `gitlab.com` |
| `CHANGELOG_FILE` | The name of changelog files (case insensitive) | `CHANGELOG.md` |

## Author
This resource is an **[official job](https://docs.r2devops.io/faq-labels/)** added in [**R2Devops repository**](https://gitlab.com/r2devops/hub) by [@Totara-thib](https://gitlab.com/Totara-thib)
+37 −34
Original line number Diff line number Diff line
@@ -9,42 +9,45 @@ job_release:
    name: alpine/httpie:${IMAGE_TAG}
    entrypoint: [""]
  variables:
    JOBS_DIRECTORY: "jobs"
    JOBS_DIRECTORY: "."
    IMAGE_TAG: "3.2.1"
    GITLAB_API_URL: "gitlab.com"
    CHANGELOG_FILE: "CHANGELOG.md"
  before_script:
    - apk update && apk add --no-cache bash
    - pip install --ignore-installed distlib pipenv
    - pipenv install
    
  script: >
    PROJECT_ENCODED=$(/bin/bash -c "$(http --ignore-stdin --body https://gist.githubusercontent.com/cdown/1163649/raw/8a35c36fdd24b373788a7057ed483a5bcd8cd43e/gistfile1.sh) && _encode '$CI_PROJECT_PATH'");

    for JOB in ${JOBS_DIRECTORY}/*; do
      JOB=$(basename ${JOB})
  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
    - JOBS="$(find $JOBS_DIRECTORY -iname $CHANGELOG_FILE)"
    - for JOB in $JOBS; do
        # Extract the job name (this is the folder name)
    -   JOB_NAME=$(basename $(dirname $JOB))
        # 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' ${JOBS_DIRECTORY}/${JOB}/CHANGELOG.md)
      for VERSION in ${VERSIONS}; do
    -   VERSIONS=$(sed -rn 's/^##\s*\[\s*([^\ ]*)\s*\]\s*-\s*[0-9]{4}-[0-9]{2}-[0-9]{2}\s*/\1/p' ${JOB})
    -   for VERSION in ${VERSIONS}; do
          #Retrieve the changes between two versions
        CHANGELOG=$(sed -n "/^##\s*\[\s*${VERSION}\s*\]/,/^##/p" ${JOBS_DIRECTORY}/${JOB}/CHANGELOG.md | sed -e '/^$/d' | head -n -1 | tail -n +2)
    -     CHANGELOG=$(sed -n "/^##\s*\[\s*${VERSION}\s*\]/,/^##/p" $JOB | 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" ${JOBS_DIRECTORY}/${JOB}/CHANGELOG.md | sed -e '/^$/d' | tail -n +2)
    - |
          [ -z "${CHANGELOG}" ] && CHANGELOG=$(sed -n "/^##\s*\[\s*$VERSION\s*\]/,/^[^##]/p" $JOB | sed -e '/^$/d' | tail -n +2)
          result=$(http --ignore-stdin POST https://${GITLAB_API_URL}/api/v4/projects/$PROJECT_ENCODED/releases \
            "JOB-TOKEN: ${CI_JOB_TOKEN}" \
          tag_name=${JOB}-${VERSION} \
            tag_name=${JOB_NAME}-${VERSION} \
            ref=${CI_COMMIT_SHA} \
            "description=${CHANGELOG}")
          if [ $(echo $result | grep "Release already exists\|${JOB}-${VERSION}" | wc -l) -eq 0 ]; then
            echo "[ERROR] Problem when attempting to create release ${JOB}-${VERSION}"
            echo "[ERROR] ${result}"
            exit 1;
          else
            if [ $(echo ${result} | grep "Release already exists" | wc -l) -eq 0 ]; then
              echo "New version detected for $JOB-$VERSION"
            fi
            echo "Processed ${JOB}-${VERSION} : ${result}";
          fi
      done
    done
    -       if [ $(echo $result | grep "Release already exists\|${JOB_NAME}-${VERSION}" | wc -l) -eq 0 ]; then
    -         echo "[ERROR] Problem when attempting to create release ${JOB_NAME}-${VERSION}"
    -         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_NAME}-${VERSION} : ${result}"
    -       fi
    -   done
    - done
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'