Confluence automation

Here’s a script that i use to automate updating an Confluence wiki content. Posting it here in case is usefull for someone else. I developed it on Windows7 and with ruby 1.9.3.

Can be used as a standalone, like so:

# for content:
$ ruby confluence.rb post "{html}<h1>Hello Confluence</h1>{html}" "page" "space" "username" "password" ""

# for attachments:
$ ruby confluence.rb attach file.pdf "application/pdf" "page" "space" "username" "password" ""

Or as a ruby lib, like so:

require 'confluence'

# for content: "{html}<h1>Hello Confluence</h1>{html}", "page", "space", "user", "pass", ""

# for attachments:
Confluence.attach "report.pdf", "application/pdf", "page", "space", "user", "pass", ""


module Confluence
  require 'xmlrpc/client'
  extend self

  # "{html}<h1>Hello Confluence</h1>{html}", "page", "space", "user", "pass", ""
  def post content, page, space, user, pass, server
    confluence = XMLRPC::Client.new2("https://#{user}:#{pass}@#{server}/rpc/xmlrpc")
    # disable certificate check    
    confluence.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE)
    # shortcut
    confluence = confluence.proxy("confluence2") # confluence1 with older

    pa = confluence.getPage("", space, page)
    pa["content"] = content
    confluence.storePage("", pa)

  # Confluence.attach "report.pdf", "application/pdf", "page", "space", "user", "pass", ""
  def attach file, type, page, space, user, pass, server
    server = XMLRPC::Client.new2("https://#{user}:#{pass}@#{server}/rpc/xmlrpc")
    # disable certificate check    
    server.instance_variable_get(:@http).instance_variable_set(:@verify_mode, OpenSSL::SSL::VERIFY_NONE)
    # shortcut
    confluence = server.proxy("confluence2") # confluence1 with older

    token = confluence.login(user, pass)
    pa = confluence.getPage(token, space, page)

    attachment = {}
    attachment['fileName'] = file
    attachment['contentType'] = type

    # Read updated local copy of the document
    data = ""   # data array does not work, init as string instead
    f =, "rb")  # don't forget the 'b' for binary {|byte| data << byte }

    confluence.addAttachment(token, pa['id'], attachment,

if __FILE__ == $0
  if ARGV[0] == "post"
    if ARGV.size != 7
      puts '$ ruby confluence.rb post "{html}<h1>Hello Confluence</h1>{html}" "page" "space" "username" "password" ""'
    else ARGV[1], ARGV[2], ARGV[3], ARGV[4], ARGV[5], ARGV[6]

  if ARGV[0] == "attach"
    if ARGV.size != 8
      puts '$ ruby confluence.rb attach file.pdf "application/pdf" "page" "space" "username" "password" ""'
      Confluence.attach ARGV[1], ARGV[2], ARGV[3], ARGV[4], ARGV[5], ARGV[6], ARGV[7]

It might change/evolve over time, check it out in my github ruby repo.

comments powered by Disqus