Skip to content

Commit

Permalink
Dynamic image tagging using PR-specific variables in Azure DevOps pip…
Browse files Browse the repository at this point in the history
…eline.

Fix: Add missing 'tag' parameter to ACR push template in Azure DevOps pipeline

Modified the tag to trigger the pipeline using the GitHub PR

Modified the tag to trigger the pipeline using the GitHub PR

Modified the tag to trigger the pipeline using the GitHub PR

Fix: Use GitHub PR number and commit SHA for dynamic tagging in Azure DevOps pipeline

Fix: Use GitHub PR number and commit SHA for dynamic tagging in Azure DevOps pipeline

Fix: Use GitHub PR number and commit SHA for dynamic tagging in Azure DevOps pipeline

Fix: Use GitHub PR number and commit SHA for dynamic tagging in Azure DevOps pipeline

Fix pipeline to dynamically generate valid Docker image tags using PR ID and commit hash with fallback mechanisms

Fix pipeline to dynamically generate valid Docker image tags using PR ID and commit hash with fallback mechanisms

Fix pipeline to dynamically generate valid Docker image tags using PR ID and commit hash with fallback mechanisms

Fix pipeline to dynamically generate valid Docker image tags using PR ID and commit hash with fallback mechanisms

Fix pipeline to dynamically generate valid Docker image tags using PR ID and commit hash with fallback mechanisms

Fix pipeline to dynamically generate valid Docker image tags using PR ID and commit hash with fallback mechanisms

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch

Added a new Extract_PR_Number job to parse the PR number from System.PullRequest.SourceBranch
  • Loading branch information
shubhadapaithankar committed Nov 23, 2024
1 parent ed9f9c9 commit c37df82
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 56 deletions.
81 changes: 55 additions & 26 deletions .pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,56 @@ variables:
value: arosvcdev.azurecr.io/vpn
- name: LOCAL_E2E_IMAGE
value: arosvcdev.azurecr.io/e2e
- name: TAG
value: $(Build.BuildId)
- name: VERSION
value: $(Build.BuildId)
value: $(TAG)
- name: TAG
value: pr-$(System.PullRequest.PullRequestId)-$(System.PullRequest.SourceCommitId)
- name: ARO_IMAGE
value: arosvcdev.azurecr.io/aro:$(Build.BuildId)
value: arosvcdev.azurecr.io/aro:$(TAG)
- name: ARO_SELENIUM_HOSTNAME
value: localhost
- name: E2E_LABEL
value: "!smoke&&!regressiontest"

stages:
- stage: Containerized_CI
variables:
- name: TAG
value: ""
jobs:
- job: Generate_Tag
pool:
name: 1es-aro-ci-pool
steps:
# Debugging: Output available variables
- bash: |
echo "DEBUG: Inspecting variables..."
echo "System.PullRequest.PullRequestId: $(System.PullRequest.PullRequestId)"
echo "System.PullRequest.SourceBranch: $(System.PullRequest.SourceBranch)"
echo "System.PullRequest.SourceCommitId: $(System.PullRequest.SourceCommitId)"
echo "##vso[task.logissue type=warning] Pull Request ID: $(System.PullRequest.PullRequestId)"
echo "##vso[task.logissue type=warning] Source Branch: $(System.PullRequest.SourceBranch)"
displayName: Debug Variables
# Generate TAG using sanitized branch name and commit SHA
- bash: |
echo "Generating TAG variable..."
# Sanitize branch name
SANITIZED_BRANCH=$(echo $(System.PullRequest.SourceBranch) | sed 's/[^a-zA-Z0-9]/-/g')
SHORT_SHA=$(echo $(System.PullRequest.SourceCommitId) | cut -c1-8)
TAG=${SANITIZED_BRANCH}-${SHORT_SHA}
echo "Generated TAG: $TAG"
# Set TAG variable
echo "##vso[task.setvariable variable=TAG]$TAG"
displayName: Generate TAG
- job: Build_Test_And_Push_Az_ARO_Extension
dependsOn: Generate_Tag
variables:
TAG: $(TAG)
pool:
name: 1es-aro-ci-pool
steps:
Expand All @@ -48,17 +83,21 @@ stages:
# Build and test the Az ARO Extension
- script: |
set -xe
DOCKER_BUILD_CI_ARGS="--load" make ci-azext-aro VERSION=$(VERSION)
DOCKER_BUILD_CI_ARGS="--load" make ci-azext-aro VERSION=$(TAG)
displayName: 🛠 Build & Test Az ARO Extension
# Push the image to ACR
- template: ./templates/template-acr-push.yml
parameters:
acrFQDN: 'arosvcdev.azurecr.io'
repository: 'azext-aro'
pushLatest: true
tag: $(TAG)
pushLatest: false

- job: Build_And_Test_RP_And_Portal
dependsOn: Generate_Tag
variables:
TAG: $(TAG)
pool:
name: 1es-aro-ci-pool
steps:
Expand All @@ -67,33 +106,21 @@ stages:
# Build and test RP and Portal
- script: |
set -xe
DOCKER_BUILD_CI_ARGS="--load" make ci-rp VERSION=$(VERSION)
DOCKER_BUILD_CI_ARGS="--load" make ci-rp VERSION=$(TAG)
displayName: 🛠 Build & Test RP and Portal
# Publish test results
- task: PublishTestResults@2
displayName: 📊 Publish tests results
inputs:
testResultsFiles: $(System.DefaultWorkingDirectory)/report.xml
condition: succeededOrFailed()

# Publish code coverage results
- task: PublishCodeCoverageResults@2
displayName: 📈 Publish code coverage
inputs:
codeCoverageTool: Cobertura
summaryFileLocation: $(System.DefaultWorkingDirectory)/coverage.xml
failIfCoverageEmpty: false
condition: succeededOrFailed()

# Push the RP image to ACR
- template: ./templates/template-acr-push.yml
parameters:
acrFQDN: 'arosvcdev.azurecr.io'
repository: 'aro'
pushLatest: true
tag: $(TAG)
pushLatest: false

- job: Build_And_Push_E2E_Image
dependsOn: Generate_Tag
variables:
TAG: $(TAG)
pool:
name: 1es-aro-ci-pool
steps:
Expand All @@ -102,15 +129,17 @@ stages:
# Build the E2E image
- script: |
set -xe
DOCKER_BUILD_CI_ARGS="--load" make aro-e2e VERSION=$(VERSION)
DOCKER_BUILD_CI_ARGS="--load" make aro-e2e VERSION=$(TAG)
displayName: 🛠 Build the E2E image
# Push the E2E image to ACR
- template: ./templates/template-acr-push.yml
parameters:
acrFQDN: 'arosvcdev.azurecr.io'
repository: 'e2e'
pushLatest: true
tag: $(TAG)
pushLatest: false


- stage: E2E # E2E Stage using Docker Compose
dependsOn: Containerized_CI
Expand Down
43 changes: 13 additions & 30 deletions .pipelines/templates/template-acr-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ parameters:
type: string
- name: repository # This is both the ACR and local repository name
type: string # The local and ACR image repository name
- name: tag # The tag for the image
type: string
- name: pushLatest
type: boolean
default: false
Expand Down Expand Up @@ -48,44 +50,25 @@ steps:
echo "Listing Docker images..."
docker images
# Define both the full repository image name and the local name
IMAGE_NAME="${ACR_FQDN}/${{ parameters.repository }}:$(VERSION)"
LOCAL_IMAGE="${{ parameters.repository }}:$(VERSION)"
# Define the full repository image name
IMAGE_NAME="${ACR_FQDN}/${{ parameters.repository }}:${{ parameters.tag }}"
# Check if the image exists locally with the full repository tag
echo "Checking for image $IMAGE_NAME..."
# Check if the local image exists
echo "Checking for image ${IMAGE_NAME}..."
if [[ "$(docker images -q $IMAGE_NAME 2> /dev/null)" == "" ]]; then
# If the full repository tagged image does not exist, check for the local image
echo "Full repository image not found. Checking for local image $LOCAL_IMAGE..."
if [[ "$(docker images -q $LOCAL_IMAGE 2> /dev/null)" == "" ]]; then
echo "Error: Neither $IMAGE_NAME nor $LOCAL_IMAGE found. Exiting."
exit 1
else
# Retag the local image with the full repository path
echo "Local image $LOCAL_IMAGE found. Retagging with full repository path..."
docker tag $LOCAL_IMAGE $IMAGE_NAME
fi
else
echo "Image $IMAGE_NAME found. Proceeding to push..."
fi
# Ensure the image is available locally before tagging 'latest'
IMAGE_LATEST="${ACR_FQDN}/${{ parameters.repository }}:latest"
echo "Checking for image $IMAGE_LATEST..."
if [[ "$(docker images -q $IMAGE_LATEST 2> /dev/null)" == "" ]]; then
echo "Warning: Image $IMAGE_LATEST not found. Skipping 'latest' tag."
SKIP_LATEST=true
echo "Error: Image $IMAGE_NAME not found locally. Exiting."
exit 1
else
echo "Image $IMAGE_LATEST found. Proceeding with 'latest' tag."
SKIP_LATEST=false
echo "Image $IMAGE_NAME found locally. Proceeding to push..."
fi
# Push the Docker image to ACR with the build ID
echo "Pushing image with build ID to ACR..."
# Push the Docker image to ACR with the specified tag
echo "Pushing image ${IMAGE_NAME} to ACR..."
docker push $IMAGE_NAME
# Optionally push the image as 'latest'
if [ "${{ parameters.pushLatest }}" == "true" ] && [ "$SKIP_LATEST" == "false" ]; then
if [ "${{ parameters.pushLatest }}" == "true" ]; then
IMAGE_LATEST="${ACR_FQDN}/${{ parameters.repository }}:latest"
echo "Tagging image with 'latest' and pushing..."
docker tag $IMAGE_NAME $IMAGE_LATEST
docker push $IMAGE_LATEST
Expand Down

0 comments on commit c37df82

Please sign in to comment.