Created
April 21, 2020 16:24
-
-
Save pretty25/30879d486f04475bed7df6ad005fbfe1 to your computer and use it in GitHub Desktop.
azure-pipelines-k8-deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Deploy to Azure Kubernetes Service | |
# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service | |
trigger: | |
- master | |
resources: | |
- repo: self | |
variables: | |
# Container registry service connection established during pipeline creation | |
dockerRegistryServiceConnection: '40f4d6f4-74c0-478f-9f14-460f7acae2b4' | |
imageRepository: 'prettypipelinesjavascriptdocker' | |
containerRegistry: 'spacr01.azurecr.io' | |
dockerfilePath: '**/Dockerfile' | |
tag: '$(Build.BuildId)' | |
imagePullSecret: 'spacr01c577-auth' | |
# Agent VM image name | |
vmImageName: 'ubuntu-latest' | |
# Name of the new namespace being created to deploy the PR changes. | |
k8sNamespaceForPR: '$(system.pullRequest.sourceBranch)' | |
stages: | |
- stage: Build | |
displayName: Build stage | |
jobs: | |
- job: Build | |
displayName: Build | |
pool: | |
vmImage: $(vmImageName) | |
steps: | |
- task: Docker@2 | |
displayName: Build and push an image to container registry | |
inputs: | |
command: buildAndPush | |
repository: $(imageRepository) | |
dockerfile: $(dockerfilePath) | |
containerRegistry: $(dockerRegistryServiceConnection) | |
tags: | | |
$(tag) | |
- upload: manifests | |
artifact: manifests | |
- stage: Deploy | |
displayName: Deploy stage | |
dependsOn: Build | |
jobs: | |
- deployment: Deploy | |
condition: and(succeeded(), not(startsWith(variables['Build.SourceBranch'], 'refs/pull/'))) | |
displayName: Deploy | |
pool: | |
vmImage: $(vmImageName) | |
environment: 'pretty25pipelinesjavascriptdocker.default' | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
- task: KubernetesManifest@0 | |
displayName: Create imagePullSecret | |
inputs: | |
action: createSecret | |
secretName: $(imagePullSecret) | |
dockerRegistryEndpoint: $(dockerRegistryServiceConnection) | |
- task: KubernetesManifest@0 | |
displayName: Deploy to Kubernetes cluster | |
inputs: | |
action: deploy | |
manifests: | | |
$(Pipeline.Workspace)/manifests/deployment.yml | |
$(Pipeline.Workspace)/manifests/service.yml | |
imagePullSecrets: | | |
$(imagePullSecret) | |
containers: | | |
$(containerRegistry)/$(imageRepository):$(tag) | |
- deployment: DeployPullRequest | |
displayName: Deploy Pull request | |
condition: and(succeeded(), startsWith(variables['Build.SourceBranch'], 'refs/pull/')) | |
pool: | |
vmImage: $(vmImageName) | |
environment: 'pretty25pipelinesjavascriptdocker.$(k8sNamespaceForPR)' | |
strategy: | |
runOnce: | |
deploy: | |
steps: | |
- reviewApp: default | |
- task: Kubernetes@1 | |
displayName: 'Create a new namespace for the pull request' | |
inputs: | |
command: apply | |
useConfigurationFile: true | |
inline: '{ "kind": "Namespace", "apiVersion": "v1", "metadata": { "name": "$(k8sNamespaceForPR)" }}' | |
- task: KubernetesManifest@0 | |
displayName: Create imagePullSecret | |
inputs: | |
action: createSecret | |
secretName: $(imagePullSecret) | |
namespace: $(k8sNamespaceForPR) | |
dockerRegistryEndpoint: $(dockerRegistryServiceConnection) | |
- task: KubernetesManifest@0 | |
displayName: Deploy to the new namespace in the Kubernetes cluster | |
inputs: | |
action: deploy | |
namespace: $(k8sNamespaceForPR) | |
manifests: | | |
$(Pipeline.Workspace)/manifests/deployment.yml | |
$(Pipeline.Workspace)/manifests/service.yml | |
imagePullSecrets: | | |
$(imagePullSecret) | |
containers: | | |
$(containerRegistry)/$(imageRepository):$(tag) | |
- task: Kubernetes@1 | |
name: get | |
displayName: 'Get services in the new namespace' | |
continueOnError: true | |
inputs: | |
command: get | |
namespace: $(k8sNamespaceForPR) | |
arguments: svc | |
outputFormat: jsonpath='http://{.items[0].status.loadBalancer.ingress[0].ip}:{.items[0].spec.ports[0].port}' | |
# Getting the IP of the deployed service and writing it to a variable for posing comment | |
- script: | | |
url="$(get.KubectlOutput)" | |
message="Your review app has been deployed" | |
if [ ! -z "$url" -a "$url" != "http://:" ] | |
then | |
message="${message} and is available at $url.<br><br>[Learn More](https://aka.ms/testwithreviewapps) about how to test and provide feedback for the app." | |
fi | |
echo "##vso[task.setvariable variable=GITHUB_COMMENT]$message" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment