gitshellgitlab-cigitlab-apigit-describe

git describe in html format (inside gitlab-ci)


Using Gitlab API, during the CI, I create a release with a curl command:

# create a release
    - >- 
        curl --request POST 
        -H "PRIVATE-TOKEN: ${GITLABAPI_TOKEN}" 
        -H 'Content-Type: application/json' 
        --data "{\"description\": \"`git log $(git describe --tags --abbrev=0)..HEAD --oneline`\"}" 
        https://gitlab.unc.nc/api/v4/projects/${APP_GITLAB_NUMBER}/repository/tags/${CI_COMMIT_TAG}/release

The description of the release is supposed to be all the commit since the last tag. This is done with this command:

git log $(git describe --tags --abbrev=0)..HEAD --oneline

The problems with this approach is that this command generates one line per commit (which result in only one line in the description). To put multilines in the release description I need something like this:

--data "{'description': 'First commit<br>Second commit'}"

This way it is displayed correctly in gitlab.

So I was thinking using a shell script inside the CI (is this doable?) to generate a variable with all the commit separate with 'or' tag

It's a little bit tricky, and I don't know how to do it. Maybe there is another good solution...

Full gitlab-ci.yml:

image: maven:3.6.0-jdk-10

variables:
  APP_NAME: demo
  APP_GITLAB_NUMBER: 7
  MAVEN_OPTS: -Dmaven.repo.local=/cache/maven.repository

stages:
- display_all_variables # useful for debug only 
- deploy_dev
- deploy_prod

deploy_dev:
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - '[[ -f /.dockerenv ]] && mkdir -p ~/.ssh && echo "$KNOWN_HOST" > ~/.ssh/known_hosts'
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  stage: deploy_dev
  environment:
    name: dev
    URL: http://devsb01:9999
  only:
  - master
  except:
  - tags
  script:
  - ssh root@devsb01 "MY_MESSAGE='Hello World'"
  - ssh root@devsb01 'echo $MY_MESSAGE'
  - mvn package -P build
  - mv target/*.jar target/$APP_NAME.jar
  - ssh root@devsb01 "service $APP_NAME stop"
  - scp target/$APP_NAME.jar root@devsb01:/var/apps/$APP_NAME/
  - ssh root@devsb01 "service $APP_NAME start"

deploy_prod:
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - '[[ -f /.dockerenv ]] && mkdir -p ~/.ssh && echo "$KNOWN_HOST" > ~/.ssh/known_hosts'
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  stage: deploy_prod
  environment:
    name: production
  only:
  - tags
  except:
  - branches
  script:
  # build
    - mvn versions:set -DnewVersion=$CI_COMMIT_REF_NAME
    - mvn package -P build
    - mv target/*.jar target/$APP_NAME.jar
    - mvn versions:set -DnewVersion=$CI_COMMIT_REF_NAME
  # incrément de la version mineur du pom
    - mvn build-helper:parse-version versions:set -DnewVersion=\${parsedVersion.majorVersion}.\${parsedVersion.nextMinorVersion} versions:commit
  # commit&push
    - git config --global user.name "gitlab-ci"
    - git config --global user.email "gitlab-ci@unc.nc"
    - git --version
    - git status
    - git add pom.xml
    - git commit -m "increment pom version [ci skip]"
    - git push http://gitlab-ci:${GITLABCI_PWD}@gitlab.unc.nc/dsi-infogestion/demo.git HEAD:master
    - git status
  # création de la release
    - >- 
        curl --request POST 
        -H "PRIVATE-TOKEN: ${GITLABAPI_TOKEN}" 
        -H 'Content-Type: application/json' 
        --data "{\"description\": \"`git log $(git describe --tags --abbrev=0)..HEAD --oneline`\"}" 
        https://gitlab.unc.nc/api/v4/projects/${APP_GITLAB_NUMBER}/repository/tags/${CI_COMMIT_TAG}/release
  # on pousser le jar sur le serveur de prod    
    - ssh root@prodsb01 "service $APP_NAME stop"
    - scp target/$APP_NAME.jar root@prodsb01:/var/apps/$APP_NAME/
    - ssh root@prodsb01 "service $APP_NAME start"

display_all_variables:
  before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - '[[ -f /.dockerenv ]] && mkdir -p ~/.ssh && echo "$KNOWN_HOST" > ~/.ssh/known_hosts'
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
  stage: display_all_variables
  script:
  - ssh root@devsb01 "echo $GITLABAPI_TOKEN"
  - ssh root@devsb01 "echo $@ARTIFACT_DOWNLOAD_ATTEMPTS"
  - ssh root@devsb01 "echo $CHAT_INPUT"
  - ssh root@devsb01 "echo $CHAT_CHANNEL"
  - ssh root@devsb01 "echo $CI_COMMIT_BEFORE_SHA"
  - ssh root@devsb01 "echo $CI_COMMIT_DESCRIPTION"
  - ssh root@devsb01 "echo $CI_COMMIT_MESSAGE"
  - ssh root@devsb01 "echo $CI_COMMIT_REF_NAME"
  - ssh root@devsb01 "echo $CI_COMMIT_REF_SLUG"
  - ssh root@devsb01 "echo $CI_COMMIT_SHA"
  - ssh root@devsb01 "echo $CI_COMMIT_SHORT_SHA"
  - ssh root@devsb01 "echo $CI_COMMIT_TAG"
  - ssh root@devsb01 "echo $CI_COMMIT_TITLE"
  - ssh root@devsb01 "echo $CI_CONFIG_PATH"
  - ssh root@devsb01 "echo $CI_DEBUG_TRACE"
  - ssh root@devsb01 "echo $CI_DEPLOY_PASSWORD"
  - ssh root@devsb01 "echo $CI_DEPLOY_USER"
  - ssh root@devsb01 "echo $CI_DISPOSABLE_ENVIRONMENT"
  - ssh root@devsb01 "echo $CI_ENVIRONMENT_NAME"
  - ssh root@devsb01 "echo $CI_ENVIRONMENT_SLUG"
  - ssh root@devsb01 "echo $CI_ENVIRONMENT_URL"
  - ssh root@devsb01 "echo $CI_JOB_ID"
  - ssh root@devsb01 "echo $CI_JOB_MANUAL"
  - ssh root@devsb01 "echo $CI_JOB_NAME"
  - ssh root@devsb01 "echo $CI_JOB_STAGE"
  - ssh root@devsb01 "echo $CI_JOB_TOKEN"
  - ssh root@devsb01 "echo $CI_JOB_URL"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_ID"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_IID"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_PROJECT_ID"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_PROJECT_PATH"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_PROJECT_URL"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_REF_PATH"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_SOURCE_BRANCH_NAME"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_SOURCE_PROJECT_ID"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_SOURCE_PROJECT_PATH"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_SOURCE_PROJECT_URL"
  - ssh root@devsb01 "echo $CI_MERGE_REQUEST_TARGET_BRANCH_NAME"
  - ssh root@devsb01 "echo $CI_NODE_INDEX"
  - ssh root@devsb01 "echo $CI_NODE_TOTAL"
  - ssh root@devsb01 "echo $CI_API_V4_URL"
  - ssh root@devsb01 "echo $CI_PIPELINE_ID"
  - ssh root@devsb01 "echo $CI_PIPELINE_IID"
  - ssh root@devsb01 "echo $CI_PIPELINE_SOURCE"
  - ssh root@devsb01 "echo $CI_PIPELINE_TRIGGERED"
  - ssh root@devsb01 "echo $CI_PIPELINE_URL"
  - ssh root@devsb01 "echo $CI_PROJECT_DIR"
  - ssh root@devsb01 "echo $CI_PROJECT_ID"
  - ssh root@devsb01 "echo $CI_PROJECT_NAME"
  - ssh root@devsb01 "echo $CI_PROJECT_NAMESPACE"
  - ssh root@devsb01 "echo $CI_PROJECT_PATH"
  - ssh root@devsb01 "echo $CI_PROJECT_PATH_SLUG"
  - ssh root@devsb01 "echo $CI_PROJECT_URL"
  - ssh root@devsb01 "echo $CI_PROJECT_VISIBILITY"
  - ssh root@devsb01 "echo $CI_REGISTRY"
  - ssh root@devsb01 "echo $CI_REGISTRY_IMAGE"
  - ssh root@devsb01 "echo $CI_REGISTRY_PASSWORD"
  - ssh root@devsb01 "echo $CI_REGISTRY_USER"
  - ssh root@devsb01 "echo $CI_REPOSITORY_URL"
  - ssh root@devsb01 "echo $CI_RUNNER_DESCRIPTION"
  - ssh root@devsb01 "echo $CI_RUNNER_EXECUTABLE_ARCH"
  - ssh root@devsb01 "echo $CI_RUNNER_ID"
  - ssh root@devsb01 "echo $CI_RUNNER_REVISION"
  - ssh root@devsb01 "echo $CI_RUNNER_TAGS"
  - ssh root@devsb01 "echo $CI_RUNNER_VERSION"
  - ssh root@devsb01 "echo $CI_SERVER"
  - ssh root@devsb01 "echo $CI_SERVER_NAME"
  - ssh root@devsb01 "echo $CI_SERVER_REVISION"
  - ssh root@devsb01 "echo $CI_SERVER_VERSION"
  - ssh root@devsb01 "echo $CI_SERVER_VERSION_MAJOR"
  - ssh root@devsb01 "echo $CI_SERVER_VERSION_MINOR"
  - ssh root@devsb01 "echo $CI_SERVER_VERSION_PATCH"
  - ssh root@devsb01 "echo $CI_SHARED_ENVIRONMENT"
  - ssh root@devsb01 "echo $GET_SOURCES_ATTEMPTS"
  - ssh root@devsb01 "echo $GITLAB_CI"
  - ssh root@devsb01 "echo $GITLAB_USER_EMAIL"
  - ssh root@devsb01 "echo $GITLAB_USER_ID"
  - ssh root@devsb01 "echo $GITLAB_USER_LOGIN"
  - ssh root@devsb01 "echo $GITLAB_USER_NAME"
  - ssh root@devsb01 "echo $GITLAB_FEATURES"
  - ssh root@devsb01 "echo $RESTORE_CACHE_ATTEMPTS"

Edit solution:

In gitlab, if you want the difference between the last tag and the tag before, on one line, with 'br', here is the command:

--data "{\"description\": \"`git log $(git tag --sort version:refname | tail -n 2 | head -n 1)..$(git tag --sort version:refname | tail -n 1) --oneline | sed '$!s/$/<br>/' | tr -d '\n'`\"}"

The output in the tag release is like so:

enter image description here


Solution

  • Perhaps you can insert the <br> tags by filtering git output:

    --data "{\"description\": \"`git log $(git describe \
        --tags --abbrev=0)..HEAD --oneline | sed '$!s/$/<br>/'`\"}"