Technical Thursday – CI/CD with Git, Azure and Jenkins
“How to prepare our CI/CD process?” – This could be the subtitle of this article. Why? Because I will show you how you can start to build a fully automated CI/CD process.
What is CI/CD? You can read it on Wikipedia. Nevertheless this is a very important and useful thing nowadays when we work in a DevOps model.
Scenario
In my scenario I would like to copy files from Git to Azure with Jenkins when a commit/push happens to my GitLab. As you can see this is quite complex therefore it’s a good practice example.
Important to know, purpose of this post to show you how can you integrate within some minutes your GitLab and your Jenkins. (So we will use our personal git account for configure connection and we will create the connection between Jenkins and Git over https – and not SSH) This means due to testing purpose we won’t create a very secure integration. 😉
Integration
Assumptions and prerequisites
- You have a Jenkins environment for automation which is a general used tool. Installation steps are here.
- You have a configured GitLab environment
- You configure your environment only for testing purpose. In other case you have to use different parameters or ssh keys during configuration from security point of view.
- You store your application in Git.
- You configured the pipeline solution in our Git.
- You have an Azure subscription with owner privileges.
Step 1: Configure Jenkins
Here you have to install some plugins to Jenkins.
- Login to your Jenkins server
- Navigate to “Manage Jenkins > Manage Plugins”
- From the “Available” tab, find and select the following plugins:
- GitLab Plugin
- Azure CLI Plugin
- Azure Credentials
- Click the “Download now and install after restart” button to download it.
- Once the plugin has been downloaded, click the “Restart Jenkins…” checkbox and wait for Jenkins to restart.
- When Jenkins restarted navigate to “Manage Jenkins > Configure System”
- Find the “Git plugin” section and configure Git basic values
- Save configuration
Step 2: Service Principal in Azure
To be able to upload our files to Azure we have to create a Service Principal which has enough privileges to make it.
- Login to a computer where the Azure-Cli 2.0 is installed
- Login to the subscription where you would like to create the Service Principal
# check relevant cloud infra where you want to login (i.e. AzureGermanCloud, AzureCloud, AzureChinaCloud, ...) az cloud set --name <name of Cloud> # Please login to your azure account az login -u <useraccount> # Select your subscription az account set --subscription <subscription ID>
- Create a Service Principal with the following command
az ad sp create-for-rbac --name <Service Principal name in Azure. eg. JenkinsGitAzure-the1bithu> --query '{"client_id": appId, "secret": password, "tenant": tenant}'
- Copy these values to a safety place because we will use it in Jenkins!
Step 3: Credentials in Jenkins
Now set some credentials such as Git, Azure.
- Navigate to “Credentials > System”
- Choose the “Global credentials” domain
- Click on “Add credentials” button
- Create GitLab user credential
- Kind: Username with password
- Username: <your git username>
- Password: <your git password>
- ID: <An internal unique ID by which these credentials are identified from jobs and other configuration. Normally left blank, in which case an ID will be generated, which is fine for jobs created using visual forms. Useful to specify explicitly when using credentials from scripted configuration. >
- Then click OK button
- Create Azure Service Principal credential (We need the data from Step 2)
- Kind: Microsoft Azure Service Principal
- Subscription ID: <Azure Storage account subscription ID>
- Client ID: <Azure Service Principal Client ID>
- Client Secret: <Azure Service Principal Client Secret>
- Tenant ID <Azure Service Principal Tenant ID>
- Azure Environment: <choose one according to your subscription location>
- ID: <An internal unique ID by which these credentials are identified from jobs and other configuration. Normally left blank, in which case an ID will be generated, which is fine for jobs created using visual forms. Useful to specify explicitly when using credentials from scripted configuration. >
- Then click OK button
- Create Azure Storage Account SAS token credential (because our pipeline solution requires the SAS token we have to store somewhere on secure way)
- Kind: Secret text
- Secret: <paste here the SAS token for storage account. It begins with ‘?sv=’>
- ID: <eg. sasTokenAzure | An internal unique ID by which these credentials are identified from jobs and other configuration. Normally left blank, in which case an ID will be generated, which is fine for jobs created using visual forms. Useful to specify explicitly when using credentials from scripted configuration. >
- Then click OK button
- Create Azure Storage Account Account Key credential (because our pipeline solution requires the Account Key we have to store somewhere on secure way)
- Kind: Secret text
- Secret: <paste here the Account Key for storage account.>
- ID: <eg. storageKeyAzure | An internal unique ID by which these credentials are identified from jobs and other configuration. Normally left blank, in which case an ID will be generated, which is fine for jobs created using visual forms. Useful to specify explicitly when using credentials from scripted configuration. >
- Then click OK button
Step 4: Create Jenkins project
- Click on ‘New Item’
- Type a name to “Enter an item name” field and choose “Pipeline project” then click on OK button
- Build Triggers
- Tick “Build when a change is pushed to GitLab. GitLab webhook URL:”
- Click on “Advanced” button
- Choose the “Filter branches by name” and write in the include filed your branch name
If you receive an error please ignore because it will be fixed when you will integrate your project with Git. (in Step 5) - Then click on “Generate” button to genetate token for this project.
- Tick “Build when a change is pushed to GitLab. GitLab webhook URL:”
- Pipeline
- Select “Piepeline script from SCM” at definition
- SCM: Git
- Repositories
- Paste the clonable url into “Repository URL”. (eg. https://gitlab.com/*****/*****.git)
- Credentials. Choose the credential which was created in Step 3.4.
- Branches to build. Put here your baranch instead of master. (eg. */master)
- Script Path (where the Jenkins file is stored in Git): pipeline/Jenkinsfile
- Click Save
- Click “Build Now” to test it from Jenkins
- Check Console output
Step 5: Integrate GitLab with Jenkins project
- Login to GitLab
- Step into your project
- Navigate to “Settings > Integrations”
- Paste the Jenkins project URL (Step 4.3.1) and Token (Step 4.3.4) to the first two fields
- Choose the required Triggers
- Uncheck the “Enable SSL verification” (if you use self-signed certificate on Jenkins)
- Click “Add webhook” button
- Scroll down and find your newly created webhook at middle of screen
- Click the “Push events” under the “Test” button dropdown menu.
- If you receive a HTTP 200 message with blue background the integration was success
Step 6: Test integration
Awesome…As you can see it works. 🙂
Please kindly notes this is a very basic implementation. If you would like to use it in production you have to configure impersonated accounts for git connections and you have to configure the pipeline solution according to your storage account related data. Additionally the SSH based integration could be better later.
Video tutorial to create Service Principal in Azure – IT Blog for sharing
2018-10-19 @ 15:26
[…] this spring I wrote a post about CI/CD with Git, Azure and Jenkins where I showed you an easy configuration of a CI/CD process. This post contained a chapter where […]
Technical Thursday – Azure-Cli storage account bug has been fixed – IT Blog for sharing
2018-05-10 @ 15:32
[…] CI/CD with Git, Azure and Jenkins […]
Technical Thursday – Jenkins pipeline and AMArETTo – IT Blog for sharing
2018-05-03 @ 23:58
[…] week I showed you How you can integrate Git and Jenkins. Inside that post I did not provide script part for Azure related operation. Today I would like to […]