Is there a way to get a github PR merge to trigger an EC2 instance to start?

时间:2016-04-21 22:17:40

标签: amazon-web-services github

We're trying to get an EC2 instance to start whenever we merge a Pull Request in github.

I have this in crontab

@reboot /home/user/server-start.sh

Which does everything I want to happen (like git pull origin master) when the instance starts up, and that script works well.

I've looked at AWS CodeDeploy but I can't see a way to have that just turn an instance on (this server will usually be turned off, it just needs to turn on and do some stuff when we merge a PR, then turn off again)

I'm not sure what the best approach for this would be, so any pointers would be great.

Here's the startup script and nginx server config file, in case they are helpful.

/home/user/server-startup.sh

#!/bin/bash

# get latest master branch
cd /path/to/repo
sudo git pull origin master

# run standard preparation script
sudo bash /home/user/scripts/prepare-app.sh

# run any custom commands for this update
sudo bash /path/to/repo/prepare-app.sh

# change http code to 200
sudo sed -i -e 's/return 500/return 200/g' /etc/nginx/sites-available/status

# restart nginx
sudo service nginx restart

# change http code to 500
sudo sed -i -e 's/return 200/return 500/g' /etc/nginx/sites-available/status

/etc/nginx/site-available/status

server {
    listen      80 default_server;
    server_name status.mydomain.com;

    location / {
        return 500;
    }
}

1 个答案:

答案 0 :(得分:2)

如果您的实例刚刚停止,未终止,那么您可以使用AWS CLI启动您的实例:

aws ec2 start-instances --instance-ids i-1348636c

文档:https://aws.amazon.com/cli/

可以使用git hook触发此代码:https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

您的脚本的其余部分仅在实例启动时运行,因此您需要在服务器上进行某种运行状况检查。

如果您想要更易于管理的内容,也可以考虑CI / CD工具,例如Bamboo / Codeship。