Skip to content

Commit 0a1492c

Browse files
committed
fix: github actions runner roller for linux
1 parent fb2b754 commit 0a1492c

File tree

6 files changed

+71
-16
lines changed

6 files changed

+71
-16
lines changed

src/actions-runner-handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ export async function rollActionsRunner() {
4646
const currentImages = currentLinuxImages(runnerFile.raw);
4747
if (currentImages.amd64 !== archDigests.amd64 || currentImages.arm64 !== archDigests.arm64) {
4848
d(`Current linux images in "${arcEnv}" are outdated, updating to ${latestVersion}.`);
49-
let newContent = runnerFile.raw.replace(currentImages.amd64, archDigests.amd64);
50-
newContent = newContent.replace(currentImages.arm64, archDigests.arm64);
49+
let newContent = runnerFile.raw.replaceAll(currentImages.amd64, archDigests.amd64);
50+
newContent = newContent.replaceAll(currentImages.arm64, archDigests.arm64);
5151
await rollInfra(
5252
`${arcEnv}/actions-runner`,
5353
'github actions runner images',

src/utils/arc-image.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const WINDOWS_RUNNER_REGEX = /ARG RUNNER_VERSION=([\d.]+)/;
66
const WINDOWS_IMAGE_REGEX =
77
/electronarc\.azurecr\.io\/win-actions-runner:main-[a-f0-9]{7}@sha256:[a-f0-9]{64}/;
88
const LINUX_IMAGE_REGEX =
9-
/if eq .cpuArch "amd64".*\n.*image: (ghcr.io\/actions\/actions-runner:[0-9]+\.[0-9]+\.[0-9]+@sha256:[a-f0-9]{64}).*\n.*{{- else }}.*\n.*image: (ghcr.io\/actions\/actions-runner:[0-9]+\.[0-9]+\.[0-9]+@sha256:[a-f0-9]{64})/;
9+
/if eq .cpuArch "amd64".*\n.*image: ghcr.io\/actions\/actions-runner:([0-9]+\.[0-9]+\.[0-9]+@sha256:[a-f0-9]{64}).*\n.*{{- else }}.*\n.*image: ghcr.io\/actions\/actions-runner:([0-9]+\.[0-9]+\.[0-9]+@sha256:[a-f0-9]{64})/;
1010

1111
export async function getFileContent(octokit: Octokit, filePath: string, ref = MAIN_BRANCH) {
1212
const { data } = await octokit.repos.getContent({

src/utils/get-latest-runner-images.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ export async function getLatestRunnerImages(
8484
platform?.os === 'linux' &&
8585
(platform.architecture === 'amd64' || platform.architecture === 'arm64')
8686
) {
87-
archDigests[platform.architecture] = manifest.digest;
87+
archDigests[platform.architecture] = `${tagVersion}@${manifest.digest}`;
8888
}
8989
}
9090

tests/actions-runner-handler.test.ts

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,41 @@ const mockFileContent = (raw: string) => ({ raw, sha: 'sha' });
1919

2020
const latestVersion = '2.325.0';
2121
const archDigests = {
22-
amd64: 'sha256:amd64digest',
23-
arm64: 'sha256:arm64digest',
22+
amd64: '2.325.0@sha256:amd64digest',
23+
arm64: '2.325.0@sha256:arm64digest',
2424
};
2525

2626
const newWinDockerFile = 'ARG RUNNER_VERSION=2.325.0';
2727
const oldWinDockerFile = 'ARG RUNNER_VERSION=2.324.0';
2828

2929
const oldLinuxImages = {
30-
amd64: 'ghcr.io/actions/actions-runner:2.324.0@sha256:oldamd64',
31-
arm64: 'ghcr.io/actions/actions-runner:2.324.0@sha256:oldarm64',
30+
amd64: '2.324.0@sha256:oldamd64',
31+
arm64: '2.324.0@sha256:oldarm64',
3232
};
3333
const newLinuxImages = {
3434
amd64: archDigests.amd64,
3535
arm64: archDigests.arm64,
3636
};
3737

38+
function generateFileContent(images) {
39+
return `
40+
{{- if eq .cpuArch "amd64" }}
41+
image: ghcr.io/actions/actions-runner:${images.amd64}
42+
{{- else }}
43+
image: ghcr.io/actions/actions-runner:${images.arm64}
44+
{{- end }}
45+
{{ more content here }}
46+
{{- if eq .cpuArch "amd64" }}
47+
image: ghcr.io/actions/actions-runner:${images.amd64}
48+
{{- else }}
49+
image: ghcr.io/actions/actions-runner:${images.arm64}
50+
{{- end }}
51+
`;
52+
}
53+
54+
const oldLinuxFileContent = generateFileContent(oldLinuxImages);
55+
const newLinuxFileContent = generateFileContent(newLinuxImages);
56+
3857
beforeEach(() => {
3958
vi.clearAllMocks();
4059
(getOctokit as any).mockResolvedValue(mockOctokit);
@@ -45,7 +64,7 @@ beforeEach(() => {
4564
vi.mocked(arcImage.getFileContent).mockImplementation(async (_octokit: any, file: string) => {
4665
if (file === constants.WINDOWS_DOCKER_FILE) return mockFileContent(oldWinDockerFile);
4766
if (file === constants.ARC_RUNNER_ENVIRONMENTS.prod)
48-
return mockFileContent(`amd64: ${oldLinuxImages.amd64}\narm64: ${oldLinuxImages.arm64}`);
67+
return mockFileContent(generateFileContent(oldLinuxImages));
4968
return mockFileContent('');
5069
});
5170
vi.mocked(arcImage.getCurrentWindowsRunnerVersion).mockImplementation(
@@ -59,33 +78,69 @@ beforeEach(() => {
5978
});
6079

6180
describe('rollActionsRunner', () => {
81+
it('should update both linx and windows if version is outdated', async () => {
82+
await rollActionsRunner();
83+
expect(rollInfraModule.rollInfra).toHaveBeenCalledTimes(2);
84+
expect(rollInfraModule.rollInfra).toHaveBeenNthCalledWith(
85+
1,
86+
'prod/actions-runner',
87+
'github actions runner images',
88+
latestVersion,
89+
constants.WINDOWS_DOCKER_FILE,
90+
newWinDockerFile,
91+
);
92+
expect(rollInfraModule.rollInfra).toHaveBeenNthCalledWith(
93+
2,
94+
'prod/actions-runner',
95+
'github actions runner images',
96+
latestVersion,
97+
constants.ARC_RUNNER_ENVIRONMENTS.prod,
98+
newLinuxFileContent,
99+
);
100+
});
101+
62102
it('should update windows runner if version is outdated', async () => {
103+
vi.mocked(arcImage.getFileContent).mockImplementation(async (_octokit: any, file: string) => {
104+
if (file === constants.WINDOWS_DOCKER_FILE) return mockFileContent(oldWinDockerFile);
105+
if (file === constants.ARC_RUNNER_ENVIRONMENTS.prod)
106+
return mockFileContent(newLinuxFileContent);
107+
return mockFileContent('');
108+
});
63109
await rollActionsRunner();
110+
expect(rollInfraModule.rollInfra).toHaveBeenCalledTimes(1);
64111
expect(rollInfraModule.rollInfra).toHaveBeenCalledWith(
65112
'prod/actions-runner',
66113
'github actions runner images',
67114
latestVersion,
68115
constants.WINDOWS_DOCKER_FILE,
69-
expect.stringContaining(latestVersion),
116+
newWinDockerFile,
70117
);
71118
});
72119

73120
it('should update linux images if digests are outdated', async () => {
121+
vi.mocked(arcImage.getFileContent).mockImplementation(async (_octokit: any, file: string) => {
122+
if (file === constants.WINDOWS_DOCKER_FILE) return mockFileContent(newWinDockerFile);
123+
if (file === constants.ARC_RUNNER_ENVIRONMENTS.prod)
124+
return mockFileContent(oldLinuxFileContent);
125+
return mockFileContent('');
126+
});
127+
74128
await rollActionsRunner();
129+
expect(rollInfraModule.rollInfra).toHaveBeenCalledTimes(1);
75130
expect(rollInfraModule.rollInfra).toHaveBeenCalledWith(
76131
'prod/actions-runner',
77132
'github actions runner images',
78133
latestVersion,
79134
constants.ARC_RUNNER_ENVIRONMENTS.prod,
80-
expect.any(String),
135+
newLinuxFileContent,
81136
);
82137
});
83138

84139
it('should skip update if everything is up-to-date', async () => {
85140
vi.mocked(arcImage.getFileContent).mockImplementation(async (_octokit: any, file: string) => {
86141
if (file === constants.WINDOWS_DOCKER_FILE) return mockFileContent(newWinDockerFile);
87142
if (file === constants.ARC_RUNNER_ENVIRONMENTS.prod)
88-
return mockFileContent(`amd64: ${archDigests.amd64}\narm64: ${archDigests.arm64}`);
143+
return mockFileContent(newLinuxFileContent);
89144
return mockFileContent('');
90145
});
91146
vi.mocked(arcImage.getCurrentWindowsRunnerVersion).mockImplementation(

tests/utils/arc-image.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ describe('arc-image utils', () => {
3939
it('should extract both linux images', () => {
4040
const images = currentLinuxImages(linuxImageContent);
4141
expect(images.amd64).toBe(
42-
'ghcr.io/actions/actions-runner:2.325.0@sha256:b865e3f046f0a92a4b936ae75c5bc5615b99b64eb4801b0e5220f13f8867d6b8',
42+
'2.325.0@sha256:b865e3f046f0a92a4b936ae75c5bc5615b99b64eb4801b0e5220f13f8867d6b8',
4343
);
4444
expect(images.arm64).toBe(
45-
'ghcr.io/actions/actions-runner:2.325.0@sha256:ab3fb968f7bcc8b34677b93a98f576142a2affde57ea2e7b461f515fd8a12453',
45+
'2.325.0@sha256:ab3fb968f7bcc8b34677b93a98f576142a2affde57ea2e7b461f515fd8a12453',
4646
);
4747
});
4848

tests/utils/get-latest-runner-images.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ describe('getLatestRunnerImages', () => {
4242
const result = await getLatestRunnerImages(mockOctokit);
4343
expect(result).toEqual({
4444
archDigests: {
45-
amd64: 'sha256:amd64digest',
46-
arm64: 'sha256:arm64digest',
45+
amd64: '2.325.0@sha256:amd64digest',
46+
arm64: '2.325.0@sha256:arm64digest',
4747
},
4848
latestVersion: '2.325.0',
4949
});

0 commit comments

Comments
 (0)