Setup CI - CD Pipeline Gitlabto Deploy To Azure Storage&CDN
Setup CI - CD Pipeline Gitlabto Deploy To Azure Storage&CDN
For the purpose of this demo, I have already created a simple Angular 7
application that is hosted on Gitlab, the application is a simple, digital
clock that looks something like this.
The Overview
The above diagram describes the whole process, right from a developer
pushing the code, to the point where the CI/CD Pipeline builds & deploys
the code to Azure Storage (Blob) which is linked to Azure CDN Endpoint.
An azure storage account contains all of your Azure Storage data objects:
blobs, files, queues, tables, and disks for our example we would be using
blobs.
1. Log in to the Azure portal, and search storage account and create a
Storage account.
2. After the Creation of the Storage Account create a Blob/Container in
order to store static Angular build files.
Probe Path: Probe path includes the file placed on the origin server to
optimize network routing configurations for the CDN. In our case the origin
server is Storage Blob to which the CDN Endpoint is pointing to.
After successfully creating the CDN Endpoint, here’s how it looks like, we
need to now make sure that whenever the user enters the CDN Endpoint
URL which is linked to the storage blob it should be able to access the
website but that’s not how it works by default, as it's not able to locate
index.html.It’s never recommended for the user to
enter www.domainname.com/index.html as that adds an extra overhead
from the user's side to append index.html each time he wants to access
the website. In order to fix this, we will be using Verizon CDN Portal to
configure the redirection rules for index.html as well as http-to-https
redirection.
This will perform a URL-redirect if in case the user request for http it will
perform a URL redirect to https.
This rule will perform URL rewrite so that even if the user enters the CDN
endpoint it will perform URL rewrite and add index.html automatically so
that it removes the overhead for the user to append index.html to the
domain name each time.
After the CDN portal opens up we need to click on ADN, as we have DSA as
a CDN type enabled and then click on Rules Engine after which we now can
configure the rules for the same.
1. http-to-https redirection
<rules schema-version=“2”
rulesetversion=“2”rulesetid=“XXXXXXX”xmlns=“http://www.whitecdn.com/schemas/rules/2.0/
rulesSchema.xsd”><rule id=“XXXXXXX” platform=“adn” status=“active”version=“0”
custid=“XXXXXXX”><description>tymmychn_http_to_https</description><!–If–>
<match.request-scheme value=“http”><feature.url-redirect
code=“301”pattern=“/XXXXXXX/tymmychn/tymmychn/(.*)”value=“https://%{host}/$1” />
</match.request-scheme></rule></rules>
After Saving the Rules it will take some time for the changes to the
reflected, which can be up to 4 hours and in the worst case even more.
After the Pending XML changes to Active XML that’s an indication that the
changes have been applied.
BACK TO GITLAB.
Now after setting up the whole infrastructure that was needed at the Azure
side, which involved setting up a storage account which will have blobs
that could store the Angular static build files (HTML, CSS and JavaScript)
as well as to serve the end-user with help of CDN Endpoint for fast
delivery.
Now its time to configure the CI/CD pipeline on Gitlab, as we already know
that GitLab provides a simple way to configure CI/CD on each repository
by mentioned all the steps in .gitlab-ci.yml file.
image: chrisedrego/azng_ubuntu:latest
stages:
- build_deploy
build_deploy:
stage: build_deploy
script:
- az login -u $AZ_USER_NAME -p $AZ_USER_PASSWORD
- az account set --subscription $AZ_SUBSCRIPTION_ID
- npm i -g @angular/cli && npm install --save-dev @angular-devkit/build-angular
# - /usr/bin/ng build --output-hashing none --base-href
https://tymmychn.blob.core.windows.net/tymmychn/
- /usr/bin/ng build --output-hashing none --base-href https://tymmychn.azureedge.net/
- az storage blob upload-batch -s ./dist/tymmchyn -d $BLOB_NAME --account-key
$ACCOUNT_KEY --account-name $ACCOUNT_NAME
- az cdn endpoint purge -g $RESOURCE_GROUP --name tymmychn --profile-name
tymmchyn --content-paths "/*"
https://gitlab.com/chrisedrego/tym_mchyn/-/blob/master/.gitlab-ci.yml
Link to this GitLab-repo contains the codebase along with the gitLab-
ci.yml. In this case, let’s quickly examine the GitLab-ci.yml and here what it
does.
Line 1: IMAGE, refers to the link to the docker image that I have used for
this example which contains the needed tools involving Azure CLI and
Node JS, NPM
Gitlab already does the overhead of cloning the repository with the branch
on the which we are currently, so taking that into consideration we can
now go ahead and perform the following steps:
Now if the pipeline was successfully and the Verizon rules have applied, we
can head over to our CDN Endpoint and we should see the final magic.