CI/CD Flow w/ Github Actions to EC2 w/ Ubuntu Docker and Nginx
For use with: https://github.com/Two-Trees-Digital/turbo-temp
EC2 Setup
- Go to Amazon account and create a new EC2
- Choose the Ubuntu option
- Choose a system size that suits your project needs
- Medium and below is probably the most you’ll need with hobby projects
- Create a .pem permissions file to be able to ssh into the system
- Allot 12-20 GBs of space for system
- Create Instance
- Once Instance is created, go ahead and open a terminal where the .pem file got downloaded
- Probably /user/downloads
- Press ‘connect to instance’ and select the ssh tab
- Copy paste that command into terminal
- You’ve now connected to your instance
- Proceed with downloading Docker
Docker
Nginx EC2
-
https://www.digitalocean.com/community/tutorials/how-to-install-nginx-on-ubuntu-20-04
-
Once through setup, you’ll need to go into your config file and input your settings
-
You’ll see a file named default here
- We want to delete this
-
We now create a new default file with:
-
Copy pasta your config settings:
Config File:
-
Write out w/ ‘ctrl + O’
-
Exit w/ ‘ctrl + X’
-
We now want to cd out and into sites-enabled and delete the default file there too
-
Now we’re going to run a link command
Link Command:
- Now restart the nginx server and we’re good to go!
Nginx Command:
Github Actions
Set up of the actions are pretty straight forward
- Go to your github repository, and go to the settings tab
- Then go to Actions > Runners
- Create New Self-Hosted Runner
- Input the commands it prompts you with 1 by 1 at the root of your EC2
- After sudo ./svc.sh start
- Check the runners tab again to see your idle runner ready to go
Building the project
- Running sudo docker ps shows there isn’t a built image of a container yet in the EC2
- Make a test commit to the production or main branch of your code
- Check the Actions tab in your repository to see a workflow run has been created
- After the initial build completes, which may take a while, give sudo docker ps another try to see your container created!