Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 0 additions & 35 deletions .github/workflows/SonarQube.yaml

This file was deleted.

37 changes: 37 additions & 0 deletions .github/workflows/build-image.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Build Image User Service

on:
workflow_call:
secrets:
DOCKER_HUB_ACCESS_TOKEN:
required: true

jobs:
build-image:
name: Build and Push Docker Image
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup JDK 17
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: 17

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: datuits
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Build the application
run: |
mvn clean
mvn -B package --file pom.xml

- name: Build and Push the docker image
run: |
docker build -t datuits/devops-user-service:latest .
docker push datuits/devops-user-service:latest
58 changes: 58 additions & 0 deletions .github/workflows/deploymentCD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Continuous Deployment for User Service

on:
workflow_run:
workflows: ["Continuous Integration for Comment Service"]
types:
- completed

env:
PROJECT_ID: gke-project-423206
CLUSTER_NAME: autopilot-cluster-1
ZONE: us-central1

jobs:
deploy:
name: Deploy to GKE Autopilot
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Setup JDK 17
uses: actions/setup-java@v3
with:
distribution: 'corretto'
java-version: 17

- name: Build the application
run: |
mvn clean
mvn -B package --file pom.xml

- name: Authenticate
uses: google-github-actions/auth@v2
with:
credentials_json: ${{ secrets.GCP_SA_KEY }}

- name: Configure gcloud
uses: google-github-actions/setup-gcloud@v2
with:
project_id: ${{ env.PROJECT_ID }}
install_components: 'gke-gcloud-auth-plugin'

- name: Set cluster context
run: |
gcloud container clusters get-credentials ${{ env.CLUSTER_NAME }} --zone ${{ env.ZONE }} --project ${{ env.PROJECT_ID }}

- name: Apply Kubernetes manifests
run: |
kubectl apply -f resources.yaml

notifications:
needs: deploy
uses: ./.github/workflows/notifyCD.yaml
secrets:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL}}

###Demo###
29 changes: 13 additions & 16 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
name: Test Stage for Comment Service
name: Dev Stage for User Service

on:
push:
branches:
- test
- dev

jobs:
testing:
name: Testing Comment Service
name: Testing User Service
runs-on: ubuntu-latest
steps:

Expand All @@ -26,18 +26,15 @@ jobs:
- name: Unit Tests
run: mvn -B test --file pom.xml

sonar-cloud-scan:
build-image:
needs: testing
uses: ./.github/workflows/SonarQube.yaml
uses: ./.github/workflows/build-image.yaml
secrets:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

scan-image:
needs: sonar-cloud-scan
uses: ./.github/workflows/scan-image.yaml

notify:
needs: scan-image
uses: ./.github/workflows/notifyCI.yaml
secrets:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
DOCKER_HUB_ACCESS_TOKEN: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

# notify:
# needs: build-image
# uses: ./.github/workflows/notifyCI.yaml
# secrets:
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
###Testing Build###
36 changes: 36 additions & 0 deletions .github/workflows/notifyCD.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Send Slack Notification for User Service

on:
workflow_call:
secrets:
SLACK_WEBHOOK_URL:
required: true

jobs:
success_notifier:
if: success()
runs-on: ubuntu-latest
steps:
- name: Send success notification on Slack
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"text": "The Continuous Deployment for User Service workflow has completed successfully."
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}

failure_notifier:
if: failure()
runs-on: ubuntu-latest
steps:
- name: Send failure notification on Slack
uses: slackapi/slack-github-action@v1.24.0
with:
payload: |
{
"text": "The Continuous Deployment for User Service workflow has failed."
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
2 changes: 1 addition & 1 deletion .github/workflows/notifyCI.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Send Slack Notification for User Service (Test Stage)
name: Send Slack Notification for User Service

on:
workflow_call:
Expand Down
47 changes: 0 additions & 47 deletions .github/workflows/scan-image.yaml

This file was deleted.

6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@
<version>2.10.1</version>
</dependency>


<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.0.1</version>
</dependency>



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,27 @@
import java.util.Map;
import java.util.Base64;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class UserController {

private static final Logger logger = LoggerFactory.getLogger(UserController.class);

@GetMapping("/")
public String getServiceName(){
//ELK
MDC.put("type", "userservice");
logger.info("User Service Start");
return "User Service";
}
private final AuthUserRepository userRepository;


private final AuthUserRepository userRepository;
private final PasswordEncoder passwordEncoder;
@Autowired
private JavaMailSender javaMailSender;
Expand Down Expand Up @@ -87,6 +97,11 @@ public ResponseEntity registerUser(@RequestBody AuthUser user) {
user.setTimestamp(new Date());
user.setAvatar(getDefaultAvatar());
AuthUser save = userRepository.save(user);
AuthUser userFromDb = userRepository.findByUsername(user.getUsername())
.orElseThrow(() -> new Exception("User not found"));
MDC.put("type", "userservice");
MDC.put("action", "register");
logger.info("UserID: " + userFromDb.getId());
return ResponseEntity.ok(save);
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
Expand All @@ -106,6 +121,9 @@ public ResponseEntity loginUser(@RequestBody AuthUser user) {
AuthUser userFromDb = userRepository.findByUsername(user.getUsername())
.orElseThrow(() -> new Exception("User not found"));
if (passwordEncoder.matches(user.getPassword(), userFromDb.getPassword())) {
MDC.put("type", "userservice");
MDC.put("action", "login");
logger.info("UserID: " + userFromDb.getId());
return ResponseEntity.ok(userFromDb);
} else {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Invalid credentials");
Expand Down Expand Up @@ -163,6 +181,9 @@ public ResponseEntity updateProfile(@PathVariable("id") String id, @RequestBody
userFromDb.setFirstName(user.getFirstName());
userFromDb.setLastName(user.getLastName());
AuthUser save = userRepository.save(userFromDb);
MDC.put("type", "userservice");
MDC.put("action", "update-profile");
logger.info("UserID: " + userFromDb.getId());

return ResponseEntity.ok(HttpStatus.OK);
} catch (Exception e) {
Expand All @@ -184,7 +205,9 @@ public ResponseEntity changePassword(@PathVariable("id") String id,

userFromDb.setPassword(passwordEncoder.encode(changePasswordRequest.getNewPassword()));
AuthUser save = userRepository.save(userFromDb);

MDC.put("type", "userservice");
MDC.put("action", "change-password");
logger.info("UserID: " + userFromDb.getId());
return ResponseEntity.ok("Password changed successfully");
} catch (Exception e) {
return ResponseEntity.internalServerError().body(e.getMessage());
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ spring.mail.username=21520714@gm.uit.edu.vn
spring.mail.password=vkux umrv rtkd svsy
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

# logstash
logging.config=classpath:logback-spring.xml
11 changes: 11 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<configuration>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>192.168.120.213:5050</destination>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
</encoder>
</appender>

<root level="INFO">
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>
1 change: 1 addition & 0 deletions src/test/java/TestUserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ public void testGetServiceName() throws Exception {
.andExpect(status().isOk());
}
}
/////