diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 000000000..d9567d95f --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,144 @@ +# Deploy to Azure Kubernetes Service +# Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service +# https://docs.microsoft.com/azure/devops/pipelines/languages/docker + + + +trigger: +- main + +resources: +- repo: self + +variables: + + # Container registry service connection established during pipeline creation + dockerRegistryServiceConnection: '70f4cae7-5b11-4a41-89b0-3e0ae9643c13' + imageRepository: 'mtailebpipelinesjavascriptdocker' + containerRegistry: 'acrmt.azurecr.io' + dockerfilePath: '**/Dockerfile' + tag: '$(Build.BuildId)' + imagePullSecret: 'acrmt040d-auth' + + # Agent VM image name + vmImageName: 'ubuntu-latest' + + # Name of the new namespace being created to deploy the PR changes. + k8sNamespaceForPR: 'review-app-$(System.PullRequest.PullRequestId)' + +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: 'mtailebpipelinesjavascriptdocker.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: 'mtailebpipelinesjavascriptdocker.$(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.

[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" diff --git a/manifests/deployment.yml b/manifests/deployment.yml new file mode 100644 index 000000000..6dbdf6046 --- /dev/null +++ b/manifests/deployment.yml @@ -0,0 +1,19 @@ +apiVersion : apps/v1 +kind: Deployment +metadata: + name: mtailebpipelinesjavascriptdocker +spec: + replicas: 1 + selector: + matchLabels: + app: mtailebpipelinesjavascriptdocker + template: + metadata: + labels: + app: mtailebpipelinesjavascriptdocker + spec: + containers: + - name: mtailebpipelinesjavascriptdocker + image: acrmt.azurecr.io/mtailebpipelinesjavascriptdocker + ports: + - containerPort: 8080 \ No newline at end of file diff --git a/manifests/service.yml b/manifests/service.yml new file mode 100644 index 000000000..f2755aa7b --- /dev/null +++ b/manifests/service.yml @@ -0,0 +1,10 @@ +apiVersion: v1 +kind: Service +metadata: + name: mtailebpipelinesjavascriptdocker +spec: + type: LoadBalancer + ports: + - port: 8080 + selector: + app: mtailebpipelinesjavascriptdocker \ No newline at end of file