-
Notifications
You must be signed in to change notification settings - Fork 10
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
build: roll windows arc images automatically #124
Open
MarshallOfSound
wants to merge
2
commits into
main
Choose a base branch
from
arc-windows
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
lib | ||
node_modules | ||
coverage | ||
.envrc | ||
This file contains 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 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,34 @@ | ||
import { Octokit } from '@octokit/rest'; | ||
import { MAIN_BRANCH, REPOS } from '../constants'; | ||
import { getOctokit } from './octokit'; | ||
|
||
const WINDOWS_IMAGE_REGEX = /electronarc\.azurecr\.io\/win-actions-runner:main-[a-f0-9]{7}@sha256:[a-f0-9]{64}/; | ||
// TODO: Also roll the linux ARC container | ||
// const LINUX_IMAGE_REGEX = | ||
// /ghcr\.io\/actions\/actions-runner:[0-9]+\.[0-9]+\.[0-9]+@sha256:[a-f0-9]{64}/; | ||
|
||
export async function getFileContent(octokit: Octokit, filePath: string, ref = MAIN_BRANCH) { | ||
const { data } = await octokit.repos.getContent({ | ||
...REPOS.electronInfra, | ||
path: filePath, | ||
ref, | ||
}); | ||
if ('content' in data) { | ||
return { raw: Buffer.from(data.content, 'base64').toString('utf8'), sha: data.sha }; | ||
} | ||
throw 'wat'; | ||
} | ||
|
||
export const currentWindowsImage = (content: string) => { | ||
return content.match(WINDOWS_IMAGE_REGEX)?.[0]; | ||
}; | ||
|
||
export const didFileChangeBetweenShas = async (file: string, sha1: string, sha2: string) => { | ||
const octokit = await getOctokit(); | ||
const [start, end] = await Promise.all([ | ||
await getFileContent(octokit, file, sha1), | ||
await getFileContent(octokit, file, sha2), | ||
]); | ||
|
||
return start.raw.trim() !== end.raw.trim(); | ||
}; |
This file contains 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 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,144 @@ | ||
import * as debug from 'debug'; | ||
|
||
import { MAIN_BRANCH, REPOS } from '../constants'; | ||
import { getOctokit } from './octokit'; | ||
import { PullsListResponseItem } from '../types'; | ||
import { Octokit } from '@octokit/rest'; | ||
import { getInfraPRText } from './pr-text'; | ||
import { getFileContent } from './arc-image'; | ||
|
||
export async function rollInfra( | ||
rollKey: string, | ||
bumpSubject: string, | ||
newShortVersion: string, | ||
filePath: string, | ||
newContent: string, | ||
): Promise<any> { | ||
const d = debug(`roller/infra/${rollKey}:rollInfra()`); | ||
const octokit = await getOctokit(); | ||
|
||
const branchName = `roller/infra/${rollKey}`; | ||
const shortRef = `heads/${branchName}`; | ||
const ref = `refs/${shortRef}`; | ||
|
||
const { owner, repo } = REPOS.electronInfra; | ||
|
||
// Look for a pre-existing PR that targets this branch to see if we can update that. | ||
let existingPrsForBranch: PullsListResponseItem[] = []; | ||
try { | ||
existingPrsForBranch = (await octokit.paginate('GET /repos/:owner/:repo/pulls', { | ||
head: branchName, | ||
owner, | ||
repo, | ||
state: 'open', | ||
})) as PullsListResponseItem[]; | ||
} catch {} | ||
|
||
const prs = existingPrsForBranch.filter((pr) => | ||
pr.title.startsWith(`build: bump ${bumpSubject}`), | ||
); | ||
|
||
const defaultBranchHeadSha = ( | ||
await octokit.repos.getBranch({ | ||
owner, | ||
repo, | ||
branch: MAIN_BRANCH, | ||
}) | ||
).data.commit.sha; | ||
|
||
if (prs.length) { | ||
// Update existing PR(s) | ||
for (const pr of prs) { | ||
d(`Found existing PR: #${pr.number} opened by ${pr.user.login}`); | ||
|
||
// Check to see if automatic infra roll has been temporarily disabled | ||
const hasPauseLabel = pr.labels.some((label) => label.name === 'roller/pause'); | ||
if (hasPauseLabel) { | ||
d(`Automatic updates have been paused for #${pr.number}, skipping infra roll.`); | ||
continue; | ||
} | ||
|
||
d(`Attempting infra update for #${pr.number}`); | ||
const { raw: currentContent, sha: currentSha } = await getFileContent( | ||
octokit, | ||
filePath, | ||
pr.head.ref, | ||
); | ||
if (currentContent.trim() !== newContent.trim()) { | ||
await updateFile( | ||
octokit, | ||
bumpSubject, | ||
newShortVersion, | ||
filePath, | ||
newContent, | ||
branchName, | ||
currentSha, | ||
); | ||
} | ||
|
||
await octokit.pulls.update({ | ||
owner, | ||
repo, | ||
pull_number: pr.number, | ||
...getInfraPRText(bumpSubject, newShortVersion), | ||
}); | ||
} | ||
} else { | ||
try { | ||
d(`roll triggered for ${bumpSubject}=${newShortVersion}`); | ||
|
||
try { | ||
await octokit.git.getRef({ owner, repo, ref: shortRef }); | ||
d(`Ref ${ref} already exists, deleting`); | ||
await octokit.git.deleteRef({ owner, repo, ref: shortRef }); | ||
} catch { | ||
// Ignore | ||
} finally { | ||
d(`Creating ref=${ref} at sha=${defaultBranchHeadSha}`); | ||
await octokit.git.createRef({ owner, repo, ref, sha: defaultBranchHeadSha }); | ||
} | ||
|
||
const { sha: currentSha } = await getFileContent(octokit, filePath); | ||
|
||
await updateFile( | ||
octokit, | ||
bumpSubject, | ||
newShortVersion, | ||
filePath, | ||
newContent, | ||
branchName, | ||
currentSha, | ||
); | ||
|
||
d(`Raising a PR for ${branchName} to ${repo}`); | ||
await octokit.pulls.create({ | ||
owner, | ||
repo, | ||
base: MAIN_BRANCH, | ||
head: `${owner}:${branchName}`, | ||
...getInfraPRText(bumpSubject, newShortVersion), | ||
}); | ||
} catch (e) { | ||
d(`Error rolling ${owner}/${repo} to ${newShortVersion}`, e); | ||
} | ||
} | ||
} | ||
|
||
async function updateFile( | ||
octokit: Octokit, | ||
bumpSubject: string, | ||
newShortVersion: string, | ||
filePath: string, | ||
newContent: string, | ||
branchName: string, | ||
currentSha: string, | ||
) { | ||
await octokit.repos.createOrUpdateFileContents({ | ||
...REPOS.electronInfra, | ||
path: filePath, | ||
message: `build: bump ${bumpSubject} in ${filePath} to ${newShortVersion}`, | ||
content: Buffer.from(newContent).toString('base64'), | ||
branch: branchName, | ||
sha: currentSha, | ||
}); | ||
} |
This file contains 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,9 @@ | ||
import { rollWindowsArcImage } from './windows-image-handler'; | ||
|
||
if (require.main === module) { | ||
rollWindowsArcImage().catch((err: Error) => { | ||
console.log('Windows Image Cron Failed'); | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
} |
This file contains 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,92 @@ | ||
import * as debug from 'debug'; | ||
|
||
import { | ||
REPOS, | ||
WINDOWS_DOCKER_IMAGE_NAME, | ||
ARC_RUNNER_ENVIRONMENTS, | ||
MAIN_BRANCH, | ||
} from './constants'; | ||
import { getOctokit } from './utils/octokit'; | ||
import { currentWindowsImage, didFileChangeBetweenShas } from './utils/arc-image'; | ||
import { rollInfra } from './utils/roll-infra'; | ||
|
||
async function getLatestVersionOfImage() { | ||
const octokit = await getOctokit(); | ||
// return a list of tags for the repo, filter out any that aren't valid semver | ||
const versions = await octokit.paginate( | ||
'GET /orgs/{org}/packages/{package_type}/{package_name}/versions', | ||
{ | ||
org: REPOS.electronInfra.owner, | ||
package_type: 'container', | ||
package_name: WINDOWS_DOCKER_IMAGE_NAME, | ||
}, | ||
); | ||
|
||
let best = null; | ||
let bestMainTag = null; | ||
for (const version of versions) { | ||
// Only images built from main should be bumped to | ||
const mainTag = version.metadata?.container?.tags?.find((t) => t.startsWith(`${MAIN_BRANCH}-`)); | ||
if (!mainTag) continue; | ||
if (!best) { | ||
best = version; | ||
bestMainTag = mainTag; | ||
continue; | ||
} | ||
|
||
if (new Date(best.created_at).getTime() < new Date(version.created_at).getTime()) { | ||
best = version; | ||
bestMainTag = mainTag; | ||
} | ||
} | ||
return [`electronarc.azurecr.io/win-actions-runner:${bestMainTag}@${best.name}`, bestMainTag]; | ||
} | ||
|
||
const WINDOWS_IMAGE_DOCKERFILE_PATH = 'docker/windows-actions-runner/Dockerfile'; | ||
|
||
export async function rollWindowsArcImage() { | ||
const d = debug(`roller/infra:rollWindowsArcImage()`); | ||
const octokit = await getOctokit(); | ||
|
||
const [latestWindowsImage, shortLatestTag] = await getLatestVersionOfImage(); | ||
|
||
for (const arcEnv of Object.keys(ARC_RUNNER_ENVIRONMENTS)) { | ||
d(`Fetching current version of "${arcEnv}" arc image in: ${ARC_RUNNER_ENVIRONMENTS[arcEnv]}`); | ||
|
||
const currentVersion = await octokit.repos.getContent({ | ||
owner: REPOS.electronInfra.owner, | ||
repo: REPOS.electronInfra.repo, | ||
path: ARC_RUNNER_ENVIRONMENTS[arcEnv], | ||
}); | ||
const data = currentVersion.data; | ||
if ('content' in data) { | ||
const currentContent = Buffer.from(data.content, 'base64').toString('utf8'); | ||
const currentImage = currentWindowsImage(currentContent); | ||
|
||
if (currentImage !== latestWindowsImage) { | ||
const currentSha = currentImage.split(`${MAIN_BRANCH}-`)[1].split('@')[0]; | ||
if ( | ||
await didFileChangeBetweenShas( | ||
WINDOWS_IMAGE_DOCKERFILE_PATH, | ||
currentSha, | ||
shortLatestTag.split('-')[1], | ||
) | ||
) { | ||
d(`Current image in "${arcEnv}" is outdated, updating...`); | ||
const newContent = currentContent.replace(currentImage, latestWindowsImage); | ||
await rollInfra( | ||
`${arcEnv}/windows-image`, | ||
'windows arc image', | ||
shortLatestTag, | ||
ARC_RUNNER_ENVIRONMENTS[arcEnv], | ||
newContent, | ||
); | ||
} else { | ||
d( | ||
`Current image in "${arcEnv}" is not latest sha but is considered up-to-date, skipping...`, | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but do we need this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really, I just ignore it in any project I touch that has local dev secrets. It's just because I use https://direnv.net/