git 以 html 格式描述(在 gitlab-ci 内)

git describe in html format (inside gitlab-ci)

使用 Gitlab API,在 CI 期间,我使用 curl 命令创建了一个版本:

# 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

发布的描述应该是自上一个标签以来的所有提交。这是通过以下命令完成的:

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

这种方法的问题是这个命令每次提交生成一行(这导致描述中只有一行)。要在发布描述中添加多行,我需要这样的东西:

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

这样在gitlab中就可以正确显示了

所以我在考虑在 CI 中使用 shell 脚本(这可行吗?)来生成一个变量,所有提交都与 'or' 标签分开

有点棘手,我不知道该怎么做。 也许还有其他好的解决方案...

完整的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"

编辑解决方案:

在gitlab中,如果你想要最后一个标签和之前的标签之间的区别,在一行中,用'br',这里是命令:

--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'`\"}"

标签发布中的输出是这样的:

也许您可以通过过滤 git 输出来插入 <br> 标签:

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