2

I'd like to publish a web page to Confluence(Cloud) using Jenkins pipeline. I used Jenkins Confluence Plugin, it didn't work. When I set my confluence page url(https://yourDomain.atlassian.net/wiki/) and username and password in the global configuration, it keep saying incorrect password and username util it reached the maximum attempt times. I could not login after that unless I contact the admin user who I don't know.

Yang Wei
  • 41
  • 1
  • 6

1 Answers1

2

I solved this problem by using Confluence REST API instead.

Please see the REST API Example as follows: [a link]https://developer.atlassian.com/cloud/confluence/rest-api-examples/

Set global credentials: Your username and password as a username and password type credential, and the page id as a secrete text credential. My pipeline for update a Confluence page is:

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
                '''
            }
        }
    }
}

To Create a Confluence page:

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
                '''
            }
        }
    }
}
Yang Wei
  • 41
  • 1
  • 6