-
Notifications
You must be signed in to change notification settings - Fork 64
Pack eng/docker-tools content in ImageBuilder
#2159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lbussell
wants to merge
7
commits into
main
Choose a base branch
from
lbussell/pack-templates-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
22a3093
Extract shared Cottle document configuration into a helper
lbussell cc1deda
Extend IFileSystem with directory and stream operations
lbussell 5790e7c
Add Infrastructure project embedding eng/docker-tools content
lbussell ace15dd
Add ImageBuilder 'update' command to emit bundled docker-tools content
lbussell 8a428c9
Add PathHelper.SafeCombine
lbussell 7d222db
Fix naming violations
lbussell e4c2df8
Pass ImageBuilder reference as an argument to the update command
lbussell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #!/usr/bin/env pwsh | ||
|
|
||
| <# | ||
| .SYNOPSIS | ||
| Example script that updates the bundled docker-tools infrastructure in the current repo to a specific | ||
| ImageBuilder image. | ||
|
|
||
| .DESCRIPTION | ||
| ImageBuilder ships a copy of the eng/docker-tools infrastructure and writes it back to disk via its | ||
| 'update' command. The reference that 'update' records into | ||
| eng/docker-tools/templates/variables/docker-images.yml is supplied as an argument rather than being | ||
| baked into the build, so the caller decides exactly which image the repo should pin to. | ||
|
|
||
| This example resolves the multi-platform (manifest list / image index) digest of an ImageBuilder image | ||
| (the published 'latest' tag by default) and passes that digest reference to the 'update' command, which | ||
| runs inside the same image with the repository mounted so it can rewrite eng/docker-tools on disk. | ||
|
|
||
| .PARAMETER ImageBuilderImage | ||
| The ImageBuilder image to resolve and run. Defaults to the published 'latest' tag. | ||
|
|
||
| .PARAMETER RepoRoot | ||
| The root of the git repository to update. Defaults to the current directory. | ||
|
|
||
| .NOTES | ||
| To exercise an unpublished ImageBuilder (for example, the 'update' command before it is released), | ||
| build the image, push it to a registry it can be pulled from, and pass its reference via | ||
| -ImageBuilderImage. The digest is read from the registry, so the image must be pushed first. | ||
| #> | ||
| [CmdletBinding()] | ||
| param( | ||
| [string] | ||
| $ImageBuilderImage = "mcr.microsoft.com/dotnet-buildtools/image-builder:latest", | ||
|
|
||
| [string] | ||
| $RepoRoot = (Get-Location).Path | ||
| ) | ||
|
|
||
| Set-StrictMode -Version Latest | ||
| $ErrorActionPreference = 'Stop' | ||
|
|
||
| function Exec { | ||
| param ([string] $Cmd) | ||
|
|
||
| Write-Output "Executing: '$Cmd'" | ||
| Invoke-Expression $Cmd | ||
| if ($LASTEXITCODE -ne 0) { | ||
| throw "Failed: '$Cmd'" | ||
| } | ||
| } | ||
|
|
||
| # Strip any existing tag or digest so the resolved digest can be appended to the bare repository name. | ||
| # A tag is a ':' within the final path segment; a registry host's ':port' precedes the last '/', so it | ||
| # must not be mistaken for a tag. | ||
| function Get-RepositoryName { | ||
| param ([string] $Reference) | ||
|
|
||
| $withoutDigest = $Reference.Split('@')[0] | ||
| $lastSlash = $withoutDigest.LastIndexOf('/') | ||
| $lastColon = $withoutDigest.LastIndexOf(':') | ||
| if ($lastColon -gt $lastSlash) { | ||
| return $withoutDigest.Substring(0, $lastColon) | ||
| } | ||
|
|
||
| return $withoutDigest | ||
| } | ||
|
|
||
| # Resolve the multi-platform digest. 'docker buildx imagetools inspect' reads the top-level manifest | ||
| # straight from the registry, so for a multi-arch image this is the manifest list (image index) digest | ||
| # rather than a single platform's digest. Pinning the index keeps the reference valid on every | ||
| # platform the pipeline runs on. | ||
| $digest = (docker buildx imagetools inspect $ImageBuilderImage --format '{{.Manifest.Digest}}') | ||
| if ($LASTEXITCODE -ne 0 -or [string]::IsNullOrWhiteSpace($digest)) { | ||
| throw "Unable to resolve a multi-platform digest for '$ImageBuilderImage'." | ||
| } | ||
|
|
||
| $repository = Get-RepositoryName $ImageBuilderImage | ||
| $imageBuilderRef = "$repository@$($digest.Trim())" | ||
|
|
||
| Write-Output "Resolved ImageBuilder reference: $imageBuilderRef" | ||
|
|
||
| # Run 'update' from the resolved digest, mounting the repository so it can write eng/docker-tools to | ||
| # disk. The command must run from the repository root, which is why $RepoRoot is the mounted working | ||
| # directory. Running by the same digest that gets recorded keeps the writer and the pinned reference | ||
| # identical. | ||
| Exec ("docker run --rm " ` | ||
| + "-v `"${RepoRoot}:/repo`" " ` | ||
| + "-w /repo " ` | ||
| + "$imageBuilderRef " ` | ||
| + "update $imageBuilderRef") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using Shouldly; | ||
|
|
||
| namespace Microsoft.DotNet.ImageBuilder.Tests; | ||
|
|
||
| [TestClass] | ||
| public class PathHelperTests | ||
| { | ||
| [TestMethod] | ||
| public void SafeCombine_RelativeSegments_CombinesUnderBasePath() | ||
| { | ||
| string basePath = $"{Path.DirectorySeparatorChar}repo"; | ||
|
|
||
| string result = PathHelper.SafeCombine(basePath, "eng", "docker-tools"); | ||
|
|
||
| result.ShouldBe(Path.Combine(basePath, "eng", "docker-tools")); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SafeCombine_ForwardSlashSegment_ConvertsToPlatformSeparator() | ||
| { | ||
| string basePath = $"{Path.DirectorySeparatorChar}repo"; | ||
|
|
||
| string result = PathHelper.SafeCombine(basePath, "templates/variables/docker-images.yml"); | ||
|
|
||
| result.ShouldBe(Path.Combine(basePath, "templates", "variables", "docker-images.yml")); | ||
|
lbussell marked this conversation as resolved.
Dismissed
|
||
| } | ||
|
|
||
| [TestMethod] | ||
| public void SafeCombine_RootedSegment_Throws() | ||
| { | ||
| string basePath = $"{Path.DirectorySeparatorChar}repo"; | ||
| string rootedSegment = $"{Path.DirectorySeparatorChar}etc{Path.DirectorySeparatorChar}passwd"; | ||
|
|
||
| ArgumentException exception = | ||
| Should.Throw<ArgumentException>(() => PathHelper.SafeCombine(basePath, rootedSegment)); | ||
| exception.Message.ShouldContain("rooted"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow("..")] | ||
| [DataRow("../escape")] | ||
| [DataRow("nested/../../escape")] | ||
| public void SafeCombine_TraversalSegment_Throws(string traversalSegment) | ||
| { | ||
| string basePath = $"{Path.DirectorySeparatorChar}repo"; | ||
|
|
||
| ArgumentException exception = | ||
| Should.Throw<ArgumentException>(() => PathHelper.SafeCombine(basePath, traversalSegment)); | ||
| exception.Message.ShouldContain("traversal"); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow("")] | ||
| [DataRow(" ")] | ||
| [DataRow(null)] | ||
| public void SafeCombine_NullOrWhitespaceSegment_Throws(string? segment) | ||
| { | ||
| string basePath = $"{Path.DirectorySeparatorChar}repo"; | ||
|
|
||
| Should.Throw<ArgumentException>(() => PathHelper.SafeCombine(basePath, segment!)); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.