Skip to content

Commit

Permalink
Added the docker logic
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhadapaithankar committed Sep 13, 2024
1 parent 3c0c875 commit 0f78704
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 282 deletions.
93 changes: 67 additions & 26 deletions .pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,44 +36,83 @@ variables:
- template: vars.yml

stages:
- stage: Containerized
dependsOn: []
- stage: NotContainerized
dependsOn: Containerized
jobs:
- template: templates/template-podman.yml
parameters:
jobName: 'Build_Test_And_Push_Az_ARO_Extension'
makeCommand: 'make ci-azext-aro'
repository: 'azext-aro' # Use a static value or correct repository name
acrFQDN: 'arosvcdev.azurecr.io'
imageRepoName: 'azext-aro'
pushLatest: true

# Build, Lint, and Test RP and Portal (with coverage publishing inside this template)
- template: templates/template-podman.yml
parameters:
jobName: 'Build_Lint_Test_RP_And_Portal'
makeCommand: 'make ci-rp'
repository: 'aro' # Use a static value or correct repository name
acrFQDN: 'arosvcdev.azurecr.io'
imageRepoName: 'aro'
pushLatest: true

- job: Lint_Az_ARO_Extension
dependsOn: Build_Test_And_Push_Az_ARO_Extension
- job: Python_Unit_Tests
pool:
name: 1es-aro-ci-pool
variables:
HOME: $(Agent.BuildDirectory)

steps:
- template: templates/template-checkout.yml
- template: ./templates/template-checkout.yml
- script: |
set -xe
export AZDEV_CONFIG_DIR=$(Agent.BuildDirectory)/azdev-config
make test-python
[[ -z "$(git status -s)" ]]
displayName: 🧪Run Python Unit Tests
target: python
- stage: Containerized-docker
- job: Golang_Unit_Tests
pool:
name: 1es-aro-ci-pool
variables:
GOCACHE: /tmp/gocache
steps:
- template: ./templates/template-checkout.yml

- script: |
set -xe
go version
go env
displayName: Print Go version & env
target: golang
- script: |
echo "##vso[task.prependpath]$(go env GOPATH)/bin"
displayName: Add GOBIN to path
target: golang
- script: |
set -xe
# Required for podman 5
sudo tdnf install -y gpgme-devel lvm2-devel btrfs-progs-devel golang-1.21.11-1.cm2
make generate
[[ -z "$(git status -s)" ]]
displayName: ⚙️ Run Golang code generate
target: golang
- script: |
set -xe
make build-all
[[ -z "$(git status -s)" ]]
displayName: 🕵️ Build Golang code
target: golang
- script: |
set -xe
make unit-test-go
displayName: 🧪 Run Golang unit tests
target: golang
- script: |
make xmlcov
displayName: ⚙️ Process Reports
condition: succeededOrFailed()
target: golang
- job: Lint_Admin_Portal
pool:
name: 1es-aro-ci-pool
steps:
- template: ./templates/template-checkout.yml
- script: |
set -xe
make lint-admin-portal
displayName: 🧹 Lint Admin Portal
- stage: Containerized
dependsOn: []
jobs:
- template: templates/template-docker.yml
Expand All @@ -83,6 +122,7 @@ stages:
repository: 'azext-aro' # Use a static value or correct repository name
acrFQDN: 'arosvcdev.azurecr.io'
imageRepoName: 'azext-aro'
dockerFilePath: 'Dockerfile.ci-azext-aro'
pushLatest: true

# Build, Lint, and Test RP and Portal (with coverage publishing inside this template)
Expand All @@ -93,6 +133,7 @@ stages:
repository: 'aro' # Use a static value or correct repository name
acrFQDN: 'arosvcdev.azurecr.io'
imageRepoName: 'aro'
dockerFilePath: 'Dockerfile.ci-rp'
pushLatest: true

- job: Lint_Az_ARO_Extension
Expand Down
138 changes: 55 additions & 83 deletions .pipelines/templates/template-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ parameters:
type: string
- name: imageRepoName
type: string
- name: dockerFilePath # Parameter to specify the Dockerfile path
type: string
- name: pushLatest
type: boolean
default: false
- name: registry
type: string
default: "registry.access.redhat.com"

jobs:
- job: ${{ parameters.jobName }}
Expand All @@ -20,105 +25,70 @@ jobs:
variables:
ACR_FQDN: ${{ parameters.acrFQDN }}
REPOSITORY: ${{ parameters.repository }}
IMAGE_TAG: $(Build.BuildId) # Include the unique BuildId or another identifier
IMAGE_TAG: $(Build.BuildId)

steps:
# Checkout source code
- template: ../templates/template-checkout.yml

# Pin Docker Version and Install Docker
- task: DockerInstaller@0
displayName: 'Install Docker'
inputs:
dockerVersion: '20.10.7'

# Build the Docker image and pass the correct REGISTRY argument
- task: Docker@2
displayName: 'Build Docker Image'
inputs:
command: build
repository: ${{ parameters.imageRepoName }}
Dockerfile: ${{ parameters.dockerFilePath }}
tags: |
$(Build.BuildId)
latest
buildContext: $(System.DefaultWorkingDirectory)
arguments: --build-arg REGISTRY=${{ parameters.registry }} # Pass the REGISTRY as a build argument

# Authenticate to ACR and push the image
- task: AzureCLI@2
displayName: 'Authenticate to Azure and Push Docker Image'
inputs:
azureSubscription: 'ado-pipeline-dev-image-push' # Service connection name
scriptType: 'bash'
scriptLocation: 'inlineScript'
azureSubscription: 'ado-pipeline-dev-image-push' # service connection
scriptType: bash
scriptLocation: inlineScript
inlineScript: |
echo "Retrieving ACR token..."
REGISTRY_NAME=${ACR_FQDN%%.*}
if [ -z "$REGISTRY_NAME" ]; then
echo "Error: ACR_FQDN is not set correctly."
exit 1
fi
TOKEN=$(az acr login --name $REGISTRY_NAME --expose-token --output tsv --query accessToken)
if [ -z "$TOKEN" ]; then
echo "Error: Failed to retrieve ACR token."
exit 1
fi
echo "##vso[task.setvariable variable=ACR_TOKEN]$TOKEN"
- script: |
set -xe
export NO_CACHE=true
export TOKEN=$(ACR_TOKEN)
export REPOSITORY=${{ parameters.repository }}
export IMAGE_REPO_NAME=${{ parameters.imageRepoName }}
# Validate repository is not empty
if [ -z "${REPOSITORY}" ]; then
echo "Error: REPOSITORY is not set."
exit 1
fi
# Install Docker, make, azure-cli, and Go
echo "Installing Docker, Make, Azure CLI, and Go..."
sudo apt-get update
sudo apt-get install -y docker.io make azure-cli
# Log in to ACR using Docker
echo "Logging into ACR with Docker..."
echo $TOKEN | docker login ${ACR_FQDN} -u "00000000-0000-0000-0000-000000000000" --password-stdin
# Check if login was successful
if [ $? -ne 0 ]; then
echo "Error: Docker login failed."
exit 1
fi
echo "Docker login successful."
echo "Authenticating to Azure and ACR..."
ACR_FQDN="${{ parameters.acrFQDN }}"
REGISTRY_NAME=$(echo $ACR_FQDN | cut -d'.' -f1)
# Run the build and capture the image tag
echo "Running the build and capturing the image tag..."
IMAGE_TAG=$(eval "${{ parameters.makeCommand }}" | grep 'Successfully tagged' | awk '{print $3}' | cut -d':' -f2)
docker images
# Login to ACR
az acr login --name $REGISTRY_NAME
if [ -z "$IMAGE_TAG" ]; then
echo "Error: Failed to capture IMAGE_TAG."
exit 1
fi
# Tag the image with the ACR repository
docker tag ${{ parameters.imageRepoName }}:$(Build.BuildId) ${ACR_FQDN}/${{ parameters.repository }}:$(Build.BuildId)
docker tag ${{ parameters.imageRepoName }}:latest ${ACR_FQDN}/${{ parameters.repository }}:latest
echo "Captured IMAGE_TAG: $IMAGE_TAG"
# Push the Docker image to ACR
docker push ${ACR_FQDN}/${{ parameters.repository }}:$(Build.BuildId)
# Retag the image with the ACR repository
echo "Tagging the image with ACR repository..."
docker tag ${IMAGE_REPO_NAME}:${IMAGE_TAG} ${ACR_FQDN}/${REPOSITORY}:${IMAGE_TAG}
# Push the image to ACR
echo "Pushing image to ACR..."
docker push ${ACR_FQDN}/${REPOSITORY}:${IMAGE_TAG}
if [ $? -ne 0 ]; then
echo "Error: Docker push failed."
exit 1
fi
# Optionally tag and push the image as 'latest'
if [ ${{ parameters.pushLatest }} == true ]; then
echo "Tagging and pushing latest image..."
docker tag ${ACR_FQDN}/${REPOSITORY}:${IMAGE_TAG} ${ACR_FQDN}/${REPOSITORY}:latest
docker push ${ACR_FQDN}/${REPOSITORY}:latest
fi
# Optionally push the image as 'latest'
if [ "${{ parameters.pushLatest }}" == "true" ]; then
docker push ${ACR_FQDN}/${{ parameters.repository }}:latest
fi
# Clear Azure session AFTER all ACR operations are complete
# Clear Azure session after all ACR operations are complete
- script: |
echo "Clearing Azure session..."
az account clear
target: ubuntu
displayName: 'Clear Azure Session'
# Only run the following steps for 'Build_Lint_Test_RP_And_Portal' job
# Additional steps for specific job (e.g., 'Build_Lint_Test_RP_And_Portal')
- ${{ if eq(parameters.jobName, 'Build_Lint_Test_RP_And_Portal') }}:
# Verify that the files exist
- script: |
echo "Verifying existence of test results and coverage files..."
ls -al $(System.DefaultWorkingDirectory) # Log files in the working directory
ls -al $(System.DefaultWorkingDirectory)
if [ ! -f "$(System.DefaultWorkingDirectory)/report.xml" ]; then
echo "Test result file report.xml not found."
exit 1
Expand All @@ -128,15 +98,17 @@ jobs:
exit 1
fi
displayName: "Verify Test Results and Coverage Files"
target: ubuntu
target: ubi8
# Publish test results
- task: PublishTestResults@2
displayName: 📊 Publish tests results
inputs:
testResultsFiles: $(System.DefaultWorkingDirectory)/report.xml
condition: succeededOrFailed()
target: ubuntu
target: ubi8

# Publish code coverage results
- task: PublishCodeCoverageResults@2
displayName: 📈 Publish code coverage
inputs:
Expand Down
Loading

0 comments on commit 0f78704

Please sign in to comment.