From 9ad0c77fb7e988495ab4443f36a7cd2019e2f679 Mon Sep 17 00:00:00 2001 From: mtaileb <116947757+mtaileb@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:22:52 +0100 Subject: [PATCH 1/5] Set up CI with Azure Pipelines [skip ci] --- azure-pipelines.yml | 142 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 azure-pipelines.yml diff --git a/azure-pipelines.yml b/azure-pipelines.yml new file mode 100644 index 000000000..9cc209c8e --- /dev/null +++ b/azure-pipelines.yml @@ -0,0 +1,142 @@ +# 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" From b3ca81dbc86bcbfa2ae8a9918946be00abe01ec8 Mon Sep 17 00:00:00 2001 From: mtaileb <116947757+mtaileb@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:22:53 +0100 Subject: [PATCH 2/5] Set up CI with Azure Pipelines [skip ci] --- manifests/deployment.yml | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 manifests/deployment.yml 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 From de40af8d45a2b90344f356bf50a98151cae8757b Mon Sep 17 00:00:00 2001 From: mtaileb <116947757+mtaileb@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:22:53 +0100 Subject: [PATCH 3/5] Set up CI with Azure Pipelines [skip ci] --- manifests/service.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 manifests/service.yml 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 From cab7afd8ff9d478efbcb965fcba6b2c021026171 Mon Sep 17 00:00:00 2001 From: mtaileb <116947757+mtaileb@users.noreply.github.com> Date: Tue, 5 Dec 2023 22:34:35 +0100 Subject: [PATCH 4/5] Update azure-pipelines.yml --- azure-pipelines.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9cc209c8e..67dacc73e 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -2,6 +2,7 @@ # Build and push image to Azure Container Registry; Deploy to Azure Kubernetes Service # https://docs.microsoft.com/azure/devops/pipelines/languages/docker + trigger: - main From 769b475b85574c07affd5eb7d66f9e019b72adfe Mon Sep 17 00:00:00 2001 From: mtaileb <116947757+mtaileb@users.noreply.github.com> Date: Wed, 6 Dec 2023 15:04:26 +0100 Subject: [PATCH 5/5] Update azure-pipelines.yml --- azure-pipelines.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 67dacc73e..d9567d95f 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -3,6 +3,7 @@ # https://docs.microsoft.com/azure/devops/pipelines/languages/docker + trigger: - main