This repository contains a flexible GitHub Actions workflow for deploying applications to Amazon EKS (Elastic Kubernetes Service). The workflow automates the process of building a Docker image, pushing it to Amazon ECR (Elastic Container Registry), and deploying it to an EKS cluster.
- Configurable deployment branch
- Customizable AWS region, ECR repository, and EKS cluster name
- Smart Dockerfile detection
- Automatic deployment creation or update
- Configurable application port and Kubernetes namespace
- Secure handling of AWS credentials
- Flexible image tagging
- Customizable kubectl version
- Adaptable deployment step
Before using this workflow, ensure you have the following:
- An AWS account with appropriate permissions for ECR and EKS
- An EKS cluster set up in your AWS account
- An ECR repository created in your AWS account
- AWS CLI configured with the necessary credentials
- kubectl installed and configured to interact with your EKS cluster
- A Dockerfile in your repository
-
Copy the workflow YAML file to your repository's
.github/workflows/directory. -
Set up the following secrets in your GitHub repository settings:
AWS_ACCESS_KEY_ID: Your AWS access key IDAWS_SECRET_ACCESS_KEY: Your AWS secret access key
-
Customize the workflow by setting the following variables in your GitHub repository settings or directly in the workflow file:
DEPLOY_BRANCH(default: 'main'): The branch that triggers the deploymentAWS_REGION(default: 'us-west-2'): Your AWS regionECR_REPOSITORY(default: 'my-app'): Your ECR repository nameSERVICE_NAME(default: 'MyService'): Your service nameEKS_CLUSTER_NAME(default: 'my-cluster'): Your EKS cluster nameENVIRONMENT(default: 'production'): The deployment environmentKUBECTL_VERSION(default: 'latest'): The kubectl version to useDOCKERFILE_PATH(optional): The path to your Dockerfile relative to the repository rootAPP_PORT(default: '80'): The port your application listens onK8S_NAMESPACE(default: 'default'): The Kubernetes namespace to deploy to
Once set up, the workflow will automatically run when you push to the specified deployment branch. You can also manually trigger the workflow from the "Actions" tab in your GitHub repository.
The workflow uses a smart Dockerfile detection mechanism:
- It first checks for the Dockerfile at the path specified by
DOCKERFILE_PATH(if set). - If not found or not set, it looks for a file named
dockerfile(lowercase) in the repository root. - If still not found, it looks for a file named
Dockerfile(capitalized) in the repository root. - If no Dockerfile is found, the workflow will fail with an error message.
To use a Dockerfile in a non-standard location, set the DOCKERFILE_PATH variable to the relative path from the repository root.
The workflow checks if a deployment with the specified SERVICE_NAME already exists in the specified K8S_NAMESPACE:
- If the deployment doesn't exist, it creates a new deployment and a ClusterIP service.
- If the deployment exists, it updates the existing deployment with the new image.
You can customize the application port and Kubernetes namespace by setting the APP_PORT and K8S_NAMESPACE variables, respectively. These are used when creating a new deployment and service.
You can add more steps to the workflow as needed, such as running tests, performing database migrations, or notifying external services.
If you encounter issues with the workflow:
- Check the workflow run logs in the GitHub Actions tab of your repository.
- Ensure all required secrets and variables are correctly set.
- Verify that your AWS credentials have the necessary permissions.
- Check that your EKS cluster and ECR repository are correctly configured and accessible.
- Confirm that a Dockerfile exists in your repository. If using a custom path, ensure
DOCKERFILE_PATHis set correctly. - Verify that the specified Kubernetes namespace exists in your cluster.
- Ensure that the application port specified matches the port your application is listening on.
Contributions to improve the workflow are welcome. Please feel free to submit issues or pull requests.
This workflow is available under the MIT License.