Skip to content
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

Implement auto deploy on pull requests #24

Merged
merged 1 commit into from Jan 9, 2020

Conversation

imella
Copy link
Contributor

@imella imella commented Dec 19, 2019

Use 'auto_deploy_on: pr' configuration to enable automatic deployments
in a pull request.

@colinjfw
Copy link
Collaborator

🎉 Thanks @imella I'll take a look at this as soon as I can, I guess the tests don't run on forks so I'll have to figure that out first here.

@colinjfw colinjfw added this to In progress in Roadmap Dec 20, 2019
@colinjfw
Copy link
Collaborator

I was reading through the GitHub events and I think there may be a simpler way to write this logic. There is a pull request "synchronized" event which I believe is fired when a PR is pushed:
https://developer.github.com/v3/activity/events/types/#pullrequestevent

We could add a new listener for this event and just trigger the automatic deployment logic on this event specifically. It would save an extra call to getAssociatedPullRequests as well.

@colinjfw colinjfw mentioned this pull request Dec 24, 2019
@colinjfw colinjfw removed this from In progress in Roadmap Dec 25, 2019
@imella
Copy link
Contributor Author

imella commented Dec 26, 2019

You're right! I'll check that "synchronized" event and make the necessary changes, thanks!

@imella imella force-pushed the add-auto-deploy-on-pr branch 2 times, most recently from bdfce92 to 65e8dde Compare December 27, 2019 17:59
colinjfw
colinjfw previously approved these changes Jan 3, 2020
Copy link
Collaborator

@colinjfw colinjfw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great!

I like using the original PR code so if there are errors this will write comments into the pull request with the error messages.

@colinjfw colinjfw dismissed their stale review January 3, 2020 15:32

Missed something

@colinjfw
Copy link
Collaborator

colinjfw commented Jan 3, 2020

@imella tested this out, I realized this doesn't wait for the necessary checks before deploying like the code in the src/auto.ts. I think that we need to plug this into the code in there so that it will use the same logic across the board.

Thanks so much for your patience here :)

Going to play around with this for a bit...

@colinjfw
Copy link
Collaborator

colinjfw commented Jan 3, 2020

Something like this will create a 'watch' on the new ref synchronized by the pull request. This will then wait for the necessary checks before continuing to deploy.

I think this is probably a better situation since most people may need to build containers or other artifacts before deploying in a PR.

$ git diff src/
diff --git a/src/auto.ts b/src/auto.ts
index 7e48a7e..7909807 100644
--- a/src/auto.ts
+++ b/src/auto.ts
@@ -31,6 +31,7 @@ export function auto(
    */
   async function addWatch(
     context: Context,
+    matchRef: string, // The ref to match for auto deployments.
     ref: string,
     sha: string,
     repository: PayloadRepository
@@ -40,7 +41,7 @@ export function auto(
       const conf = await config(context.github, context.repo());
       for (const target in conf) {
         const targetVal = conf[target]!;
-        if (!match(targetVal.auto_deploy_on, ref)) {
+        if (!match(targetVal.auto_deploy_on, matchRef)) {
           continue;
         }
         context.log.info(
@@ -283,8 +284,28 @@ export function auto(
     await addWatch(
       context,
       context.payload.ref,
+      context.payload.ref,
       context.payload.after,
       context.payload.repository
     );
   });
+
+  app.on("pull_request.synchronize", async context => {
+    await addWatch(
+      context,
+      "pr", // If auto_deploy_on matches "pr" then deploy.
+      context.payload.pull_request.head.ref,
+      context.payload.pull_request.head.sha,
+      context.payload.repository,
+    )
+  })
+  app.on("pull_request.opened", async context => {
+    await addWatch(
+      context,
+      "pr", // If auto_deploy_on matches "pr" then deploy.
+      context.payload.pull_request.head.ref,
+      context.payload.pull_request.head.sha,
+      context.payload.repository,
+    )
+  })
 }

@imella
Copy link
Contributor Author

imella commented Jan 6, 2020

@imella tested this out, I realized this doesn't wait for the necessary checks before deploying like the code in the src/auto.ts. I think that we need to plug this into the code in there so that it will use the same logic across the board.

Thanks so much for your patience here :)

Going to play around with this for a bit...

Oh you're right, it doesn't wait for anything. So using 'watches' will only deploy if all checks passes, doesn't it?

I'll give it a go with those changes, thanks!

Use 'auto_deploy_on: pr' configuration to enable automatic deployments
in a pull request.

FIXES: deliverybot#10
@jbergstroem
Copy link

Excited to see this in production :)

@colinjfw
Copy link
Collaborator

colinjfw commented Jan 9, 2020

Thanks very much @imella!

@colinjfw colinjfw merged commit 9a077ca into deliverybot:master Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants