如何在 Jenkins 中发布 Confluence(Cloud) 页面

如何在 Jenkins 中发布 Confluence(Cloud) 页面

我想使用 Jenkins 管道将网页发布到 Confluence(云)。我使用了 Jenkins Confluence 插件,但它不起作用。当我设置我的 Confluence 页面 URL(https://yourDomain.atlassian.net/wiki/) 以及全局配置中的用户名和密码,它一直提示密码和用户名不正确,直到达到最大尝试次数。此后我无法登录,除非我联系我不认识的管理员用户。

答案1

我通过使用 Confluence REST API 解决了这个问题。

请参阅以下 REST API 示例:[链接]https://developer.atlassian.com/cloud/confluence/rest-api-examples/

设置全局凭证:您的用户名和密码作为用户名和密码类型凭证,页面 ID 作为秘密文本凭证。我更新 Confluence 页面的管道是:

pipeline {
    agent any
    environment {
      CONFLUENCE_PAGE_CREDS = credentials('confluence-creds')
      PAGE_ID = credentials('confluence-page-id')
    }
    stages {
        stage('Update Confluence Page') {
            steps {
                sh '''
                    #!/bin/bash
                    curl -u ${CONFLUENCE_PAGE_CREDS} 'https://YOURDOMAIN.atlassian.net/wiki/rest/api/content/'${PAGE_ID}'?expand=version' | python -mjson.tool > version.txt
                    PAGE_VERSION=$(grep -Po '(?<="number": )[0-9]+' version.txt)
                    rm version.txt
                    PAGE_VERSION=$((PAGE_VERSION+1))
                    curl -u ${CONFLUENCE_PAGE_CREDS} 'https://YOURDOMAIN.atlassian.net/wiki/rest/api/content/'${PAGE_ID}'?expand=body.storage' | python -mjson.tool > body.txt
                    more body.txt
                    PAGE_BODY="$(grep -Po '(?<="value": ")[^"]+' body.txt)"
                    rm body.txt
                    TEXT='<p>The content to append</p>'
                    TEXT=$PAGE_BODY$TEXT
                    echo '{"id":"'${PAGE_ID}'","type":"page","title":"NEW PAGE","space":{"key":"TR"},"body":{"storage":{"value":"'$TEXT'","representation":"storage"}},"version":{"number":'$PAGE_VERSION'}}' > update.json
                    curl -u ${CONFLUENCE_PAGE_CREDS} -X PUT -H 'Content-Type: application/json' -d '@update.json' https://YOURDOMAIN.atlassian.net/wiki/rest/api/content/${PAGE_ID} | python -mjson.tool
                    rm update.json
                '''
            }
        }
    }
}

要创建 Confluence 页面:

pipeline {
    agent any
    environment {
      CONFLUENCE_PAGE_CREDS = credentials('confluence-creds')
      PAGE_ID = credentials('confluence-page-id')
    }
    stages {
        stage('Update Confluence Page') {
            steps {
                sh '''
                    #!/bin/bash
                    TEXT='<p>New page</p>'
                    echo '{"type":"page","title":"New page","ancestors":[{"id":"'${PAGE_ID}'"}],"space":{"key":"TR"},"body":{"storage":{"value":"'$TEXT'","representation":"storage"}}}' > update.json
                    curl -u ${CONFLUENCE_PAGE_CREDS} -X POST -H 'Content-Type: application/json' -d '@update.json' https://YOURDOMAIN.atlassian.net/wiki/rest/api/content/ | python -mjson.tool
                    rm update.json
                '''
            }
        }
    }
}

相关内容