diff --git a/src/configure/templates/azurePipelineTemplates/aks.yml b/src/configure/templates/azurePipelineTemplates/aks.yml new file mode 100644 index 00000000..d649b386 --- /dev/null +++ b/src/configure/templates/azurePipelineTemplates/aks.yml @@ -0,0 +1,192 @@ +# Kubernetes Service on Azure +# Build an image and deploy it to Azure as Kubernetes service. +# Add steps that analyze code, save build artifacts, deploy, and more: +# https://docs.microsoft.com/en-us/azure/devops/pipelines/languages/dotnet-core + +trigger: +- {{{ sourceRepository.branch }}} + +variables: + # Azure Resource Manager connection created during pipeline creation + azureSubscription: '{{{ assets.serviceConnectionId }}}' + + # Kubernetes Service name + serviceName: '{{ #sanitizeString }} {{{ inputs.AKSresource.name }}} {{ /sanitizeString }}' + + # Agent VM image name + vmImageName: 'ubuntu-latest' + + containerRegistry: '{{{ inputs.containerRegistry }}}.azurecr.io' + kubernetesServiceConnection: '{{{ assets.kubernetesServiceConnection }}}' + namespace: '$(serviceName) {{#tinyguid}}{{/tinyguid}}' + appName: '$(serviceName)_{{#tinyguid}}{{/tinyguid}}' + dockerAuthSecretName: '$(serviceName)dockerauth' + imageName: "$(serviceName){{#tinyguid }}{{ /tinyguid }}" + + # Working Directory + workingDirectory: '{{{ workingDirectory }}}' + + # Build Projects + dockerFile: "$(workingDirectory)/Dockerfile" + + +stages: +- stage: Build + displayName: Build stage + + jobs: + - job: Build + displayName: Build + pool: + vmImage: $(vmImageName) + + steps: + + - task: Docker@1 + displayName: 'Build an image' + inputs: + azureSubscriptionEndpoint: $(azureSubscription) + azureContainerRegistry: $(containerRegistry) + dockerFile: $(dockerFile) + imageName: $(imageName) + useDefaultContext: false + buildContext: Application + + - task: Docker@1 + displayName: 'Push an image' + inputs: + azureSubscriptionEndpoint: $(azureSubscription) + azureContainerRegistry: $(containerRegistry) + command: 'Push an image' + imageName: $(imageName) + + +- stage: Deploy + displayName: Deploy stage + dependsOn: Build + condition: succeeded() + + jobs: + - deployment: Deploy + displayName: Deploy + environment: $(appName) + pool: + vmImage: $(vmImageName) + strategy: + runOnce: + deploy: + steps: + - task: Kubernetes@1 + displayName: 'kubectl set imagePullSecrets' + inputs: + kubernetesServiceEndpoint: $(kubernetesServiceConnection) + namespace: $(namespace) + command: get + arguments: service + azureSubscriptionEndpointForSecrets: $(azureSubscription) + azureContainerRegistry: $(containerRegistry) + secretName: $(dockerAuthSecretName) + versionSpec: 1.10.12 + + - powershell: | + '# $(appName)/deployment.yaml + apiVersion: apps/v1 + kind: Deployment + metadata: + name: $(appName) + labels: + app: $(appName) + spec: + replicas: 2 + selector: + matchLabels: + app: $(appName) + template: + metadata: + labels: + app: $(appName) + spec: + containers: + - name: $(appName) + image: $(imageName) + ports: + - name: http + containerPort: {{{ inputs.containerPort }}} + protocol: TCP + + --- + + {{#if}} {{{inputs.AKSresource.properties.addonProfiles.httpapplicationrouting.enabled}}} + + # $(appName)/service.yaml + apiVersion: v1 + kind: Service + metadata: + name: $(appName) + labels: + app: $(appName) + spec: + type: ClusterIP + ports: + - port: {{{ inputs.containerPort }}} + targetPort: {{{ inputs.containerPort }}} + protocol: TCP + name: http + selector: + app: $(appName) + + --- + + # $(appName)/ingress.yaml + apiVersion: extensions/v1beta1 + kind: Ingress + metadata: + name: $(appName) + labels: + app: $(appName) + annotations: + kubernetes.io/ingress.class: addon-http-application-routing + spec: + rules: + - host: $(namespace)-$(appName).{{{inputs.AKSresource.properties.fqdn}}} + http: + paths: + - path: / + backend: + serviceName: $(appName) + servicePort: {{{ inputs.containerPort }}}' + + {{#else}} + + # $(appName)/service.yaml + apiVersion: v1 + kind: Service + metadata: + name: $(appName) + labels: + app: $(appName) + spec: + type: LoadBalancer + ports: + - port: {{{ inputs.containerPort }}} + targetPort: http + protocol: TCP + name: http + selector: + app: $(appName)' + + {{/if}} + + | + + Set-Content $(Agent.TempDirectory)/manifest.yaml + displayName: 'Generate Kubernetes Manifest file' + + - task: KubernetesManifest@0 + displayName: 'Kubernetes Manifest Deploy' + inputs: + kubernetesServiceConnection: $(kubernetesServiceConnection) + namespace: '$(namespace)' + manifests: '$(Agent.TempDirectory)/manifest.yaml' + imagePullSecrets: '$(dockerAuthSecretName)' + diff --git a/src/configure/templates/githubWorkflowTemplates/aksGithub.yml b/src/configure/templates/githubWorkflowTemplates/aksGithub.yml new file mode 100644 index 00000000..b11f1049 --- /dev/null +++ b/src/configure/templates/githubWorkflowTemplates/aksGithub.yml @@ -0,0 +1,134 @@ +on: [push] +jobs: + build-and-deploy: + env: + REGISTRY_URL: {{#toLower}} {{{inputs.containerRegistry}}} {{/toLower}}.azurecr.io + SERVICE_NAME: {{#sanitizeString}} {{{ inputs.AKSresource.name 50}}} {{/sanitizeString}} + NAMESPACE: $SERVICE_NAME{{#tinyguid}}{{/tinyguid}} + IMAGE_NAME: $SERVICE_NAME{{#tinyguid}}{{/tinyguid}} + APP_NAME: $(SERVICE_NAME)_{{#tinyguid}}{{/tinyguid}} + SECRET_NAME: {{#sanitizeString}} {{{ inputs.AKSresource.name 12}}} {{/sanitizeString}}dockerauth + + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + + - uses: azure/docker-login@v1 + with: + login-server: $REGISTRY_URL + username: ${{ assets.REGISTRY_USERNAME }} + password: ${{ assets.REGISTRY_PASSWORD }} + + - name: Build and push image to ACR + id: build-image + run: | + docker build "$GITHUB_WORKSPACE/{{ workingDirectory }}" -f "{{ workingDirectory}}/Dockerfile" -t $REGISTRY_URL/$IMAGE_NAME:{{=<% %>=}}${{ <% github.sha %> }}<%={{ }}=%> + docker push $REGISTRY_URL/$IMAGE_NAME:{{=<% %>=}}${{ <% github.sha %> }}<%={{ }}=%> + + - uses: azure/k8s-set-context@v1 + with: + kubeconfig: ${{ secrets.KUBE_CONFIG }} + id: login + + - name: Create namespace + run: | + namespacePresent=`kubectl get namespace | grep $NAMESPACE | wc -l` + if [ $namespacePresent -eq 0 ] + then + echo `kubectl create namespace $NAMESPACE` + fi + + - uses: azure/k8s-create-secret@v1 + with: + namespace: $NAMESPACE + container-registry-url: $REGISTRY_URL + container-registry-username: ${{ assets.REGISTRY_USERNAME }} + container-registry-password: ${{ assets.REGISTRY_PASSWORD }} + secret-name: $SECRET_NAME + + - name: Generate Kubernetes Manifest file + id: manifest-creation + run: | + echo "apiVersion : apps/v1beta1 + kind: Deployment + metadata: + name: "$APP_NAME" + spec: + replicas: 2 + template: + metadata: + labels: + app: "$APP_NAME" + spec: + containers: + - name: "$APP_NAME" + image: "$REGISTRY_URL/$IMAGE_NAME" + ports: + - containerPort: {{{ inputs.containerPort }}} + + {{#if}} {{{inputs.AKSresource.properties.addonProfiles.httpapplicationrouting.enabled}}} + + apiVersion: v1 + kind: Service + metadata: + name: "$APP_NAME" + labels: + app: "$APP_NAME" + spec: + type: ClusterIP + ports: + - port: {{{ inputs.containerPort }}} + targetPort: {{{ inputs.containerPort }}} + protocol: TCP + name: http + selector: + app: "$APP_NAME" + + apiVersion: extensions/v1beta1 + kind: Ingress + metadata: + name: "$APP_NAME" + labels: + app: "$APP_NAME" + annotations: + kubernetes.io/ingress.class: addon-http-application-routing + spec: + rules: + - host: $NAMESPACE-$APP_NAME.{{{inputs.AKSresource.properties.fqdn}}} + http: + paths: + - path: / + backend: + serviceName: "$APP_NAME" + servicePort: {{{ inputs.containerPort }}}" + + {{#else}} + + apiVersion: v1 + kind: Service + metadata: + name: "$APP_NAME" + labels: + app: "$APP_NAME" + spec: + type: LoadBalancer + ports: + - port: {{{ inputs.containerPort }}} + targetPort: http + protocol: TCP + name: http + selector: + app: "$APP_NAME" + + {{/if}}" > manifest_{{{IMAGE_NAME}}}.yml + + + - uses: azure/k8s-deploy@v1 + with: + namespace: $NAMESPACE + manifests: | + {{{ GITHUB_WORKSPACE/manifest_{{{IMAGE_NAME}}}.yml }}} + images: | + $REGISTRY_URL/$IMAGE_NAME:{{=<% %>=}}${{ <% github.sha %> }}<%={{ }}=%> + imagepullsecrets: | + $SECRET_NAME \ No newline at end of file