Commit 8ca3c84c authored by Thibaud-Vdb's avatar Thibaud-Vdb
Browse files

feat(job): create first version of job_release

parent 3a643647
Loading
Loading
Loading
Loading
+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] - 2022-10-14
* Initial version
+63 −0
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 

## 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 ! 😀

## 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 :

```
jobs 
   |
   job_name 
          |
          CHANGELOG.md
          job_name.yml
          README.md
   |
   job_name 
          |
          CHANGELOG.md
          job_name.yml
          README.md
```


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` :

```
# Changelog
All notable changes to this job will be documented in this file.

## [0.1.0] - 2022-10-14
* Initial version
```

## Variables

| Name | Description | Default |
| ---- | ----------- | ------- |
| `JOBS_DIRECTORY` <img width=100/> | The directory containing the jobs folders | `jobs` <img width=100/>|
| `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` |


## 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)
 No newline at end of file
+50 −0
Original line number Diff line number Diff line
# Job from R2Devops hub --> r2devops.io

stages:
  - deploy
 
job_release:
  stage: deploy
  image:
    name: alpine/httpie:${IMAGE_TAG}
    entrypoint: [""]
  variables:
    JOBS_DIRECTORY: "jobs"
    IMAGE_TAG: "3.2.1"
    GITLAB_API_URL: "gitlab.com"
  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})
      # 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
        #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)
        #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)
        result=$(http --ignore-stdin POST https://${GITLAB_API_URL}/api/v4/projects/$PROJECT_ENCODED/releases \
          "JOB-TOKEN: ${CI_JOB_TOKEN}" \
          tag_name=${JOB}-${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
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'