Skip to content

Commit

Permalink
update readme and AWS secret names
Browse files Browse the repository at this point in the history
  • Loading branch information
acaiado committed Apr 4, 2024
1 parent 8e9a6a2 commit a3cedb0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/CI-CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ jobs:
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Set permissions for private key
run: |
echo "${{ secrets.AWS_EC2_PRIVATE_KEY }}" > key.pem
echo "${{ secrets.`AWS_EC2_PRIVATE_KEY }}" > key.pem
chmod 600 key.pem
- name: Pull Docker image
run: |
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_IPADDRESS }} 'sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }}'
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.AWS_EC2_USERNAME }}@${{ secrets.AWS_EC2_IPADDRESS }} 'sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }}'
- name: Stop running container
run: |
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_IPADDRESS }} 'sudo docker stop spring-boot-tests-cicd-${{ needs.prepare.outputs.environment }} || true'
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_IPADDRESS }} 'sudo docker rm spring-boot-tests-cicd-${{ needs.prepare.outputs.environment }} || true'
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.AWS_EC2_USERNAME }}@${{ secrets.AWS_EC2_IPADDRESS }} 'sudo docker stop spring-boot-tests-cicd-${{ needs.prepare.outputs.environment }} || true'
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.AWS_EC2_USERNAME }}@${{ secrets.AWS_EC2_IPADDRESS }} 'sudo docker rm spring-boot-tests-cicd-${{ needs.prepare.outputs.environment }} || true'
- name: Run new container
run: |
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_IPADDRESS }} 'sudo docker run -d --env APP_ENV=${{ needs.prepare.outputs.environment }} --name spring-boot-tests-cicd-${{ needs.prepare.outputs.environment }} -p ${{ needs.prepare.outputs.port }}:8080 ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }}'
ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.AWS_EC2_USERNAME }}@${{ secrets.AWS_EC2_IPADDRESS }} 'sudo docker run -d --env APP_ENV=${{ needs.prepare.outputs.environment }} --name spring-boot-tests-cicd-${{ needs.prepare.outputs.environment }} -p ${{ needs.prepare.outputs.port }}:8080 ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:${{ needs.prepare.outputs.tag_version }}'
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,33 @@ The code coverage is executed with JaCoCo maven plugin and the report is generat

#### Deploy

TODO
The application is deployed in an AWS EC2 instance using SSH.

In order to authenticate the SSH connection, a private key is needed and so, it was created in AWS and stored as a secret in the repository.
The secrets used in the workflow are:
- `DOCKER_USERNAME` and `DOCKER_PASSWORD` to authenticate in Docker Hub.
- `AWS_EC2_USERNAME`, `AWS_EC2_IPADDRESS` and `AWS_EC2_PRIVATE_KEY` to connect to the EC2 instance.

__Note:__ Each time the instance is stopped and started, the EC2 instance public IP address changes and we need to update the `AWS_EC2_IPADDRESS` secret. To avoid this, an Elastic IP could have been used but for now, it was not implemented.

The deploy job has the following steps:
- Login to Docker Hub.
- Sets permissions to the private key file that is going to be used to authenticate SSH connection.
- The following steps are then executed inside the EC2 instance (connected via SSH):
- Pull the new image.
- Stop the running container (by container name).
- Remove the container (by container name).
- Run the new container.

Containers are named with the following pattern: `spring-boot-tests-cicd-<environment>`.

![containers.png](src%2Fmain%2Fresources%2Fcontainers.png)

The environment variable value is added to the container as an environment variable in the `run` command. It can be `STAGING` or `PRODUCTION`.

The application will load the environment variable and use the value in the `/api/greeting` endpoint to present a message with the environment name.

![hello-stag.png](src%2Fmain%2Fresources%2Fhello-stag.png)
![hello-prod.png](src%2Fmain%2Fresources%2Fhello-prod.png)

The application port is set to 8080 and the container port is mapped to the host port 8080 for the STAGING environment and 8081 for the PRODUCTION environment.
Binary file added src/main/resources/containers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/hello-prod.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/main/resources/hello-stag.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a3cedb0

Please sign in to comment.