Introduction
Automation is present in each and every process of software development and deployment. There are multiple ways to deploy our static files to Azure CDN. In this article, we are going to discuss how to upload the static files from your local machine to Azure CDN using gulp tasks.
We are going to get this done with the help of gulp packages from NPM. Gulp itself helps to automate some of the build/deployment processes where we can create custom tasks and add in a gulp pipeline.
Let me explain the step-by-step implementation so that it will be easy to follow.
Step 1
Create a project folder where you can keep the required files to get this task done.
Step 2
We depend on two files for this, package.json and gulpfile.js.
Step 3
Create a file called package.json with the below contents:
- {
- "name": "deploy-files-to-azure-cdn",
- "version": "0.0.1",
- "private": true,
- "engines": {
- "node": ">=0.10.0"
- },
- "scripts": {
- "build": "gulp bundle",
- "clean": "gulp clean",
- "test": "gulp test"
- },
- "dependencies": {
- "gulp-util": "^3.0.8",
- "gulp-deploy-azure-cdn": "2.1.0"
- },
- "devDependencies": {
- "gulp": "~3.9.1"
- }
- }
Step 4
Open Node JS Command Prompt and point to the location where you created the package.json file and run the below command to install the required node module packages:
Don’t worry, we just need only 3 node packages, so it won’t consume your data like a big project.
Step 5
Now create a gulpfile.js file where we will add our gulp tasks with the below contents:
- const gulp = require('gulp');
- var deployCdn = require('gulp-deploy-azure-cdn');
- var gutil = require('gulp-util');
-
- gulp.task('MeaningFullNameForTheTask', function () {
- return gulp.src('SourceFolderName/**/*', {}).pipe(deployCdn({
- containerName: 'StorageContainerName',
- serviceOptions: ['StorageAccountName', 'StorageAccountKey'],
- folder: 'FolderName (Optional)',
- deleteExistingBlobs: false,
- concurrentUploadThreads: 20,
- metadata: {
- cacheControl: 'public, max-age=1',
- cacheControlHeader: 'public, max-age=1'
- },
- testRun: false
- })).on('error', gutil.log);
- });
Step 6
“SourceFolderName” - The base folder URL for the “SourceFolderName” in gulp task src will be the gulpfile’s base folder location.
Step 7
“StorageContainerName” - The container you created in your storage account.
Step 8
“StorageAccountName” - Name of your storage account where you created the container.
Step 9
“StorageAccountKey” - Access Keys, like connection strings for DB, to securely access your Storage Account. In the properties pane of the Storage Account, under “AccessKeys” you can find the Key.
Step 10
We are all set to move the contents from our local machine to Azure CDN. Open your Node JS Command prompt and switch the directory to the location where you have placed your package.json and gulpfile.js files.
Step 11
Run the below command:
- gulp MeaningFullNameForTheTask
Based on your internet upload speed, files will be moved faster to the Azure Cloud.
Step 12
We can add any number of tasks to the same gulpfile and segregate your tasks for different use cases.
Note how easy it is to move contents from our local folder to Azure CDN, but keep in mind that since we are exposing our StorageAccountKey here, make sure that you have restricted access to the gulpfile from other users.
I hope this article helps you to understand moving files to Azure CDN with the help of Gulp Tasks. If you have any questions/issues about this article, please let me know in comments below!