diff --git a/.azure-pipelines/ci-build.yml b/.azure-pipelines/ci-build.yml index 8a894c17836..e8545e45bca 100644 --- a/.azure-pipelines/ci-build.yml +++ b/.azure-pipelines/ci-build.yml @@ -27,12 +27,19 @@ variables: BuildAgent: ${{ parameters.BuildAgent }} GitUserEmail: "GraphTooling@service.microsoft.com" GitUserName: "Microsoft Graph DevX Tooling" + REGISTRY: 'msgraphprodregistry.azurecr.io' + IMAGE_NAME: 'public/microsoftgraph/powershell' + PREVIEW_BRANCH: 'refs/heads/main' # Updated to target your branch trigger: branches: include: - main - dev + tags: + include: + - v* + pr: branches: include: @@ -156,7 +163,79 @@ extends: nuGetFeedType: external publishFeedCredentials: 'microsoftgraph PowerShell Gallery connection' + - stage: PushDockerImageToRegistry + condition: and(or(startsWith(variables['Build.SourceBranch'], 'refs/tags/v'), eq(variables['Build.SourceBranch'], variables['PREVIEW_BRANCH'])), not(contains(variables['Build.SourceBranch'], '-preview'))) + dependsOn: [] + displayName: 'Push docker image' + jobs: + - job: PushDockerImage + displayName: 'Push docker image' + pool: + name: Azure-Pipelines-1ESPT-ExDShared + image: ubuntu-latest + os: linux + steps: + - checkout: self + + # Display the contents of the Build.SourcesDirectory + - bash: | + echo "Build.SourcesDirectory: $(Build.SourcesDirectory)" + ls -la $(Build.SourcesDirectory) + displayName: 'List contents of Build.SourcesDirectory' + - task: AzureCLI@2 + displayName: "Log in to Azure Container Registry" + inputs: + azureSubscription: 'ACR Images Push Service Connection' # service connection + scriptType: 'bash' + scriptLocation: 'inlineScript' + 'inlineScript': | + az acr login --name $(REGISTRY) + - powershell: | + $date = Get-Date -Format "yyyyMMdd" + Write-Host "##vso[task.setvariable variable=currentDate]$date" + + - script: | + docker run --privileged --rm tonistiigi/binfmt --install all + displayName: 'Enable multi-platform builds' + + - script: | + docker buildx create --use --name mybuilder + displayName: 'Set up Docker BuildX' + + - powershell: | + $version = $Env:BUILD_SOURCEBRANCH.replace("refs/tags/", "") + Write-Output "##vso[task.setvariable variable=version]$version" + displayName: 'Get truncated run number' + + - bash: | + echo "Build Number: $(Build.BuildNumber)" + # Extract the last 3 characters for the run number + runnumber=$(echo "$(Build.BuildNumber)" | grep -o '[0-9]\+$') + echo "Extracted Run Number: $runnumber" + + # If extraction fails, set a default + if [ -z "$runnumber" ]; then + echo "Extraction failed, using default value" + runnumber=$(date +"%S%N" | cut -c1-3) + echo "Generated fallback run number: $runnumber" + fi + + # Set the variable for later steps + echo "##vso[task.setvariable variable=RUNNUMBER]$runnumber" + echo "##vso[task.setvariable variable=RUNNUMBER;isOutput=true]$runnumber" + displayName: 'Get truncated run number' + name: getrunnumber + + - bash: | + echo "Building docker images" + # Format the date to be compatible with Docker tags + formatted_date=$(date +"%Y%m%d%H%M%S") + docker buildx build \ + --platform linux/amd64 \ + --push \ + -t "$(REGISTRY)/$(IMAGE_NAME):latest" \ + -t "$(REGISTRY)/$(IMAGE_NAME):$formatted_date.$RUNNUMBER" \ + "$(Build.SourcesDirectory)" + displayName: 'Build and push docker images' - - \ No newline at end of file