Quite some time ago I needed to upload a bunch of files as attachments to Jira issues. To avoid uploading them all manually via the web UI I wrote a Windows batch file to enable a batch upload. It makes use of the Jira REST api and curl, a command line tool for transferring data with URLs.

  • download it and save it locally on your Windows machine
  • add your credentials and the Jira API endpoint to it in the first few lines (YOUR_USERNAME, YOUR_PASSWORD, YOUR_JIRA_BASE_URL)
  • IMPORTANT: adjust the NTFS permissions of the file to allow only your account read access to the file
  • run it.

It takes two command line arguments: The Jira issue number and the path of the file, that should be uploaded:

uploadToJira.bat <issue> <file>

The file uploadToJira.bat:

@REM Add a file as attachment to a Jira issue
@REM ----------------------------------------
@REM Make sure to replace variables within this file before using it
@REM Make also sure to protect this file with NTFS settings because it contains your password

@echo off

@REM Replace YOUR_USERNAME with your username
set UserName=YOUR_USERNAME

@REM Replace YOUR_PASSWORD with your password
@set Password=YOUR_PASSWORD

@REM Replace YOUR_JIRA_BASE_URL with the base url of the jira instance, e.g. https://jira.company.de
set JiraBaseUrl=YOUR_JIRA_BASE_URL

set curlPath="%ProgramFiles%\curl\winssl\curl"

if "%1"=="" goto error
if "%2"=="" goto error

if not exist "%2" goto fileNotFoundError

if not exist %curlPath% goto curlNotFoundError

echo Rotex Jira Upload: Upload file %2 to issue %1 as user %UserName%?
pause

set url=%JiraBaseUrl%/rest/api/latest/issue/%1/attachments
echo adding file "%2" to %url%

%curlPath% -D- -u %UserName%:%Password% -X POST -H "X-Atlassian-Token: nocheck" -F "file=@%2" %url%

goto end

:error
echo Adding a file as attachment to a Jira issue
echo Usage:
echo %0 issue file
goto end

:fileNotFoundError
echo file "%2" not found
goto end

:curlNotFoundError
echo curl (%curlPath%) is not installed or was not found, please go to https://curl.haxx.se/windows/ to download it.
goto end

:end

set UserName=
set Password=