Skip to content

Instantly share code, notes, and snippets.

@pretty25
Created June 23, 2020 00:29
Show Gist options
  • Save pretty25/f5453676637df037ad9bb3f377003729 to your computer and use it in GitHub Desktop.
Save pretty25/f5453676637df037ad9bb3f377003729 to your computer and use it in GitHub Desktop.
Automate Java container deployments with Azure Pipelines
az configure --defaults location=westus2
# resourceSuffix=$RANDOM
# webName="java-container-cicd-${resourceSuffix}"
# registryName="javacontainercicd${resourceSuffix}"
# dbServerName="java-container-cicd-${resourceSuffix}"
# rgName='java-containers-cicd-rg'
# planName='java-container-cicd-asp'
# az group create --name $rgName
# az mysql server create --name $dbServerName --resource-group $rgName --admin-user sysadmin --admin-password P@ssw0rd --sku-name GP_Gen5_2 --version 5.7
# az mysql server firewall-rule create --name AllowAzureServices --resource-group $rgName --server $dbServerName --start-ip-address 0.0.0.0 --end-ip-address 0.0.0.0
# az acr create --name $registryName --resource-group $rgName --sku Standard --admin-enabled true
# az appservice plan create --name $planName --resource-group $rgName --sku B1 --is-linux
# az webapp create --name $webName --resource-group $rgName --plan $planName --deployment-container-image-name $registryName.azurecr.io/web:latest
# az webapp list --resource-group $rgName --query "[].{hostName: defaultHostName, state: state}" --output table
# az acr list --resource-group $rgName --query "[].{loginServer: loginServer}" --output table
# # Cleanup
# az group delete --name java-containers-cicd-rg --no-wait -y --verbose
# az group list --output table
## Actual pipeline starts here###
trigger:
- master
resources:
- repo: self
variables:
dockerfilePath: '$(Build.SourcesDirectory)/src/Dockerfile'
webRepository: 'web'
tag: '$(Build.BuildId)'
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and publish Java container
jobs:
- job: Build
displayName: Build
pool:
vmImage: $(vmImageName)
variables:
- group: Release
steps:
- task: Maven@3
displayName: 'Maven pom.xml'
inputs:
options: '-DskipITs --settings ./maven/settings.xml'
publishJUnitResults: false
- task: CopyFiles@2
displayName: Copy WAR file to staging directory
inputs:
sourceFolder: '$(build.sourcesdirectory)'
contents: |
target/myshuttledev*.war
*.sql
targetFolder: '$(build.artifactstagingdirectory)'
- task: Docker@2
displayName: Build and push Java container image to registry
inputs:
command: buildAndPush
buildContext: '$(build.artifactstagingdirectory)'
repository: $(webRepository)
dockerfile: $(dockerfilePath)
containerRegistry: 'Container Registry Connection'
tags: |
$(tag)
- task: AzureMysqlDeployment@1
displayName: Run MySQL initialization script
inputs:
azureSubscription: 'Azure Connection'
serverName: '$(MySqlServer).mysql.database.azure.com'
databaseName: 'alm'
sqlUsername: $(MySqlUserName)@$(MySqlServer)
sqlPassword: $(MySqlPassword)
sqlFile: '$(Build.SourcesDirectory)/CreateMYSQLDB.sql'
## Deployment stage
- stage: Deploy
displayName: 'Deploy Web App'
dependsOn: Build
condition: succeeded()
jobs:
- deployment: DeploymentJob
pool:
vmImage: $(vmImageName)
environment: java-container
variables:
- group: Release
strategy:
runOnce:
deploy:
steps:
- task: AzureWebAppContainer@1
displayName: Update the web app with the new container
inputs:
appName: $(WebAppName)
azureSubscription: 'Azure Connection'
imageName: $(RegistryName)/$(webRepository):$(build.buildId)
- task: AzureAppServiceSettings@1
displayName: Update web app connection string
inputs:
azureSubscription: 'Azure Connection'
appName: $(WebAppName)
resourceGroupName: 'java-containers-cicd-rg'
connectionStrings: |
[
{
"name": "MyShuttleDb",
"value": "jdbc:mysql://$(MySqlServer).mysql.database.azure.com:3306/alm?useSSL=true&requireSSL=false&autoReconnect=true&user=$(MySqlUserName)@$(MySqlServer)&password=$(MySqlPassword)",
"type": "MySql",
"slotSetting": false
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment