Skip to content

Commit

Permalink
add build artifact creation
Browse files Browse the repository at this point in the history
  • Loading branch information
drewbo committed Dec 26, 2023
1 parent f6b442c commit 7aae066
Showing 1 changed file with 56 additions and 29 deletions.
85 changes: 56 additions & 29 deletions scripts/localstack-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const s3 = new S3Client({
},
});

const fakeIndex = `<html>
const fakeHtml = `<html>
<head>
<title>site</title>
</head>
Expand All @@ -30,37 +30,41 @@ Website
</html>
`;

const cmd = new CreateBucketCommand({ Bucket: config.s3BuildLogs.bucket });
s3.send(cmd);
async function initSitesAndBuckets() {
// create Build Logs bucket
const cmd = new CreateBucketCommand({ Bucket: config.s3BuildLogs.bucket });
await s3.send(cmd);

Site.findAll()
.then((sites) => {
sites.forEach(async (site) => {
const siteCmd = new CreateBucketCommand({ Bucket: site.awsBucketName });
await s3.send(siteCmd);
const folders = ['site', 'demo'];
folders.forEach((folder) => {
const folderCmd = new PutObjectCommand({
Body: fakeIndex,
Bucket: site.awsBucketName,
Key: `${folder}/${site.owner}/${site.repository}/index.html`,
// create a bucket and some files for each Site
await Site.findAll()
.then(async (sites) => {
await Promise.all(sites.map(async (site) => {
const siteCmd = new CreateBucketCommand({ Bucket: site.awsBucketName });
await s3.send(siteCmd);
const folders = ['site', 'demo'];
folders.forEach(async (folder) => {
const folderCmd = new PutObjectCommand({
Body: fakeHtml,
Bucket: site.awsBucketName,
Key: `${folder}/${site.owner}/${site.repository}/index.html`,
});
await s3.send(folderCmd);
});
s3.send(folderCmd);
});
const fakePreviews = ['branch1', 'branch2', 'branch3'];
fakePreviews.forEach((branch) => {
const previewCmd = new PutObjectCommand({
Body: fakeIndex,
Bucket: site.awsBucketName,
Key: `preview/${site.owner}/${site.repository}/${branch}/index.html`,
const fakePreviews = ['branch1', 'branch2', 'branch3'];
fakePreviews.forEach(async (branch) => {
const previewCmd = new PutObjectCommand({
Body: fakeHtml,
Bucket: site.awsBucketName,
Key: `preview/${site.owner}/${site.repository}/${branch}/index.html`,
});
await s3.send(previewCmd);
});
s3.send(previewCmd);
});
});
})
.catch(err => console.error(err));
}));
})
.catch(err => console.error(err));
}

console.log('Uploading logs to S3');
// archive older build logs
async function archiveLogs() {
const date = moment().subtract(3, 'days').startOf('day');

Expand All @@ -78,4 +82,27 @@ async function archiveLogs() {
builds.forEach(build => BuildLogs.archiveBuildLogsForBuildId(build.id));
}

archiveLogs();
// create artifacts for Build Tasks
async function createBuildTaskArtifacts() {
const artifactedTasks = await BuildTask.findAll({
where: {
artifact: {
[Op.not]: null,
},
},
include: [{ model: Build, include: [Site] }],
});
artifactedTasks.forEach((task) => {
console.log(task.Build.Site)
const artifactCmd = new PutObjectCommand({
Body: fakeHtml,
Bucket: task.Build.Site.awsBucketName,
Key: task.artifact,
});
s3.send(artifactCmd);
});
}

initSitesAndBuckets()
.then(archiveLogs)
.then(createBuildTaskArtifacts);

0 comments on commit 7aae066

Please sign in to comment.