YesCoding

Go to Korean
search:

Deploy to EC2 with Github Actions and codeDeploy

github actions introduction

github actions introduction

Go to Korean version

Recently, I have started a new project so that I need to set up a deployment strategy. As There are diverse way to deploy out there, I discussed with my colleague on it. My first choice was a jenkins, Colleague's choice was Github Actions.

The reason I suggest Jenkins

I have used Jenkins for 2 years so Jenkins is familiar to me. And I felt Jenkins is really handy to deal with the flow 'dev => feature => production'.

The reason My colleague suggests Github Actions

We manage our source code in Github so It is easy to manage a deployment process at the same place. And for now, we just have only production level.

I thought It makes sense, Especially I agreed that The Fast deployment speed is the most important thing at the moment. So We choose Github Actions.

Task Objectives

When you push the code you worked on to the main(master) branch, use AWS codeDeploy to distribute the code to ec2 instance.

Required Pre-Working

(This course is not covered in detail in this post.)

  1. Create an ec2
  2. Create new application and create deployment groups in AWS codeDeploy service.

github actions introduction

github actions introduction

github actions Working Process

  1. On the Actions menu, click New workflow.

    github actions introduction

    github actions introduction

  2. There is no ec2 in popular service, Choose Manual workflow.

    github actions introduction

    github actions introduction

  3. Fill the yml file created in .github/workflows/

name: // name of workflow. You can input whatever you want. on: push: // Type the github action to trigger this workflow. branches: [ main ] workflow_dispatch: jobs: deploy: // The name of job which will be executed in workflow. runs-on: ubuntu-latest // It was okay for me to use as default. steps: - uses: actions/checkout@v2 // It was okay for me to use as default. // You need to do ec2 access configuration before deploying to ec2 with codeDeploy - name: Configure AWS credentials from Production account uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id: ${{ secrets.PROD_AWS_ACCESS_KEY_ID }} // You can register secrets in the secrets menu in the repo settings. // Add the access key and key id that you received when creating AWS credential. aws-secret-access-key: ${{ secrets.PROD_AWS_SECRET_ACCESS_KEY }} aws-region: ap-northeast-2 // Install aws cli and enter the command below. // GITHUB_REPOSITORY and GITHUB_SHA are automatically reflected as repo and commit_id at the time of push. - name: deploy with aws codeDeploy run: aws deploy create-deployment --application-name ${The name of application in codeDeploy} --deployment-group-name ${The name of deploy group in codeDeploy} --deployment-config-name CodeDeployDefault.AllAtOnce --github-location repository=${GITHUB_REPOSITORY},commitId=${GITHUB_SHA}

Test the above process to see if it works well. Enter the Actions menu of the repo to view the progress.

Recommend Post
© Copyright 2022, YesCoding