Skip to content

Commit 3cf23b5

Browse files
authored
Merge pull request #3091 from microsoftgraph/task/migrate-acr-push-pipeline
Task: Fixes docker images push
2 parents ba1450c + 38d7e98 commit 3cf23b5

File tree

1 file changed

+81
-2
lines changed

1 file changed

+81
-2
lines changed

.azure-pipelines/ci-build.yml

Lines changed: 81 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,19 @@ variables:
2727
BuildAgent: ${{ parameters.BuildAgent }}
2828
GitUserEmail: "GraphTooling@service.microsoft.com"
2929
GitUserName: "Microsoft Graph DevX Tooling"
30+
REGISTRY: 'msgraphprodregistry.azurecr.io'
31+
IMAGE_NAME: 'public/microsoftgraph/powershell'
32+
PREVIEW_BRANCH: 'refs/heads/main' # Updated to target your branch
3033

3134
trigger:
3235
branches:
3336
include:
3437
- main
3538
- dev
39+
tags:
40+
include:
41+
- v*
42+
3643
pr:
3744
branches:
3845
include:
@@ -156,7 +163,79 @@ extends:
156163
nuGetFeedType: external
157164
publishFeedCredentials: 'microsoftgraph PowerShell Gallery connection'
158165

166+
- stage: PushDockerImageToRegistry
167+
condition: and(or(startsWith(variables['Build.SourceBranch'], 'refs/tags/v'), eq(variables['Build.SourceBranch'], variables['PREVIEW_BRANCH'])), not(contains(variables['Build.SourceBranch'], '-preview')))
168+
dependsOn: []
169+
displayName: 'Push docker image'
170+
jobs:
171+
- job: PushDockerImage
172+
displayName: 'Push docker image'
173+
pool:
174+
name: Azure-Pipelines-1ESPT-ExDShared
175+
image: ubuntu-latest
176+
os: linux
177+
steps:
178+
- checkout: self
179+
180+
# Display the contents of the Build.SourcesDirectory
181+
- bash: |
182+
echo "Build.SourcesDirectory: $(Build.SourcesDirectory)"
183+
ls -la $(Build.SourcesDirectory)
184+
displayName: 'List contents of Build.SourcesDirectory'
159185
186+
- task: AzureCLI@2
187+
displayName: "Log in to Azure Container Registry"
188+
inputs:
189+
azureSubscription: 'ACR Images Push Service Connection' # service connection
190+
scriptType: 'bash'
191+
scriptLocation: 'inlineScript'
192+
'inlineScript': |
193+
az acr login --name $(REGISTRY)
194+
- powershell: |
195+
$date = Get-Date -Format "yyyyMMdd"
196+
Write-Host "##vso[task.setvariable variable=currentDate]$date"
197+
198+
- script: |
199+
docker run --privileged --rm tonistiigi/binfmt --install all
200+
displayName: 'Enable multi-platform builds'
201+
202+
- script: |
203+
docker buildx create --use --name mybuilder
204+
displayName: 'Set up Docker BuildX'
205+
206+
- powershell: |
207+
$version = $Env:BUILD_SOURCEBRANCH.replace("refs/tags/", "")
208+
Write-Output "##vso[task.setvariable variable=version]$version"
209+
displayName: 'Get truncated run number'
210+
211+
- bash: |
212+
echo "Build Number: $(Build.BuildNumber)"
213+
# Extract the last 3 characters for the run number
214+
runnumber=$(echo "$(Build.BuildNumber)" | grep -o '[0-9]\+$')
215+
echo "Extracted Run Number: $runnumber"
216+
217+
# If extraction fails, set a default
218+
if [ -z "$runnumber" ]; then
219+
echo "Extraction failed, using default value"
220+
runnumber=$(date +"%S%N" | cut -c1-3)
221+
echo "Generated fallback run number: $runnumber"
222+
fi
223+
224+
# Set the variable for later steps
225+
echo "##vso[task.setvariable variable=RUNNUMBER]$runnumber"
226+
echo "##vso[task.setvariable variable=RUNNUMBER;isOutput=true]$runnumber"
227+
displayName: 'Get truncated run number'
228+
name: getrunnumber
229+
230+
- bash: |
231+
echo "Building docker images"
232+
# Format the date to be compatible with Docker tags
233+
formatted_date=$(date +"%Y%m%d%H%M%S")
234+
docker buildx build \
235+
--platform linux/amd64 \
236+
--push \
237+
-t "$(REGISTRY)/$(IMAGE_NAME):latest" \
238+
-t "$(REGISTRY)/$(IMAGE_NAME):$formatted_date.$RUNNUMBER" \
239+
"$(Build.SourcesDirectory)"
240+
displayName: 'Build and push docker images'
160241
161-
162-

0 commit comments

Comments
 (0)