GeezerVison is showing github.com — Merge pull request #480 from vvoland/docker-install-image-latest-fix · docker/actions-toolkit@853d5fa
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: __tests__/docker/install.test.itg.ts +14-8Lines changed: 14 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 14 | 14 | * limitations under the License. | 15 | 15 | */ | 16 | 16 |
| 17 | | -import {jest, describe, expect, test, beforeEach, afterEach} from '@jest/globals'; | | 17 | +import {jest, describe, test, beforeEach, afterEach, expect} from '@jest/globals'; | 18 | 18 | import fs from 'fs'; | 19 | 19 | import os from 'os'; | 20 | 20 | import path from 'path'; | @@ -43,6 +43,7 @@ aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-g | 43 | 43 | test.each([ | 44 | 44 | {type: 'image', tag: '27.3.1'} as InstallSourceImage, | 45 | 45 | {type: 'image', tag: 'master'} as InstallSourceImage, | | 46 | + {type: 'image', tag: 'latest'} as InstallSourceImage, | 46 | 47 | {type: 'archive', version: 'v26.1.4', channel: 'stable'} as InstallSourceArchive, | 47 | 48 | {type: 'archive', version: 'latest', channel: 'stable'} as InstallSourceArchive, | 48 | 49 | ])( | @@ -65,12 +66,17 @@ aarch64:https://cloud.debian.org/images/cloud/bookworm/20231013-1532/debian-12-g | 65 | 66 | daemonConfig: `{"debug":true,"features":{"containerd-snapshotter":true}}` | 66 | 67 | }); | 67 | 68 | await expect((async () => { | 68 | | - await install.download(); | 69 | | - await install.install(); | 70 | | - await Docker.printVersion(); | 71 | | - await Docker.printInfo(); | 72 | | - })().finally(async () => { | 73 | | - await install.tearDown(); | 74 | | - })).resolves.not.toThrow(); | | 69 | + try { | | 70 | + await install.download(); | | 71 | + await install.install(); | | 72 | + await Docker.printVersion(); | | 73 | + await Docker.printInfo(); | | 74 | + } catch (error) { | | 75 | + console.error(error); | | 76 | + throw error; | | 77 | + } finally { | | 78 | + await install.tearDown(); | | 79 | + } | | 80 | + })()).resolves.not.toThrow(); | 75 | 81 | }, 30 * 60 * 1000); | 76 | 82 | }); |
Collapse file Copy file name to clipboardExpand all lines: src/docker/assets.ts +4-6Lines changed: 4 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -237,12 +237,10 @@ provision: | 237 | 237 |
| 238 | 238 | HOME=/tmp undock moby/moby-bin:{{srcImageTag}} /usr/local/bin | 239 | 239 |
| 240 | | - wget https://raw.githubusercontent.com/moby/moby/{{srcImageTag}}/contrib/init/systemd/docker.service \ | 241 | | - https://raw.githubusercontent.com/moby/moby/v{{srcImageTag}}/contrib/init/systemd/docker.service \ | 242 | | - -O /etc/systemd/system/docker.service || true | 243 | | - wget https://raw.githubusercontent.com/moby/moby/{{srcImageTag}}/contrib/init/systemd/docker.socket \ | 244 | | - https://raw.githubusercontent.com/moby/moby/v{{srcImageTag}}/contrib/init/systemd/docker.socket \ | 245 | | - -O /etc/systemd/system/docker.socket || true | | 240 | + wget https://raw.githubusercontent.com/moby/moby/{{gitCommit}}/contrib/init/systemd/docker.service \ | | 241 | + -O /etc/systemd/system/docker.service | | 242 | + wget https://raw.githubusercontent.com/moby/moby/{{gitCommit}}/contrib/init/systemd/docker.socket \ | | 243 | + -O /etc/systemd/system/docker.socket | 246 | 244 |
| 247 | 245 | sed -i 's|^ExecStart=.*|ExecStart=/usr/local/bin/dockerd -H fd://|' /etc/systemd/system/docker.service | 248 | 246 | sed -i 's|containerd.service||' /etc/systemd/system/docker.service | |
Collapse file Copy file name to clipboardExpand all lines: src/docker/install.ts +25-2Lines changed: 25 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -34,6 +34,7 @@ import {Util} from '../util'; | 34 | 34 | import {limaYamlData, dockerServiceLogsPs1, setupDockerWinPs1} from './assets'; | 35 | 35 | import {GitHubRelease} from '../types/github'; | 36 | 36 | import {HubRepository} from '../hubRepository'; | | 37 | +import {Image} from '../types/oci/config'; | 37 | 38 |
| 38 | 39 | export interface InstallSourceImage { | 39 | 40 | type: 'image'; | @@ -71,6 +72,8 @@ export class Install { | 71 | 72 | private _version: string | undefined; | 72 | 73 | private _toolDir: string | undefined; | 73 | 74 |
| | 75 | + private gitCommit: string | undefined; | | 76 | + | 74 | 77 | private readonly limaInstanceName = 'docker-actions-toolkit'; | 75 | 78 |
| 76 | 79 | constructor(opts: InstallOpts) { | @@ -127,12 +130,28 @@ export class Install { | 127 | 130 | const cli = await HubRepository.build('dockereng/cli-bin'); | 128 | 131 | extractFolder = await cli.extractImage(tag); | 129 | 132 |
| | 133 | + const moby = await HubRepository.build('moby/moby-bin'); | 130 | 134 | if (['win32', 'linux'].includes(platform)) { | 131 | 135 | core.info(`Downloading dockerd from moby/moby-bin:${tag}`); | 132 | | - const moby = await HubRepository.build('moby/moby-bin'); | 133 | 136 | await moby.extractImage(tag, extractFolder); | 134 | 137 | } else if (platform == 'darwin') { | 135 | | - // On macOS, the docker daemon binary will be downloaded inside the lima VM | | 138 | + // On macOS, the docker daemon binary will be downloaded inside the lima VM. | | 139 | + // However, we will get the exact git revision from the image config | | 140 | + // to get the matching systemd unit files. | | 141 | + core.info(`Getting git revision from moby/moby-bin:${tag}`); | | 142 | + | | 143 | + // There's no macOS image for moby/moby-bin - a linux daemon is run inside lima. | | 144 | + const manifest = await moby.getPlatformManifest(tag, 'linux'); | | 145 | + | | 146 | + const config = await moby.getJSONBlob<Image>(manifest.config.digest); | | 147 | + core.debug(`Config ${JSON.stringify(config.config)}`); | | 148 | + | | 149 | + this.gitCommit = config.config?.Labels?.['org.opencontainers.image.revision']; | | 150 | + if (!this.gitCommit) { | | 151 | + core.warning(`No git revision can be determined from the image. Will use master.`); | | 152 | + this.gitCommit = 'master'; | | 153 | + } | | 154 | + core.info(`Git revision is ${this.gitCommit}`); | 136 | 155 | } else { | 137 | 156 | core.warning(`dockerd not supported on ${platform}, only the Docker cli will be available`); | 138 | 157 | } | @@ -193,6 +212,9 @@ export class Install { | 193 | 212 | } | 194 | 213 |
| 195 | 214 | private async installDarwin(): Promise<string> { | | 215 | + if (this.source.type == 'image' && !this.gitCommit) { | | 216 | + throw new Error('gitCommit must be set. Run download first.'); | | 217 | + } | 196 | 218 | const src = this.source; | 197 | 219 | const limaDir = path.join(os.homedir(), '.lima', this.limaInstanceName); | 198 | 220 | await io.mkdirP(limaDir); | @@ -229,6 +251,7 @@ export class Install { | 229 | 251 | customImages: Install.limaCustomImages(), | 230 | 252 | daemonConfig: limaDaemonConfig, | 231 | 253 | dockerSock: `${limaDir}/docker.sock`, | | 254 | + gitCommit: this.gitCommit, | 232 | 255 | srcType: src.type, | 233 | 256 | srcArchiveVersion: this._version, // Use the resolved version (e.g. latest -> 27.4.0) | 234 | 257 | srcArchiveChannel: srcArchive.channel, | |
Collapse file Copy file name to clipboardExpand all lines: src/hubRepository.ts +29-12Lines changed: 29 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -21,8 +21,8 @@ import * as core from '@actions/core'; | 21 | 21 | import {Manifest} from './types/oci/manifest'; | 22 | 22 | import * as tc from '@actions/tool-cache'; | 23 | 23 | import fs from 'fs'; | 24 | | -import {MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from './types/oci/mediatype'; | 25 | | -import {MEDIATYPE_IMAGE_MANIFEST_V2, MEDIATYPE_IMAGE_MANIFEST_LIST_V2} from './types/docker/mediatype'; | | 24 | +import {MEDIATYPE_IMAGE_CONFIG_V1, MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_V1} from './types/oci/mediatype'; | | 25 | +import {MEDIATYPE_IMAGE_CONFIG_V1 as DOCKER_MEDIATYPE_IMAGE_CONFIG_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V2} from './types/docker/mediatype'; | 26 | 26 | import {DockerHub} from './dockerhub'; | 27 | 27 |
| 28 | 28 | export class HubRepository { | @@ -40,15 +40,20 @@ export class HubRepository { | 40 | 40 | return new HubRepository(repository, token); | 41 | 41 | } | 42 | 42 |
| 43 | | - // Unpacks the image layers and returns the path to the extracted image. | 44 | | - // Only OCI indexes/manifest list are supported for now. | 45 | | - public async extractImage(tag: string, destDir?: string): Promise<string> { | 46 | | - const index = await this.getManifest<Index>(tag); | | 43 | + public async getPlatformManifest(tagOrDigest: string, os?: string): Promise<Manifest> { | | 44 | + const index = await this.getManifest<Index>(tagOrDigest); | 47 | 45 | if (index.mediaType != MEDIATYPE_IMAGE_INDEX_V1 && index.mediaType != MEDIATYPE_IMAGE_MANIFEST_LIST_V2) { | | 46 | + core.error(`Unsupported image media type: ${index.mediaType}`); | 48 | 47 | throw new Error(`Unsupported image media type: ${index.mediaType}`); | 49 | 48 | } | 50 | | - const digest = HubRepository.getPlatformManifestDigest(index); | 51 | | - const manifest = await this.getManifest<Manifest>(digest); | | 49 | + const digest = HubRepository.getPlatformManifestDigest(index, os); | | 50 | + return await this.getManifest<Manifest>(digest); | | 51 | + } | | 52 | + | | 53 | + // Unpacks the image layers and returns the path to the extracted image. | | 54 | + // Only OCI indexes/manifest list are supported for now. | | 55 | + public async extractImage(tag: string, destDir?: string): Promise<string> { | | 56 | + const manifest = await this.getPlatformManifest(tag); | 52 | 57 |
| 53 | 58 | const paths = manifest.layers.map(async layer => { | 54 | 59 | const url = this.blobUrl(layer.digest); | @@ -99,25 +104,35 @@ export class HubRepository { | 99 | 104 | } | 100 | 105 |
| 101 | 106 | public async getManifest<T>(tagOrDigest: string): Promise<T> { | 102 | | - const url = `https://registry-1.docker.io/v2/${this.repo}/manifests/${tagOrDigest}`; | | 107 | + return await this.registryGet<T>(tagOrDigest, 'manifests', [MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V1, MEDIATYPE_IMAGE_MANIFEST_V2]); | | 108 | + } | | 109 | + | | 110 | + public async getJSONBlob<T>(tagOrDigest: string): Promise<T> { | | 111 | + return await this.registryGet<T>(tagOrDigest, 'blobs', [MEDIATYPE_IMAGE_CONFIG_V1, DOCKER_MEDIATYPE_IMAGE_CONFIG_V1]); | | 112 | + } | | 113 | + | | 114 | + private async registryGet<T>(tagOrDigest: string, endpoint: 'manifests' | 'blobs', accept: Array<string>): Promise<T> { | | 115 | + const url = `https://registry-1.docker.io/v2/${this.repo}/${endpoint}/${tagOrDigest}`; | 103 | 116 |
| 104 | 117 | const headers = { | 105 | 118 | Authorization: `Bearer ${this.token}`, | 106 | | - Accept: [MEDIATYPE_IMAGE_INDEX_V1, MEDIATYPE_IMAGE_MANIFEST_LIST_V2, MEDIATYPE_IMAGE_MANIFEST_V1, MEDIATYPE_IMAGE_MANIFEST_V2].join(', ') | | 119 | + Accept: accept.join(', ') | 107 | 120 | }; | | 121 | + | 108 | 122 | const resp = await HubRepository.http.get(url, headers); | 109 | 123 | const body = await resp.readBody(); | 110 | 124 | const statusCode = resp.message.statusCode || 500; | 111 | 125 | if (statusCode != 200) { | | 126 | + core.error(`registryGet(${this.repo}:${tagOrDigest}) failed: ${statusCode} ${body}`); | 112 | 127 | throw DockerHub.parseError(resp, body); | 113 | 128 | } | 114 | 129 |
| 115 | 130 | return <T>JSON.parse(body); | 116 | 131 | } | 117 | 132 |
| 118 | | - private static getPlatformManifestDigest(index: Index): string { | | 133 | + private static getPlatformManifestDigest(index: Index, osOverride?: string): string { | 119 | 134 | // This doesn't handle all possible platforms normalizations, but it's good enough for now. | 120 | | - let pos: string = os.platform(); | | 135 | + let pos: string = osOverride || os.platform(); | 121 | 136 | if (pos == 'win32') { | 122 | 137 | pos = 'windows'; | 123 | 138 | } | @@ -150,8 +165,10 @@ export class HubRepository { | 150 | 165 | return true; | 151 | 166 | }); | 152 | 167 | if (!manifest) { | | 168 | + core.error(`Cannot find manifest for ${pos}/${arch}/${variant}`); | 153 | 169 | throw new Error(`Cannot find manifest for ${pos}/${arch}/${variant}`); | 154 | 170 | } | | 171 | + | 155 | 172 | return manifest.digest; | 156 | 173 | } | 157 | 174 | } |
Collapse file Copy file name to clipboardExpand all lines: src/types/docker/mediatype.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 17 | 17 | export const MEDIATYPE_IMAGE_MANIFEST_LIST_V2 = 'application/vnd.docker.distribution.manifest.list.v2+json'; | 18 | 18 |
| 19 | 19 | export const MEDIATYPE_IMAGE_MANIFEST_V2 = 'application/vnd.docker.distribution.manifest.v2+json'; | | 20 | + | | 21 | +export const MEDIATYPE_IMAGE_CONFIG_V1 = 'application/vnd.docker.container.image.v1+json'; |
Collapse file Copy file name to clipboardExpand all lines: src/types/oci/mediatype.ts +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -23,3 +23,5 @@ export const MEDIATYPE_IMAGE_INDEX_V1 = 'application/vnd.oci.image.index.v1+json | 23 | 23 | export const MEDIATYPE_IMAGE_LAYER_V1 = 'application/vnd.oci.image.layer.v1.tar'; | 24 | 24 |
| 25 | 25 | export const MEDIATYPE_EMPTY_JSON_V1 = 'application/vnd.oci.empty.v1+json'; | | 26 | + | | 27 | +export const MEDIATYPE_IMAGE_CONFIG_V1 = 'application/vnd.oci.image.config.v1+json'; |
|