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

时间:2019-02-22 00:30:37

标签: git shell gitlab-ci gitlab-api git-describe

在CI期间,使用Gitlab API,我使用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'`\"}"

标记版本中的输出如下:

enter image description here

1 个答案:

答案 0 :(得分:2)

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

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