Confluence is a popular tool for sharing information and documents among teams. While it’s great for storing things like project plans and meeting notes, keeping everything up to date can be tough, especially if you need to make lots of changes at once. That’s where BASH script comes in handy. BASH scripts are like little programs that can automatically update Confluence pages for you.
Understanding Confluence Integration
Confluence provides a robust REST API that allows developers to interact with Confluence pages programmatically. This API enables actions such as creating, updating, and deleting pages, as well as retrieving page content and metadata. By leveraging this API, BASH scripts can be used to automate various Confluence-related tasks, from simple content updates to more complex operations involving multiple pages or spaces.
Script for updating Confluence Page
#!/bin/bash
# Confluence server details
CONFLUENCE_URL=“Your confluence URL”
PAGE_URL=“Your confluence page URL”
USERNAME=“your-username”
API_TOKEN=“your-api-token”
#You can either use your user name and API Token or password. Token is better as we can restrict access using that
# Page id will be the page number provided in the url
PAGE_ID=$(echo “${PAGE_URL}“ | sed -n ‘s/.*pageId=\([0-9]*\).*/\1/p’)
echo “Page ID: ${PAGE_ID}“
# Since to update the content, you first need to get the existing content and then append the updated content in it
EXISTING_CONTENT=$(curl -s -X GET -H “Content-Type: application/json” -u “${USERNAME}:${API_TOKEN}” \ “${CONFLUENCE_URL}/rest/api/content/${PAGE_ID}?expand=body.storage” | jq -r ‘.body.storage.value’)
# New content to update the page with
NEW_CONTENT=“This is the updated content for the Confluence page.”
# Combine existing and new content
UPDATED_CONTENT=“${EXISTING_CONTENT}\n\n${NEW_CONTENT}“
curl -s -X PUT -H “Content-Type: application/json” -u “${USERNAME}:${API_TOKEN}“ -d “{\”type\”:\”page\”,\”title\”:\”Updated Page Title\”,\”body\”:{\”storage\”:{\”value\”:\”${UPDATED_CONTENT}\”,\”representation\”:\”storage\”}}}” “${CONFLUENCE_URL}/rest/api/content/${PAGE_ID}“
echo “Curl command completed”
Conclusion
In an era where collaboration and knowledge sharing are paramount, automation tools like BASH scripts offer a powerful way to streamline Confluence page management. By leveraging the Confluence REST API, developers can create scripts that automate updates, imports, and other tasks, freeing up valuable time for teams to focus on more meaningful work.
