Skip to content

Commit aa0228d

Browse files
committed
docker: parseRepoTag and pull methods
Signed-off-by: CrazyMax <[email protected]>
1 parent 0200700 commit aa0228d

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Copyright 2024 actions-toolkit authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import {describe, expect, test} from '@jest/globals';
18+
19+
import {Docker} from '../../src/docker/docker';
20+
21+
const maybe = !process.env.GITHUB_ACTIONS || (process.env.GITHUB_ACTIONS === 'true' && process.env.ImageOS && process.env.ImageOS.startsWith('ubuntu')) ? describe : describe.skip;
22+
23+
maybe('pull', () => {
24+
// prettier-ignore
25+
test.each([
26+
'busybox',
27+
'busybox:1.36',
28+
'busybox@sha256:7ae8447f3a7f5bccaa765926f25fc038e425cf1b2be6748727bbea9a13102094'
29+
])(
30+
'pulling %s', async (image) => {
31+
await expect((async () => {
32+
await Docker.pull(image, true);
33+
})()).resolves.not.toThrow();
34+
}, 600000);
35+
});

src/docker/docker.ts

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ import path from 'path';
2020
import * as core from '@actions/core';
2121
import * as io from '@actions/io';
2222

23+
import {Context} from '../context';
24+
import {Cache} from '../cache';
2325
import {Exec} from '../exec';
26+
import {Util} from '../util';
2427

2528
import {ConfigFile} from '../types/docker';
2629

@@ -73,4 +76,78 @@ export class Docker {
7376
public static async printInfo(): Promise<void> {
7477
await Exec.exec('docker', ['info']);
7578
}
79+
80+
public static parseRepoTag(image: string): {repository: string; tag: string} {
81+
let sepPos: number;
82+
const digestPos = image.indexOf('@');
83+
const colonPos = image.lastIndexOf(':');
84+
if (digestPos >= 0) {
85+
// priority on digest
86+
sepPos = digestPos;
87+
} else if (colonPos >= 0) {
88+
sepPos = colonPos;
89+
} else {
90+
return {
91+
repository: image,
92+
tag: 'latest'
93+
};
94+
}
95+
const tag = image.slice(sepPos + 1);
96+
if (tag.indexOf('/') === -1) {
97+
return {
98+
repository: image.slice(0, sepPos),
99+
tag: tag
100+
};
101+
}
102+
return {
103+
repository: image,
104+
tag: 'latest'
105+
};
106+
}
107+
108+
public static async pull(image: string, cache?: boolean): Promise<void> {
109+
const parsedImage = Docker.parseRepoTag(image);
110+
const repoSanitized = parsedImage.repository.replace(/[^a-zA-Z0-9.]+/g, '--');
111+
const tagSanitized = parsedImage.tag.replace(/[^a-zA-Z0-9.]+/g, '--');
112+
113+
const imageCache = new Cache({
114+
htcName: repoSanitized,
115+
htcVersion: tagSanitized,
116+
baseCacheDir: path.join(Docker.configDir, '.cache', 'images', repoSanitized),
117+
cacheFile: 'image.tar'
118+
});
119+
120+
let cacheFoundPath: string | undefined;
121+
if (cache) {
122+
cacheFoundPath = await imageCache.find();
123+
if (cacheFoundPath) {
124+
core.info(`Image found from cache in ${cacheFoundPath}`);
125+
await Exec.getExecOutput(`docker`, ['load', '-i', cacheFoundPath]).catch(e => {
126+
core.warning(`Failed to load image from cache: ${e}`);
127+
});
128+
}
129+
}
130+
131+
let pulled = true;
132+
await Exec.getExecOutput(`docker`, ['pull', image]).catch(e => {
133+
pulled = false;
134+
if (cacheFoundPath) {
135+
core.warning(`Failed to pull image, using one from cache: ${e}`);
136+
} else {
137+
throw new Error(e);
138+
}
139+
});
140+
141+
if (cache && pulled) {
142+
const imageTarPath = path.join(Context.tmpDir(), `${Util.hash(image)}.tar`);
143+
await Exec.getExecOutput(`docker`, ['save', '-o', imageTarPath, image])
144+
.then(async () => {
145+
const cachePath = await imageCache.save(imageTarPath);
146+
core.info(`Image cached to ${cachePath}`);
147+
})
148+
.catch(e => {
149+
core.warning(`Failed to save image: ${e}`);
150+
});
151+
}
152+
}
76153
}

0 commit comments

Comments
 (0)