I am having a issue to where exactly i need to add test cases task in yaml file becausei building the Nodejs code in Docker can i add test cases test in Dockerfile or should i can add task after the Docker is that ok..
please give us some ideas how it will work
Yaml file:
trigger:
branches:
include:
- main
resources:
repositories:
- repository: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: 'xxxxxx9f-xxxxx-xxxxx-xxxx'
${{ if eq(variables['Build.SourceBranch'], 'refs/heads/main') }}:
imageRepository: 'jui-dev'
webAppName: 'itdev'
containerRegistry: 'jmrtvg.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push 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)
- task: AzureRmWebAppDeployment@4
inputs:
ConnectionType: 'AzureRM'
azureSubscription: JMsDe
appType: 'webAppContainer'
WebAppName: $(webAppName)
DockerNamespace: 'jM.azurecr.io'
DockerRepository: $(imageRepository)
DockerImageTag: '$(tag)'
Docker File:
FROM node:20.8.1 as node WORKDIR /app RUN npm cache clean --force COPY . . RUN npm install RUN npm run build --prod
#Stage: 2 FROM nginx:alpine COPY --from=node /app/dist/it-planning /usr/share/nginx/html
Some ideas how to write test cases in Docker file or yaml file
You need to run the Tests commands in the Dockerfile
itself then build it and push it in Azure Container Registry and use it for your Azure Web App Deployment.
My Dockerfile:-
FROM node:latest
# Set the working directory inside the container
WORKDIR /usr/src/app
COPY package*.json ./
# Install app dependencies
RUN npm install
# Copy the entire app directory into the container
COPY . .
# Run tests (replace 'test' with your actual test command)
RUN npm test
# Build the React app for production
RUN npm run build
# Expose the port used by the React app
EXPOSE 3000
# Start the React app when the container starts
CMD ["npm", "start"]
Dockerfile you can try:-
# Stage 1: Build the application
FROM node:20.8.1 as node
WORKDIR /app
COPY . .
RUN npm cache clean --force
RUN npm install
RUN npm run build --prod
# Stage 2: Run tests
FROM node:20.8.1 as test
WORKDIR /app
COPY . .
RUN npm install # Include test-specific dependencies if needed
RUN npm test # Run your test scripts here
# Stage 3: Finalize for production
FROM nginx:alpine
COPY --from=node /app/dist/it-planning /usr/share/nginx/html
My Azure Devops YAML code:-
trigger:
- main
resources:
- repo: self
variables:
# Container registry service connection established during pipeline creation
dockerRegistryServiceConnection: '256dd76d-688e-425b-9435-f9c25e5d9faa'
imageRepository: 'myreactapp'
containerRegistry: 'siliconacr.azurecr.io'
dockerfilePath: '$(Build.SourcesDirectory)/Dockerfile'
tag: '$(Build.BuildId)'
# Agent VM image name
vmImageName: 'ubuntu-latest'
stages:
- stage: Build
displayName: Build and push 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)
Output:-