Skip to content
This repository was archived by the owner on May 2, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions src/configure/templates/azurePipelineTemplates/aks.yml
Original file line number Diff line number Diff line change
@@ -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 }}'
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use inputs.aksCluster


# 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can build context always be 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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will this remain hard coded ?


- 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)'

134 changes: 134 additions & 0 deletions src/configure/templates/githubWorkflowTemplates/aksGithub.yml
Original file line number Diff line number Diff line change
@@ -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}}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inputs.aksCluster

use this where we need to access the cluster resource

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 }}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${{ assets.containerRegistryUsername }}

password: ${{ assets.REGISTRY_PASSWORD }}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${{ assets.containerRegistryPassword}}


- 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 }}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we also need to add logout in the end of workflow ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assets.aksKubeConfig

id: login

- name: Create namespace
run: |
namespacePresent=`kubectl get namespace | grep $NAMESPACE | wc -l`
if [ $namespacePresent -eq 0 ]
then
echo `kubectl create namespace $NAMESPACE`
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we assume that the namespace is created after running this command or do we need to wait/check again for confirmation ?

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