Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
acbf23c
updates
sodiadrhain Jul 21, 2024
5073e06
added README
GideonIsBuilding Jul 21, 2024
7eb48b3
added deployment stages to documentation
sodiadrhain Jul 21, 2024
3a22621
updated readme
sodiadrhain Jul 21, 2024
aa8d9f9
renamed image
sodiadrhain Jul 21, 2024
bb3e0f9
added image
sodiadrhain Jul 21, 2024
a305c50
change installation id
sodiadrhain Jul 21, 2024
cffdc72
change installation id
sodiadrhain Jul 21, 2024
8850e29
update code
sodiadrhain Jul 21, 2024
ec84173
Update README.md
MujeebAyokunle Jul 21, 2024
794622c
update readme
Toluwalemi Jul 21, 2024
bdca546
Update README.md
Shirlyne20 Jul 21, 2024
7677b13
Merge pull request #4 from sodiadrhain/tolu/update-readme
sodiadrhain Jul 21, 2024
226e3a8
update readme
sodiadrhain Jul 21, 2024
1181d7b
Merge branch 'main' into patch-2
MujeebAyokunle Jul 21, 2024
bcecdaa
wande
Jul 21, 2024
eb39426
Merge pull request #3 from MujeebAyokunle/patch-2
sodiadrhain Jul 21, 2024
3afd3aa
Merge pull request #9 from sodiadrhain/fix-documentation
sodiadrhain Jul 21, 2024
786889d
updated
kenneth-stack Jul 21, 2024
9daa9b2
Merge pull request #10 from sodiadrhain/new
sodiadrhain Jul 21, 2024
e501622
wande corrections
Jul 21, 2024
681ba67
Update README.md
Adeshile2 Jul 21, 2024
e216c19
Update README.md
Adeshile2 Jul 21, 2024
92cf30b
Merge pull request #11 from sodiadrhain/fix-documentation
sodiadrhain Jul 21, 2024
a694b60
Merge pull request #13 from sodiadrhain/Adeshile2-patch-1
Adeshile2 Jul 21, 2024
e80e7d7
Update README.md
Abdul-Barri Jul 21, 2024
59ce39a
Merge pull request #14 from sodiadrhain/abdul-barri/update-readme
sodiadrhain Jul 21, 2024
ca398e2
updatefort
fortis-07 Jul 21, 2024
57b92b0
Merge pull request #16 from sodiadrhain/forttcontri
sodiadrhain Jul 22, 2024
4fc56cd
updateRM
fortis-07 Jul 22, 2024
cdd712d
Merge pull request #17 from sodiadrhain/hn-contributor
sodiadrhain Jul 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
522 changes: 522 additions & 0 deletions README.md

Large diffs are not rendered by default.

Binary file added images/Screenshot 2024-07-20 181055.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Screenshot 2024-07-20 181326.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/alpha bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/alpha-bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/bot install.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/closed-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/create-pr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/deployment-complete.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/dev settings.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/downloaded pvt keys.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/events subscription.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/github app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/github webhook.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/install on repo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/installing the app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/new bot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/ngrok.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/permissions.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/pr-being-deployed.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/private key.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/public page.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/read and write.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/realstepa.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/realstepb.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/realstepc.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/repo perm.png
Binary file added images/stepa.PNG
Binary file added images/stepb.PNG
Binary file added images/stepd.png
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ app.post('/webhook', async (req, res) => {
const prNumber = req.body.number;
const branchName = req.body.pull_request.head.ref;
const repoName = req.body.repository.full_name;
const forkName = req.body.pull_request.head.repo.full_name;
const installationId = req.body.installation.id

const repoUrl = `https://github.com/${repoName}.git`;
const repoBranchName = `${repoName.replace('/', '_')}_${branchName.replace('/', '_')}`;
console.log(repoBranchName)
const imageName = repoBranchName.toLowerCase();
const containerName = `${imageName}_pr_${prNumber}`.toLowerCase();
const forkRepoUrl = `https://github.com/${forkName}.git`;

if (action === 'opened' || action === 'reopened' || action === 'synchronize') {
try {
const preDeployMessage = 'The PR is currently being deployed...'
await addPRComment(repoName, prNumber, preDeployMessage);
await addPRComment(repoName, prNumber, preDeployMessage, installationId);

const postDeployMessage = await triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName);
await addPRComment(repoName, prNumber, postDeployMessage);
const postDeployMessage = await triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName, forkRepoUrl);
await addPRComment(repoName, prNumber, postDeployMessage, installationId);

console.log("Deployment completed and comment added!")
} catch (error) {
Expand All @@ -51,7 +54,7 @@ app.post('/webhook', async (req, res) => {
else if (action === 'closed') {
try {
const message = await removeDeployedContainer(containerName, imageName);
await addPRComment(repoName, prNumber, message);
await addPRComment(repoName, prNumber, message, installationId);
console.log('Container removed and comment added')
} catch (error) {
console.error('Error during deployment:', error);
Expand Down
41 changes: 31 additions & 10 deletions services/deploymentService.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function runCommand(command, args, cwd) {
}


export async function triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName) {
export async function triggerDeployment(repoUrl, branchName, prNumber, imageName, containerName, forkRepoUrl) {
const containerPort = process.env.containerPort || 5000
const hostPort = 9000 + (prNumber % 1000);
const folderName = imageName
Expand All @@ -37,18 +37,39 @@ export async function triggerDeployment(repoUrl, branchName, prNumber, imageName

if (folderExists) {
console.log('Repository folder exists. Changing directory and checking out branch...');
await runCommand(`cd ${folderName} && git checkout`, [branchName]);

await runCommand(`cd ${folderName} && git fetch --all`);
await runCommand(`cd ${folderName} && git branch -a`); // List all branches for debugging

// Check if the branch exists locally
try {
await runCommand(`cd ${folderName} && git checkout ${branchName}`);
} catch (error) {
console.log(`Branch ${branchName} not found locally. Fetching from remote...`);
if (forkRepoUrl !== repoUrl) {
await runCommand(`cd ${folderName} && git fetch ${forkRepoUrl} ${branchName}:${branchName}`);
await runCommand(`cd ${folderName} && git checkout ${branchName}`);
} else {
throw new Error(`Branch ${branchName} not found and forkRepoUrl is not provided`);
}
}

console.log('Pulling the remote branch for updates')
await runCommand(`git pull origin`, [branchName]);
} else {
await runCommand(`cd ${folderName} && git checkout ${branchName} && git pull origin ${branchName}`);

} else {

console.log('Repository folder does not exist. Cloning repository...');
await runCommand(`git clone`, [repoUrl, `${folderName}`], '.');

console.log('Checking out branch...');
await runCommand('git checkout', [branchName], `${folderName}`);
}


if (forkRepoUrl !== repoUrl) {
await runCommand(`cd ${folderName} && git fetch ${forkRepoUrl} ${branchName}:${branchName}`);
await runCommand(`cd ${folderName} && git checkout ${branchName}`);
} else {
await runCommand('git checkout', [branchName], `${folderName}`);
}
}

console.log('Building Docker image...');
await runCommand('docker build -t', [imageName, '.'], `${folderName}`);

Expand All @@ -59,7 +80,7 @@ export async function triggerDeployment(repoUrl, branchName, prNumber, imageName
await runCommand('docker run -d --name', [containerName, `-p ${hostPort}:${containerPort}`, imageName]);

// build the URL of the deployed app and return as part of a message
const deploymentLink = await getNgrokUrl(hostPort);
const deploymentLink = `${process.env.DEPLOYMENT_BASE_URL}:${hostPort}`;
return `Deployment completed! View it here: ${deploymentLink}`;

} catch (error) {
Expand Down
5 changes: 2 additions & 3 deletions services/repositoryService.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import path from "path";
const WEBHOOK_SECRET = process.env.WEBHOOK_SECRET;
const APP_ID = process.env.APP_ID;
const PRIVATE_KEY_PATH = process.env.PRIVATE_KEY_PATH;
const INSTALLATION_ID = process.env.INSTALLATION_ID;

let encoder = new TextEncoder();

export async function addPRComment(repoName, prNumber, message) {
export async function addPRComment(repoName, prNumber, message, installationId) {

const privateKeyPath = path.resolve(PRIVATE_KEY_PATH);
const privateKey = fs.readFileSync(privateKeyPath, "utf8");
Expand All @@ -21,7 +20,7 @@ export async function addPRComment(repoName, prNumber, message) {
auth: {
appId: APP_ID,
privateKey: privateKey,
installationId: INSTALLATION_ID,
installationId,
},
});
const url = `/repos/${repoName}/issues/${prNumber}/comments`;
Expand Down