Skip to content

Instantly share code, notes, and snippets.

@clemp6r
Last active May 11, 2021 05:34
Show Gist options
  • Save clemp6r/08c46ff5fea917ff23c0 to your computer and use it in GitHub Desktop.
Save clemp6r/08c46ff5fea917ff23c0 to your computer and use it in GitHub Desktop.
Gradle task for deploying a file to Azure Blob Storage
package com.github.clemp6r.azuregradle
import org.gradle.api.DefaultTask
import com.microsoft.azure.storage.*;
import com.microsoft.azure.storage.blob.*
import org.gradle.api.tasks.TaskAction;
class AzureStorageDeployTask extends DefaultTask {
String connectionString
String container
String fileToDeploy
@TaskAction
def deploy() {
checkArguments()
CloudStorageAccount account = CloudStorageAccount.parse(connectionString);
CloudBlobClient serviceClient = account.createCloudBlobClient();
CloudBlobContainer blobContainer = serviceClient.getContainerReference(container);
blobContainer.createIfNotExists();
def file = project.file(fileToDeploy)
CloudBlockBlob blob = blobContainer.getBlockBlobReference(file.name);
blob.upload(new FileInputStream(file), file.length());
}
private void checkArguments() {
if (!connectionString) {
throw new IllegalArgumentException("The 'connectionString' property is missing");
}
if (!container) {
throw new IllegalArgumentException("The 'container' property is missing");
}
if (!fileToDeploy) {
throw new IllegalArgumentException("The 'fileToDeploy' property is missing");
}
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.microsoft.azure:azure-storage:2.0.0'
}
@StromboliKQ
Copy link

@GokhanArik did you get a response or resolution for this? I need to push a file to azure blob storage after every build..

@anujku
Copy link

anujku commented May 16, 2019

@GokhanArik You can execute the Java/groovy class from gradle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment