skip to Main Content

I have an ubuntu EC2 instance where the docker container runs. I need a simple CD architecture that will pull code from GitHub and run docker build... and docker run ... on my EC2 instance after every code push.

I’ve tried with GitHub actions and I’m able to connect to the EC2 instance but it gets stuck after docker commands.

name: scp files
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Pull changes and run docker
      uses: fifsky/ssh-action@master
      with:
        command: |
          cd test_ec2_deployment
          git pull
          sudo docker build --network host -f Dockerfile -t test .
          sudo docker run -d --env-file=/home/ubuntu/.env -ti test
        host: ${{ secrets.HOST }}
        user: ubuntu
        key: ${{ secrets.SSH_KEY }}
        args: "-tt"

output

Step 12/13 : RUN /usr/bin/crontab /etc/cron.d/cron-job
 ---> Running in 52a5a0174958
Removing intermediate container 52a5a0174958
 ---> badf6fdaf774
Step 13/13 : CMD printenv > /etc/environment && cron -f
 ---> Running in 0e9fd12db4f7
Removing intermediate container 0e9fd12db4f7
 ---> 888a2a9e5910
Successfully built 888a2a9e5910
Successfully tagged test:latest

Also, I’ve tried to separate docker commands into .sh script but it didn’t help. Here is an issue for that https://github.com/fifsky/ssh-action/issues/30.

I wonder if it’s possible to implement this CD structure using AWS CodePipeline or any other AWS services. Also, I’m not sure is it too complicated to set up Jenkins for this case.

2

Answers


  1. Chosen as BEST ANSWER

    As @Myz suggested, this can be done using GitHub actions with AWS ECR and AWS ECS. Below are some articles which I was following to solve the issue:


  2. This is definitely possible using AWS CodePipeline but it will require you to have a Lambda function since you want to deploy your container to your own EC2 instance (which I think is not necessary unless you have a specific use-case). This is how your pipeline would look like;

    AWS CodePipline stages:

    • Source: Connect your GitHub repository. In the background, it will automatically clone code from your Git repo, zip it, and store it in S3 to be used by the next stage. There are other options as well if you want to do it all by yourself. For example;
      • using your GitHub actions, you zip the file and store it in S3 bucket. On the AWS side, you will add S3 as a source and provide the bucket and object key so whenever this object version changes, it will trigger the pipeline.
      • You can also use GitHub actions to actually build your Docker image and push it to AWS ECR (container registry) and totally skip build stage. So, either do build on GitHub or on AWS side, upto you.
    • Build: For this stage (if you decide to build using AWS), you can either use Jenkins or AWS Codebuild. I have used AWS Codebuild, so IMO this is fairly easy and quick solution for the build stage. At this stage, it will use the zip file in S3 bucket, unzip it, build your Docker container image and push it to AWS ECR.
    • Deploy: Since you want to run your Docker container on EC2, there is no straight forward way to do this. However, you can utilize the power of Lambda function to run your image on your own EC2 instance. But you will have to code your function which could be tricky. I would highly recommend using AWS ECS to run your container in a more manageable way. You can essentially do all the things that you want to do in your EC2 instance to your ECS container.
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search