Automated Java Application Deployment to AWS Elastic Beanstalk using GitHub Actions

Automated Java Application Deployment to AWS Elastic Beanstalk using GitHub Actions
Photo by Roman Synkevych / Unsplash

When developing modern applications, having a robust CI/CD pipeline is crucial. In this guide, we'll walk through setting up automated deployments to AWS Elastic Beanstalk using GitHub Actions for both staging and production environments.

Prerequisites

  • Java application with Maven
  • GitHub repository
  • AWS Elastic Beanstalk environment
  • AWS IAM credentials

Setting Up GitHub Actions

We'll create two workflow files: one for staging and one for production. These files should be placed in the .github/workflows/ directory of your repository.

Staging Deployment Configuration

name: Deploy staging
on:
  push:
    branches:
    - staging

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout source code
      uses: actions/checkout@v4

    - name: Setup Java
      uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-version: '21'

    - name: Setup Maven
      uses: stCarolas/setup-maven@v5
      with:
        maven-version: 3.8.2

    - name: Build project
      run: mvn clean package -DskipTests

    - name: Deploy to EB
      uses: einaregilsson/beanstalk-deploy@v22
      with:
        aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        application_name: 'your-app-name-staging'
        environment_name: 'your-environment-name-staging'
        version_label: ${{ github.sha}}
        region: eu-central-1
        existing_bucket_name: 'elasticbeanstalk-eu-central-1-your-bucket'
        deployment_package: target/your-application.jar

Production Deployment Configuration

name: Deploy production
on:
  push:
    branches:
    - production

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout source code
      uses: actions/checkout@v4

    - name: Setup Java
      uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-version: '21'

    - name: Setup Maven
      uses: stCarolas/setup-maven@v5
      with:
        maven-version: 3.8.2

    - name: Build project
      run: mvn clean package -DskipTests

    - name: Deploy to EB
      uses: einaregilsson/beanstalk-deploy@v22
      with:
        aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        application_name: 'your-app-name-prod'
        environment_name: 'your-environment-name-prod'
        version_label: ${{ github.sha}}
        region: eu-central-1
        existing_bucket_name: 'elasticbeanstalk-eu-central-1-your-bucket'
        deployment_package: target/your-application.jar

Setting Up GitHub Secrets

To securely manage AWS credentials, you must add them as GitHub secrets. Here's how:

  1. Navigate to your GitHub repository
  2. Go to Settings > Secrets and Variables> Actions
  3. Click "New repository secret"
  4. Add the following secrets:
    • AWS_ACCESS_KEY_ID: Your AWS access key
    • AWS_SECRET_ACCESS_KEY: Your AWS secret key

How It Works

Let's break down the key components of our workflow:

1. Trigger Configuration

on:
  push:
    branches:
    - main  # or production for prod deployment

The workflow triggers when code is pushed to the specified branch.

2. Environment Setup

- name: Setup Java
  uses: actions/setup-java@v4
  with:
    distribution: 'temurin'
    java-version: '21'

- name: Setup Maven
  uses: stCarolas/setup-maven@v5
  with:
    maven-version: 3.8.2

Sets up the Java and Maven environment for building the application.

3. Build Process

- name: Build project
  run: mvn clean package -DskipTests

Builds the Java application and creates the JAR file.

4. Deployment

- name: Deploy to EB
  uses: einaregilsson/beanstalk-deploy@v22
  with:
    aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    application_name: 'your-app-name'
    environment_name: 'your-environment-name'
    version_label: ${{ github.sha}}
    region: eu-central-1
    existing_bucket_name: 'elasticbeanstalk-eu-central-1-your-bucket'
    deployment_package: target/your-application.jar

Deploys the built application to AWS Elastic Beanstalk.

Important Notes

  1. Security: Never commit AWS credentials directly in the workflow files.
  2. Version Label: We use github.sha it for version tracking, ensuring unique deployments.
  3. Skip Tests: The current configuration skips tests during the build (-DskipTests). Consider enabling tests for production deployments.
  4. Region: Use the correct AWS region where your Elastic Beanstalk environment is hosted.
  5. Bucket Name: The existing_bucket_name should match your Elastic Beanstalk S3 bucket.

Benefits

  • Automated deployments triggered by code pushes
  • Separate workflows for staging and production environments
  • Secure credential management using GitHub secrets
  • Consistent build and deployment process
  • Version tracking using Git SHA

Conclusion

This setup provides a robust CI/CD pipeline for Java applications, automating the deployment process to AWS Elastic Beanstalk. By maintaining separate workflows for staging and production, we ensure a proper deployment strategy while keeping our sensitive credentials secure.

See you in the next article! 👻