DEV Community

Cover image for Host Your Resume on AWS EC2 with a CI/CD Setup Using GitHub Actions
Yeshwanth
Yeshwanth

Posted on • Updated on

Host Your Resume on AWS EC2 with a CI/CD Setup Using GitHub Actions

This article helps you understand how you can automatically deploy your code to AWS EC2 from GitHub

Step1: Create an EC2 Instance and Download the Key Pair.

Step2: Create Secrets in GitHub for the Repository

Step3: Creating your first workflow

Step4: Testing

Creating your first workflow
Create a .github/workflows directory in your repository on GitHub if this directory does not already exist.
In the .github/workflows directory, create a file named github-actions-ec2.yml.
Now your github-actions-ec2.yml should be present in .github/workflows/github-actions-ec2.yml in your repository

Start your file by defining jobs, jobs are the steps that you can define and see individual status reports when you see the logs in your Actions tab

jobs:
  deploy:
    name: Deploy to EC2
    runs-on: ubuntu-latest
Enter fullscreen mode Exit fullscreen mode

In the above block we have defined our job with name Deploy to EC2 and enforced it to run on latest Ubuntu by runs-on: ubuntu-latest line

Now, we need to checkout the pushed code to the runner by using a predefined action named actions/checkout@v2. The code responsible for this step should look like the following

steps:
  - name: Checkout the files
    uses: actions/checkout@v2
Enter fullscreen mode Exit fullscreen mode

Now, we are deploying the code to the server, in order to to do this we need to access the EC2 using ssh and perform rsync form the runner. For this we are going to use another GitHub action easingthemes/ssh-deploy

- name: Deploy to Server 1
  uses: easingthemes/ssh-deploy@main
  env:
    SSH_PRIVATE_KEY: ${ { secrets.EC2_SSH_KEY }}
    REMOTE_HOST: ${ { secrets.HOST_DNS }}
    REMOTE_USER: ${ { secrets.USERNAME }}
    TARGET: ${ { secrets.TARGET_DIR }}
Enter fullscreen mode Exit fullscreen mode

Note: You need to put the double parentheses together; I had to leave a space because my code formatter refuses to print it (:facepalm)

You need to fill in the secrets using GitHub Secrets that you can add in your repo, read GitHub Secrets

  • EC2_SSH_KEY: This will be your .pem file which you will use to login to the instance

  • HOST_DNS: Public DNS record of the instance, it will look something like this ec2-xx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com

  • USERNAME: Will be the username of the EC2 instance, usually ubuntu

  • TARGET_DIR: Is where you want to deploy your code.
    Once you add all these information your repo will look like thisGitHub Secrets

Image description

Trigger deployment only on push to master branch

Add the following code so that your actions only run when you push to main branch.

on:
  push:
    branches:
      - main
Enter fullscreen mode Exit fullscreen mode

The final .github/workflows/github-actions-ec2.yml should looks like the following

name: Push-to-EC2

# Trigger deployment only on push to main branch
on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: Deploy to EC2 on master branch push
    runs-on: ubuntu-latest

    steps:
      - name: Checkout the files
        uses: actions/checkout@v2

      - name: Deploy to Server 1
        uses: easingthemes/ssh-deploy@main
        env:
          SSH_PRIVATE_KEY: ${{ secrets.EC2_SSH_KEY }}
          REMOTE_HOST: ${{ secrets.HOST_DNS }}
          REMOTE_USER: ${{ secrets.USERNAME }}
          TARGET: ${{ secrets.TARGET_DIR }}

      - name: Executing remote ssh commands using ssh key
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST_DNS }}
          username: ${{ secrets.USERNAME }}
          key: ${{ secrets.EC2_SSH_KEY }}
          script: |
            sudo apt-get -y update
            sudo apt-get install -y apache2
            sudo systemctl start apache2
            sudo systemctl enable apache2
            cd home
            sudo mv * /var/www/html
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
darkmavis1980 profile image
Alessio Michelini

Just one typo, you said deploy to "master" branch, and then show "main" in the example ;-)

Collapse
 
yeshwanthlm profile image
Yeshwanth

Corrected! Thanks Alessio Michelini :)

Collapse
 
surinderpanwar05 profile image
surinder

err: Synchronizing state of apache2.service with SysV service script with /lib/systemd/systemd-sysv-install.
err: Executing: /lib/systemd/systemd-sysv-install enable apache2
err: mv: cannot move 'css' to '/var/www/html/css': Directory not empty
err: mv: cannot move 'dashbord' to '/var/www/html/dashbord': Directory not empty
err: mv: cannot move 'js' to '/var/www/html/js': Directory not empty
err: mv: cannot move 'main' to '/var/www/html/main': Directory not empty
err: mv: cannot move 'rocket' to '/var/www/html/rocket': Directory not empty
err: mv: cannot move 'script' to '/var/www/html/script': Directory not empty

hi these error showing. First deploy work perfectly fine. but when i did changes in existing files then its not deploy on aws EC2 ubuntu 22. please give me solution. your help will be really appreciate.

Collapse
 
yudiztaukirkatava profile image
Yudiz_TaukirKatava

In your commands, just use '-r' option. like, "mv -r css /var/www/html/"