Skip to content

multi stage dockerfile with githb actions along with On-demand self-hosted AWS EC2 runner for GitHub Actions

Notifications You must be signed in to change notification settings

shivaniii23/python-multi-stage-github-actions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

We have a simple one file project to build a multi stage docker image.

Run the following commands to see the difference in the image sizes of 2 docker images- docker build -t app . docker build -t multi_stage -f Dockerfile_with_multi_stage .

On-demand self-hosted AWS EC2 runner for GitHub Actions

Use the following steps to prepare your workflow for running on your EC2 self-hosted runner:

  1. Create IAM user with AWS access keys Create an IAM user with the required EC2 roles. To create, go to IAM -> Create user. image image

Then click on next -> create. Then we need to create the access key for this user. Click on the user -> Create access key . image image

Click on next -> create access key. image

Download the .csv file which will have the access key and the secret key. Now, we will add these keys in our GitHub repository that we will use for this project. Go to your repository on GitHub -> settings -> Secrets and variables -> Actions -> Secrets -> Repository secrets. image

Add the secret key and access key from the csv we downloaded and the AWS_REGION.(eg - us-east-1).

  1. Prepare GitHub personal access token Create a new GitHub personal access token with the repository scope. The action will use the token for self-hosted runners’ management in the GitHub account on the repository level. Add the token to GitHub secrets. Go to your GitHub profile on GitHub -> settings -> Developer Settings -> Personal Access Tokens -> Tokens(classic) -> Generate new token -> Generate new token(classic) -> Add note, set expiration date -> Select all the boxes in scope and -> Generate token. You will get the token. Copy it because it won’t be accessible afterwards. image

Now, we will add this token in our GitHub repository that we will use for this project. We will also add docker hub credentials so that we can push the docker image. Go to your repository on GitHub -> settings -> Secrets and variables -> Actions -> Secrets -> Repository secrets. image

  1. Prepare EC2 image Create a new EC2 instance based on any Linux distribution you need. Select a key-pair and new security group and create the instance. Connect to the instance using SSH, install docker, then enable docker service – sudo apt-get update sudo apt install docker.io -y sudo systemctl enable --now docker sudo usermod -aG docker $USER && newgrp docker sudo systemctl restart docker Install any other tools required for your workflow. Create a new EC2 image (AMI) from the instance. image

Select the instance -> Actions -> Image and templates -> Create image -> Enter the name and click on Create Image. image

Check the status of the image and make sure it is in ‘available’ state. Remove the instance if not required anymore. image

  1. Prepare VPC with subnet and security group Create a new VPC and a new subnet in it. Or use the existing VPC and subnet. We will use the default VPC. image

Create a new security group for the runners in the VPC. Only the inbound traffic on port 22 is required. Keep the outbound traffic port as it is. image image

  1. Configure the GitHub workflow Create a new GitHub Actions workflow or edit the existing one. We have a project which creates a docker container with multi stage builds. Github repository - https://github.com/shivaniii23/python-multi-stage-github-actions.git We have “deploy.yml” file which has the job named ‘My First GitHub Actions’ which starts the EC2 runner on push, executes the job, and terminates the runner after everything. image image image image

Here, we can see that the docker image was built and pushed to docker hub.

About

multi stage dockerfile with githb actions along with On-demand self-hosted AWS EC2 runner for GitHub Actions

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published