diff --git a/.azure-pipelines/ci-build.yml b/.azure-pipelines/ci-build.yml index ed791e58b..e4101f4bb 100644 --- a/.azure-pipelines/ci-build.yml +++ b/.azure-pipelines/ci-build.yml @@ -15,10 +15,15 @@ pr: include: - main - support/v1 + variables: buildPlatform: 'Any CPU' buildConfiguration: 'Release' ProductBinPath: '$(Build.SourcesDirectory)\src\Microsoft.OpenApi\bin\$(BuildConfiguration)' + REGISTRY: 'msgraphprodregistry.azurecr.io' + IMAGE_NAME: 'public/openapi/hidi' + PREVIEW_BRANCH: 'refs/heads/main' + resources: repositories: - repository: 1ESPipelineTemplates @@ -313,3 +318,119 @@ extends: assets: '$(Pipeline.Workspace)\**\*.exe' addChangeLog: false + - stage: Build_and_deploy_docker_images + displayName: 'Build and deploy docker images' + condition: or(eq(variables['build.sourceBranch'], 'refs/tags/v'), eq(variables['build.sourceBranch'], variables['PREVIEW_BRANCH'])) + dependsOn: build + pool: + name: Azure-Pipelines-1ESPT-ExDShared + image: ubuntu-latest + os: linux + jobs: + - job: buildAndPush + steps: + - task: AzureCLI@2 + displayName: 'Login to Azure Container Registry' + inputs: + azureSubscription: 'ACR Images Push Service Connection' + scriptType: bash + scriptLocation: inlineScript + inlineScript: | + az acr login --name msgraphprodregistry + + - powershell: | + $content = [XML](Get-Content ./Directory.Build.props) + Write-Host "XML loaded, finding version..." + + # Handle PropertyGroup as either a single element or array + $version = $null + if ($content.Project.PropertyGroup -is [array]) { + Write-Host "PropertyGroup is an array, checking each entry..." + foreach ($pg in $content.Project.PropertyGroup) { + if ($pg.Version) { + $version = $pg.Version.ToString().Trim() + Write-Host "Found version in PropertyGroup array: $version" + break + } + } + } else { + # Single PropertyGroup + $version = $content.Project.PropertyGroup.Version + if ($version) { + $version = $version.ToString().Trim() + Write-Host "Found version in PropertyGroup: $version" + } + } + + if (-not $version) { + Write-Host "##vso[task.logissue type=error]Version not found in Directory.Build.props" + exit 1 + } + + Write-Host "Version found: $version" + Write-Host "##vso[task.setvariable variable=version;isoutput=true]$version" + Write-Host "##vso[task.setvariable variable=VERSION]$version" + displayName: 'Get version from csproj' + name: getversion + + - bash: | + # Debug output to verify version variable + echo "Version from previous step: $VERSION" + displayName: 'Verify version variable' + + - 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 + condition: eq(variables['Build.SourceBranch'], variables['PREVIEW_BRANCH']) + + - bash: | + date=$(date +'%Y%m%d') + echo "Date value: $date" + echo "##vso[task.setvariable variable=BUILDDATE;isOutput=true]$date" + echo "##vso[task.setvariable variable=BUILDDATE]$date" + displayName: 'Get current date' + name: setdate + condition: eq(variables['Build.SourceBranch'], variables['PREVIEW_BRANCH']) + + - bash: | + echo "Building Docker image..." + echo "Using build date: ${BUILDDATE}" + # Using quotes around tags to prevent flag interpretation + docker build \ + -t "$(REGISTRY)/$(IMAGE_NAME):nightly" \ + -t "$(REGISTRY)/$(IMAGE_NAME):${VERSION}.${BUILDDATE}${RUNNUMBER}" \ + "$(Build.SourcesDirectory)" + + echo "Pushing Docker image with nightly tag..." + docker push "$(REGISTRY)/$(IMAGE_NAME):nightly" + docker push "$(REGISTRY)/$(IMAGE_NAME):${VERSION}.${BUILDDATE}${RUNNUMBER}" + displayName: 'Build and Push Nightly Image' + condition: eq(variables['Build.SourceBranch'], variables['PREVIEW_BRANCH']) + + - bash: | + echo "Building Docker image for release..." + docker build \ + -t "$(REGISTRY)/$(IMAGE_NAME):latest" \ + -t "$(REGISTRY)/$(IMAGE_NAME):${VERSION}.${BUILDDATE}${RUNNUMBER}" \ + "$(Build.SourcesDirectory)" + + echo "Pushing Docker image with latest and version tags..." + docker push "$(REGISTRY)/$(IMAGE_NAME):latest" + docker push "$(REGISTRY)/$(IMAGE_NAME):${VERSION}.${BUILDDATE}${RUNNUMBER}" + displayName: 'Build and Push Release Image' + condition: startsWith(variables['Build.SourceBranch'], 'refs/tags/v') \ No newline at end of file diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml deleted file mode 100644 index d1c7cd9c7..000000000 --- a/.github/workflows/docker.yml +++ /dev/null @@ -1,54 +0,0 @@ -name: Publish Docker image -on: - workflow_dispatch: - push: - tags: ["v*"] - branches: [main] - pull_request: -env: - REGISTRY: msgraphprod.azurecr.io - IMAGE_NAME: public/openapi/hidi - PREVIEW_BRANCH: "refs/heads/main" -jobs: - push_to_registry: - environment: - name: acr - name: Push Docker image - runs-on: ubuntu-latest - steps: - - name: Check out the repo - uses: actions/checkout@v4 - - name: Login to registry - uses: docker/login-action@v3.3.0 - with: - username: ${{ secrets.ACR_USERNAME }} - password: ${{ secrets.ACR_PASSWORD }} - registry: ${{ env.REGISTRY }} - - run: | - $content = [XML](Get-Content ./src/Microsoft.OpenApi.Hidi/Microsoft.OpenApi.Hidi.csproj) - $version = $content.Project.PropertyGroup.Version - echo "::set-output name=version::${version}" - shell: pwsh - id: getversion - - name: Get truncated run number - if: contains(github.ref, env.PREVIEW_BRANCH) - id: runnumber - run: echo "runnumber=$(echo ${{ github.run_number }} | awk '{ print substr($0, length($0)-3, length($0)) }')" >> $GITHUB_OUTPUT - - name: Get current date - if: contains(github.ref, env.PREVIEW_BRANCH) - id: date - run: echo "date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT - - name: Push to registry - Nightly - if: contains(github.ref, env.PREVIEW_BRANCH) - uses: docker/build-push-action@v6.14.0 - with: - push: true - tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:nightly,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }}-preview.${{ steps.date.outputs.date }}${{ steps.runnumber.outputs.runnumber }} - build-args: | - version_suffix=preview.${{ steps.date.outputs.date }}${{ steps.runnumber.outputs.runnumber }} - - name: Push to registry - Release - if: contains(github.ref, 'refs/tags/v') - uses: docker/build-push-action@v6.14.0 - with: - push: true - tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest,${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.getversion.outputs.version }}