A gradle plugin to send files to slack.
Sometime in your developer life, you will need to send files to slack. When this became a routine I wrote this plugin that does the work for me. With it we're able to create custom gradle tasks that sends some file.
This was built just to upload files, if you're looking for a plugin to send messages use this one, which is based on Java Slack-webhook.
First we need to generate a token for our app. Click on the buttom bellow to generate a token for you.
To install this task is simple, it's released into Jitpack.
Go to your build.gradle
and add the following, it can be on your root level or on app level:
buildscript {
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
classpath 'com.github.brunodles:SlackUpload:-SNAPSHOT'
}
}
You can create a new task to upload the file you want.
task <your task name>(type: com.brunodles.slackupload.UploadTask) {
token "<your token>" // required, A token to identify who you are and where to send. To create the token look above. Don't need to be filled if you you use the `tokenFile`.
tokenFile "<path to your tokenfile>" // fill with the path to your token file. Don't use when `token` is filled.
file "<path to your file>" // fill with the path to the file you want to send. Don't fill this if `content` is filled
content "<content>" // fill with any content, in this case the content will be converted into a Snippet. Don't fill this if `file` is filled.
fileType "<type>" // optional
filename "<file name>" // optional, will be used on download the file
title "<title for this file>" // optional, will be shown on slack
initial_comment "<first comment>" // optional, the description of the file
channels "<channel name>" // required, can be a channel or a private group
}
Just used to test the upload task.
task testSlack(type: com.brunodles.slackupload.UploadTask) {
token "xoxp-11111111111-22222222222-33333333333-654asd645asd645sager654hge"
file "build.gradle"
channels "#general"
}
I built this task for the project I'm working on.
task sendQARelease(type: com.github.brunodles.slacksender.UploadTask, dependsOn: 'assembleRelease') {
description "Send the apk to QA Channel on Slack"
group "build"
token "xoxp-11111111111-22222222222-33333333333-654asd645asd645sager654hge"
file "app/build/outputs/apk/app-release.apk"
channels "QA"
initial_comment "*pr-${prNumber()}* - https://bitbucket.org/myteam/myproject/pull-requests/${prNumber()}"
fileType "apk"
filename "app-release${prNumber()}.apk"
title getCurrentBranchCodeName()
}
Did you noticed the getCurrentBranchCodeName
? It's a custom method inside my gradle file, so you can call it inside the task setup.
As I said on the description, this task post as you, won't be cool everyone posting as you on the slack.
So you can read the token from a file.
To do that just replace the token value by a file read operation
To do that use the project.file("token").text
.tokenFile
property to setup your task.
Like this
task sendQARelease(type: com.github.brunodles.slacksender.UploadTask, dependsOn: 'assembleRelease') {
...
tokenFile "token"
}
Now add the file (token
) into your .gitignore
.
When you have multiple upload tasks you can create a plugin extension in your project and apply the slackUpload
plugin in you app level gradle file. root/app/build.gradle
apply plugin: 'com.github.brunodles.SlackUpload'
slackUpload {
// choose one
token "<your token>"
tokenFile "<path to your tokenfile>" // <- this one is suggested
}
Just need to add this before your tasks.
Issues are welcome, create one and we will discourse about it. If you saw any error, please reports, it will be a great help.
You can use any code you found here, some of then I found on the internet too.
I'm using the MIT Licence, take a look on Licence.
If you're using this task, please give me some credits too.