|
| 1 | +--- |
| 2 | +title: Azure Spring Cloud CI/CD with GitHub Actions |
| 3 | +description: How to build up CI/CD workflow for Azure Spring Cloud with GitHub Actions |
| 4 | +author: MikeDodaro |
| 5 | +ms.author: barbkess |
| 6 | +ms.service: spring-cloud |
| 7 | +ms.topic: how-to |
| 8 | +ms.date: 01/15/2019 |
| 9 | +--- |
| 10 | +# Azure Spring Cloud CI/CD with GitHub Actions |
| 11 | + |
| 12 | +GitHub Actions support an automated software development lifecycle workflow. With GitHub Actions for Azure Spring Cloud you can create workflows in your repository to build, test, package, release, and deploy to Azure. |
| 13 | + |
| 14 | +## Prerequisites |
| 15 | +This example requires the [Azure CLI](https://review.docs.microsoft.com/azure/spring-cloud/spring-cloud-quickstart-launch-app-cli#install-the-azure-cli-extension) |
| 16 | + |
| 17 | +## Set up your GitHub repository and authenticate with Azure |
| 18 | +You need an Azure service principle credential to authorize Azure login action. To get an Azure credential, execute the following commands on your local machine: |
| 19 | +``` |
| 20 | +az login |
| 21 | +az ad sp create-for-rbac --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID> --sdk-auth |
| 22 | +``` |
| 23 | +To access to a specific resource group, you can reduce the scope: |
| 24 | +``` |
| 25 | +az ad sp create-for-rbac --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID>/resourceGroups/{RESOURCE_GROUP} --sdk-auth |
| 26 | +``` |
| 27 | +The command should output a JSON object: |
| 28 | +```JSON |
| 29 | +{ |
| 30 | + "clientId": "<GUID>", |
| 31 | + "clientSecret": "<GUID>", |
| 32 | + "subscriptionId": "<GUID>", |
| 33 | + "tenantId": "<GUID>", |
| 34 | + ... |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +This example uses the [Piggy Metrics](https://github.com/Azure-Samples/piggymetrics) sample on GitHub. Fork the sample, open GitHub repository page, and click Settings tab. Open Secrets menu, and click Add a new secret: |
| 39 | + |
| 40 | +  |
| 41 | + |
| 42 | +Set the secret name to AZURE_CREDENTIALS, and its value to the JSON string that you found under the heading *Set up your GitHub repository and authenticate with Azure*. |
| 43 | + |
| 44 | +  |
| 45 | + |
| 46 | +## Provision Azure Spring Cloud service instance |
| 47 | +To provision your service instance, run the following commands using the Azure CLI. |
| 48 | +``` |
| 49 | +az extension add --name spring-cloud |
| 50 | +az group create --location eastus --name <resource group name> |
| 51 | +az spring-cloud create -n <service instance name> -g <resource group name> |
| 52 | +az spring-cloud config-server git set -n <service instance name> --uri https://github.com/xxx/piggymetrics --label config |
| 53 | +``` |
| 54 | +## Build the workflow |
| 55 | +The workflow can be defined using the following options. |
| 56 | + |
| 57 | +### Prepare for deployment with Azure CLI |
| 58 | +The command `az spring-cloud app create` is currently not idempotent. We recommend this workflow on existing Azure Spring Cloud apps and instances. |
| 59 | + |
| 60 | +Use the following Azure CLI commands for preparation: |
| 61 | +``` |
| 62 | +az configure --defaults group=<service group name> |
| 63 | +az configure --defaults spring-cloud=<service instance name> |
| 64 | +az spring-cloud app create --name gateway |
| 65 | +az spring-cloud app create --name auth-service |
| 66 | +az spring-cloud app create --name account-service |
| 67 | +``` |
| 68 | + |
| 69 | +### Deploy with Azure CLI directly |
| 70 | +Create the `.github/workflow/main.yml` file in the repository: |
| 71 | + |
| 72 | +``` |
| 73 | +name: AzureSpringCloud |
| 74 | +
|
| 75 | +env: |
| 76 | + GROUP: <resource group name> |
| 77 | + SERVICE_NAME: <service instance name> |
| 78 | +
|
| 79 | +jobs: |
| 80 | + build-and-deploy: |
| 81 | + runs-on: ubuntu-latest |
| 82 | + steps: |
| 83 | + |
| 84 | + - uses: actions/checkout@master |
| 85 | + |
| 86 | + - name: Set up JDK 1.8 |
| 87 | + uses: actions/setup-java@v1 |
| 88 | + with: |
| 89 | + java-version: 1.8 |
| 90 | + |
| 91 | + - name: maven build, clean |
| 92 | + run: | |
| 93 | + mvn clean package -D skipTests |
| 94 | + |
| 95 | + - name: Azure Login |
| 96 | + uses: azure/login@v1 |
| 97 | + with: |
| 98 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 99 | + |
| 100 | + - name: Install ASC AZ extension |
| 101 | + run: az extension add --name spring-cloud |
| 102 | + |
| 103 | + - name: Deploy with AZ CLI commands |
| 104 | + run: | |
| 105 | + az configure --defaults group=$GROUP |
| 106 | + az configure --defaults spring-cloud=$SERVICE_NAME |
| 107 | + az spring-cloud app deploy -n gateway --jar-path ${{ github.workspace }}/gateway/target/gateway.jar |
| 108 | + az spring-cloud app deploy -n account-service --jar-path ${{ github.workspace }}/account-service/target/account-service.jar |
| 109 | + az spring-cloud app deploy -n auth-service --jar-path ${{ github.workspace }}/auth-service/target/auth-service.jar |
| 110 | +``` |
| 111 | +### Deploy with Azure CLI action |
| 112 | +The az `run` command will use the latest version of Azure CLI. In case of breaking changes, you can also use a specific version of Azure CLI with azure/CLI `action`. This will run all the az command in a new container, therefore note that `env` will not work, and cross action file access may have extra restrictions. |
| 113 | + |
| 114 | +Create the .github/workflow/main.yml file in the repository: |
| 115 | +``` |
| 116 | +name: AzureSpringCloud |
| 117 | +
|
| 118 | +jobs: |
| 119 | + build-and-deploy: |
| 120 | + runs-on: ubuntu-latest |
| 121 | + steps: |
| 122 | + |
| 123 | + - uses: actions/checkout@master |
| 124 | + |
| 125 | + - name: Set up JDK 1.8 |
| 126 | + uses: actions/setup-java@v1 |
| 127 | + with: |
| 128 | + java-version: 1.8 |
| 129 | + |
| 130 | + - name: maven build, clean |
| 131 | + run: | |
| 132 | + mvn clean package -D skipTests |
| 133 | + |
| 134 | + - name: Azure Login |
| 135 | + uses: azure/login@v1 |
| 136 | + with: |
| 137 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 138 | + |
| 139 | + - name: Azure CLI script |
| 140 | + uses: azure/CLI@v1 |
| 141 | + with: |
| 142 | + azcliversion: 2.0.75 |
| 143 | + inlineScript: | |
| 144 | + az extension add --name spring-cloud |
| 145 | + az configure --defaults group=<service group name> |
| 146 | + az configure --defaults spring-cloud=<service instance name> |
| 147 | + az spring-cloud app deploy -n gateway --jar-path $GITHUB_WORKSPACE/gateway/target/gateway.jar |
| 148 | + az spring-cloud app deploy -n account-service --jar-path $GITHUB_WORKSPACE/account-service/target/account-service.jar |
| 149 | + az spring-cloud app deploy -n auth-service --jar-path $GITHUB_WORKSPACE/auth-service/target/auth-service.jar |
| 150 | +``` |
| 151 | + |
| 152 | +## Deploy with Maven Plugin |
| 153 | +Another option is to use the [Maven Plugin](https://docs.microsoft.com/azure/spring-cloud/spring-cloud-quickstart-launch-app-maven) for deploying the Jar and updating App settings. The command `mvn azure-spring-cloud:deploy` is idempotent and will automatically create Apps if needed. You don't need to create corresponding apps in advance. |
| 154 | + |
| 155 | +``` |
| 156 | +name: AzureSpringCloud |
| 157 | +
|
| 158 | +jobs: |
| 159 | + build-and-deploy: |
| 160 | + runs-on: ubuntu-latest |
| 161 | + steps: |
| 162 | + |
| 163 | + - uses: actions/checkout@master |
| 164 | + |
| 165 | + - name: Set up JDK 1.8 |
| 166 | + uses: actions/setup-java@v1 |
| 167 | + with: |
| 168 | + java-version: 1.8 |
| 169 | + |
| 170 | + - name: maven build, clean |
| 171 | + run: | |
| 172 | + mvn clean package -D skipTests |
| 173 | + |
| 174 | + # Maven plugin can cosume this authentication method automatically |
| 175 | + - name: Azure Login |
| 176 | + uses: azure/login@v1 |
| 177 | + with: |
| 178 | + creds: ${{ secrets.AZURE_CREDENTIALS }} |
| 179 | + |
| 180 | + # Maven deploy, make sure you have correct configurations in your pom.xml |
| 181 | + - name: deploy to Azure Spring Cloud using Maven |
| 182 | + run: | |
| 183 | + mvn azure-spring-cloud:deploy |
| 184 | +``` |
| 185 | + |
| 186 | +## Run the workflow |
| 187 | +GitHub Actions should be enabled automatically after you push `.github/workflow/main.yml` to GitHub. The action will be triggered when you push a new commit. If you create this file in the browser, your action should have already run. |
| 188 | + |
| 189 | +To verify your action has been enabled, click **Actions** tab on the GitHub repository page: |
| 190 | + |
| 191 | +  |
| 192 | + |
| 193 | +If your action runs in error, for example, you haven't set Azure credential, you can re-run checks after fixing the error. On GitHub repository page, click **Actions**, select the specific workflow task, then click Re-run checks button to re-run checks: |
| 194 | + |
| 195 | +  |
| 196 | + |
| 197 | +## Next steps |
| 198 | +* [Azure Active Directory service principals](https://docs.microsoft.com/cli/azure/ad/sp?view=azure-cli-latest#az-ad-sp-create-for-rbac) |
| 199 | +* [GitHub Actions for Azure](https://github.com/Azure/actions/) |
0 commit comments