Skip to content

Commit b5ead3c

Browse files
authored
Merge pull request #99073 from puicchan/master
New Jenkins CICD tutorial for Azure Spring Cloud
2 parents 2503fb2 + 6485137 commit b5ead3c

File tree

2 files changed

+282
-0
lines changed

2 files changed

+282
-0
lines changed
61.1 KB
Loading
Lines changed: 282 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,282 @@
1+
---
2+
title: Deploy apps to Azure Spring Cloud using Jenkins and the Azure CLI
3+
description: Learn how to use Azure CLI in a continuous integration and deployment pipeline to deploy microservices to Azure Spring Cloud service
4+
ms.topic: tutorial
5+
ms.date: 12/06/2019
6+
---
7+
8+
# Tutorial: Deploy apps to Azure Spring Cloud using Jenkins and the Azure CLI
9+
10+
[Azure Spring Cloud](https://docs.microsoft.com/azure/spring-cloud/spring-cloud-overview) is a fully managed microservice development with built-in service discovery and configuration management. The service makes it easy to deploy Spring Boot-based microservice applications to Azure. This tutorial demonstrates how you can use Azure CLI in Jenkins to automate continuous integration and delivery (CI/CD) for Azure Spring Cloud.
11+
12+
In this tutorial, you'll complete these tasks:
13+
14+
> [!div class="checklist"]
15+
> * Provision a service instance and launch a Java Spring application
16+
> * Prepare your Jenkins server
17+
> * Use the Azure CLI in a Jenkins pipeline to build and deploy the microservice applications
18+
19+
This tutorial assumes intermediate knowledge of core Azure services, Azure Spring Cloud, Jenkins [pipelines](https://jenkins.io/doc/book/pipeline/) and plug-ins, and GitHub.
20+
21+
## Prerequisites
22+
23+
>[!Note]
24+
> Azure Spring Cloud is currently offered as a public preview. Public preview offerings allow customers to experiment with new features prior to their official release. Public preview features and services are not meant for production use. For more information about support during previews, please review our [FAQ](https://azure.microsoft.com/support/faq/) or file a [Support request](https://docs.microsoft.com/azure/azure-supportability/how-to-create-azure-support-request) to learn more.
25+
26+
[!INCLUDE [open-source-devops-prereqs-azure-subscription.md](../../includes/open-source-devops-prereqs-azure-subscription.md)]
27+
28+
* A GitHub account. If you don't have a GitHub account, create a [free account](https://github.com/) before you begin.
29+
30+
* A Jenkins master server. If you don't already have a Jenkins master, deploy [Jenkins](https://aka.ms/jenkins-on-azure) on Azure by following the steps in this [quickstart](https://docs.microsoft.com/azure/jenkins/install-jenkins-solution-template). The following are required on the Jenkins node/agent (for example. build server):
31+
32+
* [Git](https://git-scm.com/)
33+
* [JDK 8](https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable)
34+
* [Maven 3.0 or above](https://maven.apache.org/download.cgi)
35+
* [Azure CLI installed](/cli/azure/install-azure-cli?view=azure-cli-latest), version 2.0.67 or higher
36+
37+
>[!TIP]
38+
> Tools like Git, JDK, Az CLI, and Azure plug-ius are included by default in the Azure Marketplace [Microsoft Jenkins](https://aka.ms/jenkins-on-azure) solution template.
39+
40+
* [Sign up for an Azure subscription](https://azure.microsoft.com/free/)
41+
42+
## Provision a service instance and launch a Java Spring application
43+
44+
We use [Piggy Metrics](https://github.com/Azure-Samples/piggymetrics) as the sample Microsoft service application and follow the same steps in [Quickstart: Launch a Java Spring application using the Azure CLI](https://docs.microsoft.com/azure/spring-cloud/spring-cloud-quickstart-launch-app-cli) to provision the service instance and set up the applications. If you have already gone through the same process, you can skip to the next section. Otherwise, included in the following are the Azure CLI commands. Refer to [Quickstart: Launch a Java Spring application using the Azure CLI](https://docs.microsoft.com/azure/spring-cloud/spring-cloud-quickstart-launch-app-cli) to get additional background information.
45+
46+
Your local machine needs to meet the same prerequisite as the Jenkins build server. Make sure the following are installed to build and deploy the microservice applications:
47+
* [Git](https://git-scm.com/)
48+
* [JDK 8](https://docs.microsoft.com/java/azure/jdk/?view=azure-java-stable)
49+
* [Maven 3.0 or above](https://maven.apache.org/download.cgi)
50+
* [Azure CLI installed](/cli/azure/install-azure-cli?view=azure-cli-latest), version 2.0.67 or higher
51+
52+
1. Install the Azure Spring Cloud extension:
53+
54+
```Azure CLI
55+
az extension add --name spring-cloud
56+
```
57+
58+
2. Create a resource group to contain your Azure Spring Cloud service:
59+
60+
```Azure CLI
61+
az group create --location eastus --name <resource group name>
62+
```
63+
64+
3. Provision an instance of Azure Spring Cloud:
65+
66+
```Azure CLI
67+
az spring-cloud create -n <service name> -g <resource group name>
68+
```
69+
70+
4. Fork the [Piggy Metrics](https://github.com/Azure-Samples/piggymetrics) repo to your own GitHub account. In your local machine, clone your repo in a directory called `source-code`:
71+
72+
```bash
73+
mkdir source-code
74+
git clone https://github.com/<your GitHub id>/piggymetrics
75+
```
76+
77+
5. Set up your configuration server. Make sure you replace &lt;your GitHub id&gt; with the correct value.
78+
79+
```Azure CLI
80+
az spring-cloud config-server git set -n <your-service-name> --uri https://github.com/<your GitHub id>/piggymetrics --label config
81+
```
82+
83+
6. Build the project:
84+
85+
```bash
86+
cd piggymetrics
87+
mvn clean package -D skipTests
88+
```
89+
90+
7. Create the three microservices: **gateway**, **auth-service**, and **account-service**:
91+
92+
```Azure CLI
93+
az spring-cloud app create --n gateway -s <service name> -g <resource group name>
94+
az spring-cloud app create --n auth-service -s <service name> -g <resource group name>
95+
az spring-cloud app create --n account-service -s <service name> -g <resource group name>
96+
```
97+
98+
8. Deploy the applications:
99+
100+
```Azure CLI
101+
az spring-cloud app deploy -n gateway -s <service name> -g <resource group name> --jar-path ./gateway/target/gateway.jar
102+
az spring-cloud app deploy -n account-service -s <service name> -g <resource group name> --jar-path ./account-service/target/account-service.jar
103+
az spring-cloud app deploy -n auth-service -s <service name> -g <resource group name> --jar-path ./auth-service/target/auth-service.jar
104+
```
105+
106+
9. Assign public endpoint to gateway:
107+
108+
```Azure CLI
109+
az spring-cloud app update -n gateway -s <service name> -g <resource group name> --is-public true
110+
```
111+
112+
10. Query the gateway application to get the url so that you can verify that the application is running.
113+
114+
```Azure CLI
115+
az spring-cloud app show --name gateway | grep url
116+
```
117+
118+
Navigate to the URL provided by the previous command to run the PiggyMetrics application.
119+
120+
## Prepare Jenkins server
121+
122+
In this section, you prepare the Jenkins server to run a build, which is fine for testing. However, because of security implication, you should use an [Azure VM agent](https://plugins.jenkins.io/azure-vm-agents) or [Azure Container agent](https://plugins.jenkins.io/azure-container-agents) to spin up an agent in Azure to run your builds. For more information, see the Jenkins article on the [security implications of building on master](https://wiki.jenkins.io/display/JENKINS/Security+implication+of+building+on+master).
123+
124+
### Install plug-ins
125+
126+
1. Sign in to your Jenkins server. Choose **Manage Jenkins > Manage Plugins**.
127+
2. On the **Available** tab, select the following plug-ins:
128+
* [GitHub Integration](https://plugins.jenkins.io/github-pullrequest)
129+
* [Azure Credential](https://plugins.jenkins.io/azure-credentials)
130+
131+
If these plug-ins don't appear in the list, check the **Installed** tab to see if they're already installed.
132+
133+
3. To install the plug-ins, choose **Download now and install after restart**.
134+
135+
4. Restart your Jenkins server to complete the installation.
136+
137+
### Add your Azure Service Principal credential in Jenkins credential store
138+
139+
1. You need an Azure Service Principal to deploy to Azure. For more information, see the [Create service principal](https://docs.microsoft.com/azure/jenkins/tutorial-jenkins-deploy-web-app-azure-app-service#create-service-principal) section in the Deploy to Azure App Service tutorial. The output from `az ad sp create-for-rbac` looks something like this:
140+
141+
```
142+
{
143+
"appId": "xxxxxx-xxx-xxxx-xxx-xxxxxxxxxxxx",
144+
"displayName": "xxxxxxxjenkinssp",
145+
"name": "http://xxxxxxxjenkinssp",
146+
"password": "xxxxxx-xxx-xxxx-xxx-xxxxxxxxxxxx",
147+
"tenant": "xxxxxx--xxx-xxxx-xxx-xxxxxxxxxxxx"
148+
}
149+
```
150+
151+
2. On the Jenkins dashboard, select **Credentials** > **System**. Then, select **Global credentials(unrestricted)**.
152+
153+
3. Select **Add Credentials**.
154+
155+
4. Select **Microsoft Azure Service Principal** as kind.
156+
157+
5. Supply values for:
158+
* Subscription ID: use your Azure subscription ID
159+
* Client ID: use `appId`
160+
* Client Secret: use `password`
161+
* Tenant ID: use `tenant`
162+
* Azure Environment: select a pre-set value. For example, use **Azure** for Azure Global
163+
* ID: set as **azure_service_principal**. We use this ID in a later step in this article
164+
* Description: is an optional field. We recommend providing a meaningful value here.
165+
166+
### Install Maven and Az CLI spring-cloud extension
167+
168+
The sample pipeline uses Maven to build and Az CLI to deploy to the service instance. When Jenkins is installed, it creates an admin account named *jenkins*. Ensure that the user *jenkins* has permission to run the spring-cloud extension.
169+
170+
1. Connect to the Jenkins master via SSH.
171+
172+
2. Install Maven
173+
174+
```bash
175+
sudo apt-get install maven
176+
```
177+
178+
3. Install the Azure CLI. For more information, see [Installing the Azure CLI](https://docs.microsoft.com/cli/azure/install-azure-cli?view=azure-cli-latest). Azure CLI is installed by default if you use [Jenkins Master on Azure](https://aka.ms/jenkins-on-azure).
179+
180+
4. Switch to the `jenkins` user:
181+
182+
```bash
183+
sudo su jenkins
184+
```
185+
186+
5. Add the **spring-cloud** extension:
187+
188+
```bash
189+
az extension add --name spring-cloud
190+
```
191+
192+
## Create a Jenkinsfile
193+
1. In your own repo (https://github.com/&lt;your GitHub id&gt;/piggymetrics), create a **Jenkinsfile** in the root.
194+
195+
2. Update the file as follows. Make sure you replace the values of **\<resource group name>** and **\<service name>**. Replace **azure_service_principal** with the right ID if you use a different value when you added the credential in Jenkins.
196+
197+
```groovy
198+
node {
199+
stage('init') {
200+
checkout scm
201+
}
202+
stage('build') {
203+
sh 'mvn clean package'
204+
}
205+
stage('deploy') {
206+
withCredentials([azureServicePrincipal('azure_service_principal')]) {
207+
// login to Azure
208+
sh '''
209+
az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET -t $AZURE_TENANT_ID
210+
az account set -s $AZURE_SUBSCRIPTION_ID
211+
'''
212+
// Set default resource group name and service name. Replace <resource group name> and <service name> with the right values
213+
sh 'az configure --defaults group=<resource group name>'
214+
sh 'az configure --defaults spring-cloud=<service name>'
215+
// Deploy applications
216+
sh 'az spring-cloud app deploy -n gateway --jar-path ./gateway/target/gateway.jar'
217+
sh 'az spring-cloud app deploy -n account-service --jar-path ./account-service/target/account-service.jar'
218+
sh 'az spring-cloud app deploy -n auth-service --jar-path ./auth-service/target/auth-service.jar'
219+
sh 'az logout'
220+
}
221+
}
222+
}
223+
```
224+
225+
3. Save and commit the change.
226+
227+
## Create the job
228+
229+
1. On the Jenkins dashboard, click **New Item**.
230+
231+
2. Provide a name, *Deploy-PiggyMetrics* for the job and select **Pipeline**. Click OK.
232+
233+
3. Click the **Pipeline** tab next.
234+
235+
4. For **Definition**, select **Pipeline script from SCM**.
236+
237+
5. For **SCM**, select **Git**.
238+
239+
6. Enter the GitHub URL for your forked repo: **https://github.com/&lt;your GitHub id&gt;/piggymetrics.git**
240+
241+
7. Make sure **Branch Specifier (black for 'any')** is ***/Azure**
242+
243+
8. Keep **Script path** as **Jenkinsfile**
244+
245+
7. Click **Save**
246+
247+
## Validate and run the job
248+
249+
Before running the job, let's update the text in the login input box to **enter login ID**.
250+
251+
1. In your own repo, open `index.html` in **/gateway/src/main/resources/static/**
252+
253+
2. Search for "enter your login" and update to "enter login ID"
254+
255+
```HTML
256+
<input class="frontforms" id="frontloginform" name="username" placeholder="enter login ID" type="text" autocomplete="off"/>
257+
```
258+
259+
3. Commit the changes
260+
261+
4. Run the job in Jenkins manually. On the Jenkins dashboard, click the job *Deploy-PiggyMetrics* and then **Build Now**.
262+
263+
After the job is complete, navigate to the public IP of the **gateway** application and verify that your application has been updated.
264+
265+
![Updated Piggy Metrics](media/tutorial-jenkins-deploy-cli-spring-cloud/piggymetrics.png)
266+
267+
## Clean up resources
268+
269+
When no longer needed, delete the resources created in this article:
270+
271+
```bash
272+
az group delete -y --no-wait -n <resource group name>
273+
```
274+
275+
## Next steps
276+
277+
In this article, you learned how to use Azure CLI in Jenkins to automate continuous integration and delivery (CI/CD) for Azure Spring Cloud.
278+
279+
To learn more about Azure Jenkins provider, see the Jenkins on Azure site.
280+
281+
> [!div class="nextstepaction"]
282+
> [Jenkins on Azure](/azure/jenkins/)

0 commit comments

Comments
 (0)