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
cd / etc / nginx;cd sites-availablels; - 
You’ll see a file named default here
- We want to delete this
 
sudo rm -rf default - 
We now create a new default file with:
sudo nano default - 
Copy pasta your config settings:
 
Config File:
`server {``listen 80;``location / {``proxy_pass http://127.0.0.1:3000;``}``location /api/v1 {``rewrite ^/api/v1(/.*)$ $1 break;``proxy_pass http://127.0.0.1:3001;``}``location /dashboard/ {``rewrite ^/dashboard(/.*)$ $1 break;``proxy_pass http://127.0.0.1:3002;``}``}`;- 
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
cd ..cd sites-enabledsudo rm -rf default - 
Now we’re going to run a link command
 
Link Command:
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/default- Now restart the nginx server and we’re good to go!
 
Nginx Command:
sudo /etc/init.d/nginx startsudo /etc/init.d/nginx restartGithub 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!
 
Building Image
docker-compose build turbo-temp
 
docker-compose up --no-deps -d turbo-temp