From 2d8d45962eb91b63cded950856d41fe151c0fcea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Caiado?= Date: Thu, 4 Apr 2024 14:13:28 +0100 Subject: [PATCH] ci(task-nr): environment as env variable (#46) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: AndreĢ Caiado da Silva --- .github/workflows/CI-CD.yml | 2 +- .../example/spring/test/controller/TutorialController.java | 6 +++++- src/main/resources/application.properties | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/CI-CD.yml b/.github/workflows/CI-CD.yml index eda58d4..fbba919 100644 --- a/.github/workflows/CI-CD.yml +++ b/.github/workflows/CI-CD.yml @@ -153,4 +153,4 @@ jobs: 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' - name: Run new container run: | - ssh -o StrictHostKeyChecking=no -i key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_IPADDRESS }} 'sudo docker run -d --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.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 }}' diff --git a/src/main/java/com/example/spring/test/controller/TutorialController.java b/src/main/java/com/example/spring/test/controller/TutorialController.java index 5323dbb..e9a04de 100644 --- a/src/main/java/com/example/spring/test/controller/TutorialController.java +++ b/src/main/java/com/example/spring/test/controller/TutorialController.java @@ -5,6 +5,7 @@ import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.CrossOrigin; @@ -29,9 +30,12 @@ public class TutorialController { @Autowired TutorialRepository tutorialRepository; + @Value("${app.environment}") + private String environment; + @GetMapping("/greeting") public String greeting() { - return "Hello, World"; + return "Hello World from " + environment + " environment"; } @GetMapping("/tutorials") diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index ea76c01..686ab7d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -9,4 +9,6 @@ spring.datasource.password= spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect -spring.jpa.hibernate.ddl-auto= update \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update + +app.environment=${APP_ENV:LOCAL}