From f51bd3ec89e421531a7f51a96859948bbb3fc8f8 Mon Sep 17 00:00:00 2001 From: Djuradj Kurepa <91743470+dkurepa@users.noreply.github.com> Date: Mon, 26 Aug 2024 10:28:54 +0200 Subject: [PATCH] Build docker during pr (#3867) --- azure-pipelines-pr.yml | 34 +++++++++------ ...pipelines-product-construction-service.yml | 41 +++---------------- eng/templates/steps/docker-build.yml | 36 ++++++++++++++++ eng/templates/steps/test.yml | 11 +++-- .../ProductConstructionService.Api/Dockerfile | 3 +- 5 files changed, 71 insertions(+), 54 deletions(-) create mode 100644 eng/templates/steps/docker-build.yml diff --git a/azure-pipelines-pr.yml b/azure-pipelines-pr.yml index fabcfee8de..fa1097a0c4 100644 --- a/azure-pipelines-pr.yml +++ b/azure-pipelines-pr.yml @@ -6,6 +6,8 @@ variables: value: https://dev.azure.com/dnceng - name: AzdoProject value: internal +- name: configuration + value: Release pr: branches: @@ -64,29 +66,35 @@ stages: enablePublishUsingPipelines: ${{ variables._PublishUsingPipelines }} jobs: - - job: Windows_NT + - job: Build + displayName: Build Repo timeoutInMinutes: 90 pool: name: NetCore-Public demands: ImageOverride -equals 1es-windows-2019-open - strategy: - matrix: - debug_configuration: - _BuildConfig: Debug - _PublishType: none - _SignType: test - release_configuration: - _BuildConfig: Release - # PRs or external builds are not signed. - _PublishType: none - _SignType: test steps: - checkout: self clean: true - template: /eng/templates/steps/build.yml parameters: - configuration: $(_BuildConfig) + configuration: $(configuration) - template: /eng/templates/steps/test.yml + parameters: + configuration: $(configuration) + + - job: Builder_Docker + displayName: Build Docker Image + pool: + name: NetCore-Public + demands: ImageOverride -equals 1es-ubuntu-2004-open + + steps: + - checkout: self + clean: true + + - template: /eng/templates/steps/docker-build.yml + parameters: + dockerImageName: test diff --git a/azure-pipelines-product-construction-service.yml b/azure-pipelines-product-construction-service.yml index ac9a480cf5..a105ccd795 100644 --- a/azure-pipelines-product-construction-service.yml +++ b/azure-pipelines-product-construction-service.yml @@ -58,44 +58,13 @@ stages: steps: - checkout: self - - powershell: | - Write-Host "Dev branch suffix is $(devBranchSuffix)" - $shortSha = "$(Build.SourceVersion)".Substring(0,10) - $newDockerTag = "$(Build.BuildNumber)-$(System.JobAttempt)-$shortSha$(devBranchSuffix)" - Write-Host "##vso[task.setvariable variable=newDockerImageTag]$newDockerTag" - Write-Host "set newDockerImageTag to $newDockerTag" - displayName: Generate docker image tag - - - powershell: > - docker build . - -f $(Build.SourcesDirectory)/src/ProductConstructionService/ProductConstructionService.Api/Dockerfile - -t "$(dockerRegistryUrl)/$(containerName):$(newDockerImageTag)" - displayName: Build docker image + - template: eng/templates/steps/docker-build.yml + parameters: + devBranchSuffix: $(devBranchSuffix) + dockerImageName: $(dockerRegistryUrl)/$(containerName) - ${{ if notin(variables['Build.Reason'], 'PullRequest') }}: - - task: AzureCLI@2 - inputs: - azureSubscription: $(serviceConnectionName) - scriptType: pscore - scriptLocation: inlineScript - inlineScript: | - az acr login --name $(containerRegistryName) - docker push "$(dockerRegistryUrl)/$(containerName):$(newDockerImageTag)" - displayName: Push docker image - - ${{ if ne(variables['Build.SourceBranch'], 'refs/heads/production') }}: - - task: AzureCLI@2 - inputs: - # The Service Connection name needs to be known at compile time, so we can't use a variable for the azure subscription - azureSubscription: $(serviceConnectionName) - scriptType: pscore - scriptLocation: inlineScript - inlineScript: | - New-Item -ItemType Directory -Path $(diffFolder) - $before = az containerapp show --name $(containerappName) -g $(resourceGroupName) --output json - Set-Content -Path $(diffFolder)/before.json -Value $before - displayName: Snapshot configuration (before) - - task: AzureCLI@2 name: GetAuthInfo displayName: Get PCS Token @@ -118,7 +87,7 @@ stages: arguments: > -resourceGroupName $(resourceGroupName) -containerappName $(containerappName) - -newImageTag $(newDockerImageTag) + -newImageTag $(DockerTag.newDockerImageTag) -containerRegistryName $(containerRegistryName) -imageName $(containerName) -token $(GetAuthInfo.Token) diff --git a/eng/templates/steps/docker-build.yml b/eng/templates/steps/docker-build.yml new file mode 100644 index 0000000000..1add68f816 --- /dev/null +++ b/eng/templates/steps/docker-build.yml @@ -0,0 +1,36 @@ +parameters: +- name: devBranchSuffix + type: string + default: '' +- name: dockerImageName + type: string + +steps: +- powershell: | + Write-Host "Dev branch suffix is ${{ parameters.devBranchSuffix }}" + $shortSha = "$(Build.SourceVersion)".Substring(0,10) + $newDockerTag = "$(Build.BuildNumber)-$(System.JobAttempt)-$shortSha${{ parameters.devBranchSuffix }}" + Write-Host "##vso[task.setvariable variable=newDockerImageTag;isOutput=true]$newDockerTag" + Write-Host "set newDockerImageTag to $newDockerTag" + displayName: Generate docker image tag + name: DockerTag + +- powershell: | + mkdir $(Build.SourcesDirectory)/artifacts/log + docker build . ` + -f $(Build.SourcesDirectory)/src/ProductConstructionService/ProductConstructionService.Api/Dockerfile ` + -t "${{ parameters.dockerImageName }}:$(DockerTag.newDockerImageTag)" ` + --progress=plain ` + 2>&1 | tee $(Build.SourcesDirectory)/artifacts/log/docker-build.log + displayName: Build docker image + +- ${{ if notin(variables['Build.Reason'], 'PullRequest') }}: + - task: AzureCLI@2 + inputs: + azureSubscription: $(serviceConnectionName) + scriptType: pscore + scriptLocation: inlineScript + inlineScript: | + az acr login --name $(containerRegistryName) + docker push "$(dockerRegistryUrl)/$(containerName):$(DockerTag.newDockerImageTag)" + displayName: Push docker image \ No newline at end of file diff --git a/eng/templates/steps/test.yml b/eng/templates/steps/test.yml index 6c6248f18c..cca64f9705 100644 --- a/eng/templates/steps/test.yml +++ b/eng/templates/steps/test.yml @@ -1,5 +1,8 @@ -steps: +parameters: +- name: configuration + type: string +steps: - task: Powershell@2 displayName: Install SQL Express inputs: @@ -14,12 +17,12 @@ steps: $(Build.SourcesDirectory)\arcade-services.sln custom: test arguments: > - --configuration $(_BuildConfig) + --configuration ${{ parameters.configuration }} --filter "TestCategory!=PostDeployment&TestCategory!=Nightly&TestCategory!=PreDeployment" --no-build --logger "trx;LogFilePrefix=TestResults-" -v normal - /bl:$(Build.SourcesDirectory)/artifacts/log/$(_BuildConfig)/UnitTest.binlog + /bl:$(Build.SourcesDirectory)/artifacts/log/${{ parameters.configuration }}/UnitTest.binlog -- "RunConfiguration.ResultsDirectory=$(Build.ArtifactStagingDirectory)\TestResults" RunConfiguration.MapCpuCount=4 @@ -36,7 +39,7 @@ steps: searchFolder: $(Build.ArtifactStagingDirectory)\TestResults testRunTitle: Basic Tests mergeTestResults: true - configuration: $(_BuildConfig) + configuration: ${{ parameters.configuration }} - script: echo export const token = ''; > src/environments/token.ts workingDirectory: $(Build.SourcesDirectory)/src/Maestro/maestro-angular diff --git a/src/ProductConstructionService/ProductConstructionService.Api/Dockerfile b/src/ProductConstructionService/ProductConstructionService.Api/Dockerfile index d85c4f6517..0f51b8d432 100644 --- a/src/ProductConstructionService/ProductConstructionService.Api/Dockerfile +++ b/src/ProductConstructionService/ProductConstructionService.Api/Dockerfile @@ -26,9 +26,10 @@ COPY ["src/Microsoft.DotNet.Darc/DarcLib/Microsoft.DotNet.DarcLib.csproj", "./Mi WORKDIR /src/ProductConstructionService COPY ["src/ProductConstructionService/ProductConstructionService.Api/ProductConstructionService.Api.csproj", "./ProductConstructionService.Api/"] COPY ["src/ProductConstructionService/ProductConstructionService.Common/ProductConstructionService.Common.csproj", "./ProductConstructionService.Common/"] +COPY ["src/ProductConstructionService/ProductConstructionService.DependencyFlow/ProductConstructionService.DependencyFlow.csproj", "./ProductConstructionService.DependencyFlow/"] +COPY ["src/ProductConstructionService/ProductConstructionService.LongestBuildPathUpdater/ProductConstructionService.LongestBuildPathUpdater.csproj", "./ProductConstructionService.LongestBuildPathUpdater/"] COPY ["src/ProductConstructionService/ProductConstructionService.ServiceDefaults/ProductConstructionService.ServiceDefaults.csproj", "./ProductConstructionService.ServiceDefaults/"] COPY ["src/ProductConstructionService/ProductConstructionService.SubscriptionTriggerer/ProductConstructionService.SubscriptionTriggerer.csproj", "./ProductConstructionService.SubscriptionTriggerer/"] -COPY ["src/ProductConstructionService/ProductConstructionService.LongestBuildPathUpdater/ProductConstructionService.LongestBuildPathUpdater.csproj", "./ProductConstructionService.LongestBuildPathUpdater/"] COPY ["src/ProductConstructionService/ProductConstructionService.WorkItems/ProductConstructionService.WorkItems.csproj", "./ProductConstructionService.WorkItems/"] RUN dotnet restore "./ProductConstructionService.Api/ProductConstructionService.Api.csproj"