In this article we are going to cover Deploy Java Application to AWS Elastic Beanstalk using the GitLab CI.
Here we need to do two things. First of all we need to create a new application version. And second thing we need to update the environment.
So here you can see our application is api-deploy-example and we have created an environment called Apideployexample-env

Table of Contents
#1:Install Gitlab-Runner
Visit the official GitLab Runner page to install latest GitLab Runner
https://docs.gitlab.com/runner/install/linux-repository.html
Step 1: Add the official GitLab repository:
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
Step 2: To Install the latest version of GitLab Runner,
sudo apt-get install gitlab-runner
OR
To install specific version of GitLab Runner –
apt-cache madison gitlab-runner sudo apt-get install gitlab-runner=10.0.0 sudo gitlab-runner -version sudo gitlab-runner status
#2:Add gitlab-runner and user in root using below commands
sudo visudo
gitlab-runner ALL=(ALL:ALL) ALL ubuntu ALL=(ALL:ALL) ALL
#3:Register Gitlab Runner
Step 1: firstly run the the command : sudo gitlab-runner register
Step 2: Then enter the URL https://gitlab.com/ (copy from gitlab account ).
Step 3: Then Enter the registration token so firstly
- Go to the GitLab Account
- Click on Project
- Then click on setting and in below click on CI/CD
- Click on Runner Expand scroll down and copy registration token
- And paste on command
Step 4: Then you can give any meaning full description
Step 5: Then Enter tags for the runner (any tag)
Step 6: Then choose any executor – shell , ssh , docker , etc.
#4:Install OpenJDK on Ubuntu 20.04 LTS
Update System packages:
sudo apt update
Install OpenJDK:
sudo apt install default-jdk -y
To Check Java Version
java --version
#5. Install AWS CLI
sudo apt install awscli
#5:Create new Bucket in AWS
Firstly search S3 and go ti buckets then click on create bucket

Then enter unique name because bucket name must be unique and not contain spaces or uppercase letters

Then remaining keep as it is and click on create bucket

#6:Create new User in AWS
Firstly go to IAM<<Users<<Add users

Now enter user name and select Aws credential type so here i will choose Access key

Then here click on Attach existing policies directly then select Amazons3FullAccess and AWSelasticBeanstalk

Next step is add tags so i don’t want to add any tag so skip and then review once

Now here you can see Access key ID or Secret access key now you can download.
#7:Creating variables inside gitlab
Go to settings<<CI/CD<<variables<<Expand it
#Add bucket name in $S3_BUCKET variable #Add secret key in $AWS_SECRET_ACCESS_KEY_ variable #Add access key in $AWS_ACCESS_KEY_ID variable

When creating variables you have to remove protect variable flag

#8:Deploy Java Application to AWS Elastic Beanstalk using the GitLab
Below is .gitlab-ci.yml file to Deploy Java Application to AWS Elastic Beanstalk using the GitLab
variables:
ARTIFACT_NAME: AWS-0.0.1-SNAPSHOT-v$CI_PIPELINE_IID.jar
APP_NAME: api-deploy-example
stages:
- build
- deploy
build:
stage: build
image: openjdk:12-alpine
script:
- ./gradlew build
- mv ./build/libs/AWS-0.0.1-SNAPSHOT.jar ./build/libs/$ARTIFACT_NAME
artifacts:
paths:
- ./build/libs/
deploy:
stage: deploy
image:
name: banst/awscli
entrypoint: [""]
script:
- aws configure set region ap-south-1
- aws s3 cp /home/gitlab-runner/builds/xvXKhe5J/0/devopshint/aws-elastic-beanstalk/build/libs/$ARTIFACT_NAME s3://$S3_BUCKET/$ARTIFACT_NAME
- aws elasticbeanstalk create-application-version --application-name $APP_NAME --version-label $CI_PIPELINE_IID --source-bundle S3Bucket=$S3_BUCKET,S3Key=$ARTIFACT_NAME
- aws elasticbeanstalk update-environment --application-name $APP_NAME --environment-name "Apideployexample-env" --version-label=$CI_PIPELINE_IID
#9:Permission denied ./gradlew
git clone
cd project name
ls -l gradlew
chmod +x gradlew
git update-index --chmod=+x gradlew
git add .
git commit -m "Update execute permission for gradlew command".
git push
Here you can see here is our final output

So here we have running application version 3 and now we have successfully updated

Conclusion:
In this article we have covered Deploy Java Application to AWS Elastic Beanstalk using the GitLab CI.
Related Articles:
SonarQube Integration for Node JS Project using GitLab
Reference:
Deploy a Docker application on AWS Elastic Beanstalk with GitLab
Thanks for sharing this and well done and its interesting to note that this can be done this way i seems to prefer cicd deployment using jenkins Jenkinsfile , and thank you for this. Although i have zero knowledge about deploying apps or running a CICd deployment workflow using GitLab CI but this is quite mind blowing will take a closer look at it going forward. Thanks for sharing