Here is a Kubernetes YAML file that should meet your requirements. Replace the placeholders with your specific values:
yamlapiVersion: v1
kind: Secret
metadata:
name: acr-auth
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: <your-base64-encoded-docker-credentials>
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: code-and-artifacts-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: batch/v1
kind: Job
metadata:
name: build-job
spec:
template:
spec:
initContainers:
- name: git-clone
image: alpine/git:latest
command: ["git", "clone", "--single-branch", "<your-azure-devops-git-repo-url>", "/code"]
volumeMounts:
- name: code-and-artifacts
mountPath: /code
containers:
- name: build-container
image: <your-acr-name>.azurecr.io/<your-image-name>:<your-image-tag>
resources:
requests:
cpu: 500m
memory: 1Gi
env:
- name: AZURE_DEVOPS_TOKEN
valueFrom:
secretKeyRef:
name: acr-auth
key: .dockerconfigjson
volumeMounts:
- name: code-and-artifacts
mountPath: /code
subPath: code
- name: code-and-artifacts
mountPath: /artifacts
subPath: artifacts
volumes:
- name: code-and-artifacts
persistentVolumeClaim:
claimName: code-and-artifacts-pvc
restartPolicy: Never
Replace the placeholders in the YAML:
<your-base64-encoded-docker-credentials>
: Your base64-encoded Docker credentials for accessing the Azure Container Registry.<your-azure-devops-git-repo-url>
: The URL of your Azure DevOps Git repository.<your-acr-name>
: Your Azure Container Registry name.<your-image-name>
: The name of the image you want to use from the Azure Container Registry.<your-image-tag>
: The image tag you want to use from the Azure Container Registry.
This YAML file includes:
- A Secret for authenticating with the Azure Container Registry.
- A PersistentVolumeClaim with 10Gi of storage.
- A Job with an init container that clones the code from the Azure DevOps Git repository.
- A main container that pulls the image from the Azure Container Registry and has access to the code and artifacts folders.
- Mounting the PersistentVolumeClaim to the init and main containers for code and artifacts storage.
Make sure to adjust the resources requests for the main container based on your application's specific needs. The example provided in the YAML assumes 500m CPU and 1Gi of memory, which may need to be adjusted depending on your use case.
To use a Personal Access Token (PAT) for the init container to clone the code from Azure DevOps Services, you can create a Kubernetes Secret with the PAT and then use it as an environment variable in the init container. Here is an updated Kubernetes YAML file:
yamlapiVersion: v1
kind: Secret
metadata:
name: acr-auth
type: kubernetes.io/dockerconfigjson
data:
.dockerconfigjson: <your-base64-encoded-docker-credentials>
---
apiVersion: v1
kind: Secret
metadata:
name: azure-devops-pat
type: Opaque
data:
pat: <your-base64-encoded-azure-devops-pat>
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: code-and-artifacts-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 10Gi
---
apiVersion: batch/v1
kind: Job
metadata:
name: build-job
spec:
template:
spec:
initContainers:
- name: git-clone
image: alpine/git:latest
command: ["/bin/sh", "-c"]
args:
- git clone --single-branch https://<your-azure-devops-username>:$PAT@<your-azure-devops-git-repo-url> /code
env:
- name: PAT
valueFrom:
secretKeyRef:
name: azure-devops-pat
key: pat
volumeMounts:
- name: code-and-artifacts
mountPath: /code
containers:
- name: build-container
image: <your-acr-name>.azurecr.io/<your-image-name>:<your-image-tag>
resources:
requests:
cpu: 500m
memory: 1Gi
env:
- name: AZURE_DEVOPS_TOKEN
valueFrom:
secretKeyRef:
name: acr-auth
key: .dockerconfigjson
volumeMounts:
- name: code-and-artifacts
mountPath: /code
subPath: code
- name: code-and-artifacts
mountPath: /artifacts
subPath: artifacts
volumes:
- name: code-and-artifacts
persistentVolumeClaim:
claimName: code-and-artifacts-pvc
restartPolicy: Never
Replace the placeholders in the YAML:
<your-base64-encoded-docker-credentials>
: Your base64-encoded Docker credentials for accessing the Azure Container Registry.<your-base64-encoded-azure-devops-pat>
: Your base64-encoded Azure DevOps Personal Access Token.<your-azure-devops-username>
: Your Azure DevOps username.<your-azure-devops-git-repo-url>
: The URL of your Azure DevOps Git repository (without the protocol and username part).<your-acr-name>
: Your Azure Container Registry name.<your-image-name>
: The name of the image you want to use from the Azure Container Registry.<your-image-tag>
: The image tag you want to use from the Azure Container Registry.
This updated YAML file includes a new Secret named azure-devops-pat
to store the Azure DevOps PAT. The PAT is then passed as an environment variable to the init container, which uses it to clone the code from the Azure DevOps Git repository.
As a technical writer, I have provided instructions and a sample Azure Pipelines YAML file that uses a custom pipeline task to run a build job on a Kubernetes-based cluster. The user will be able to define all the necessary placeholders to configure the pipeline task.
To run a build job on a Kubernetes-based cluster from Azure Pipelines, you can use the custom pipeline task named KubernetesBuildJob
. This task will create a Kubernetes Job using the provided configuration and return the results, such as logs and artifacts.
To get started, you'll need to include the KubernetesBuildJob
task in your Azure Pipelines YAML file. The following is a sample pipeline that demonstrates how to use the task:
yamltrigger:
branches:
include:
- main
pool:
vmImage: 'ubuntu-latest'
steps:
- task: KubernetesBuildJob@1
inputs:
dockerCredentials: <your-base64-encoded-docker-credentials>
azureDevOpsPAT: <your-base64-encoded-azure-devops-pat>
azureDevOpsUsername: <your-azure-devops-username>
azureDevOpsGitRepoUrl: <your-azure-devops-git-repo-url>
acrName: <your-acr-name>
imageName: <your-image-name>
imageTag: <your-image-tag>
cpuRequest: 500m
memoryRequest: 1Gi
Replace the placeholders in the YAML:
<your-base64-encoded-docker-credentials>
: Your base64-encoded Docker credentials for accessing the Azure Container Registry.<your-base64-encoded-azure-devops-pat>
: Your base64-encoded Azure DevOps Personal Access Token.<your-azure-devops-username>
: Your Azure DevOps username.<your-azure-devops-git-repo-url>
: The URL of your Azure DevOps Git repository.<your-acr-name>
: Your Azure Container Registry name.<your-image-name>
: The name of the image you want to use from the Azure Container Registry.<your-image-tag>
: The image tag you want to use from the Azure Container Registry.cpuRequest
: The requested CPU for the build container. Adjust this value based on your application's specific needs.memoryRequest
: The requested memory for the build container. Adjust this value based on your application's specific needs.
After you have updated the placeholders with your specific values, commit the YAML file to your Azure DevOps Git repository. The pipeline will be triggered when changes are pushed to the main
branch. The KubernetesBuildJob
task will run the build job on your Kubernetes cluster, and the results (logs and artifacts) will be returned and made available in the pipeline.