diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml new file mode 100644 index 0000000..993b5d8 --- /dev/null +++ b/.github/workflows/docker.yml @@ -0,0 +1,139 @@ +name: Deploy to Amazon EC2 using Docker + +on: + push: + branches: + - docker + +# 본인이 설정한 값을 여기서 채워넣습니다. +# 리전, 버킷 이름, CodeDeploy 앱 이름, CodeDeploy 배포 그룹 이름 +env: + DOCKER_USER: ${{ secrets.DOCKER_USER }} + DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }} + REPO: chadda-repo + +permissions: + contents: read + +jobs: + deploy: + name: Deploy + runs-on: ubuntu-latest + environment: production + + steps: + # (1) 기본 체크아웃 + - name: Checkout + uses: actions/checkout@v3 + + # (2) JDK 11 세팅 + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: '11' + + # (3) properties 생성 + - name: make application-secret.properties + run: | + echo "ls -a . & pwd" + pwd + ls -a . + echo "mkdir & touch" + mkdir -p ./src/main/resources + cd ./src/main/resources + touch ./application-secret.properties + pwd + ls -a . + echo "copy properties" + echo $DEV_PROP >> ./application-secret.properties + cat application-secret.properties + shell: bash + env: + DEV_PROP: ${{ secrets.JWT_SECRET }} + + - name: make application-db-username.properties + run: | + echo "ls -a . & pwd" + pwd + ls -a . + echo "mkdir & touch" + mkdir -p ./src/main/resources + cd ./src/main/resources + touch ./application-db-username.properties + pwd + ls -a . + echo "copy properties" + echo $DEV_PROP >> ./application-db-username.properties + cat application-db-username.properties + env: + DEV_PROP: ${{ secrets.DB_USERNAME }} + + - name: make application-db-password.properties + run: | + echo "ls -a . & pwd" + pwd + ls -a . + echo "mkdir & touch" + mkdir -p ./src/main/resources + cd ./src/main/resources + touch ./application-db-password.properties + pwd + ls -a . + echo "copy properties" + echo $DEV_PROP >> ./application-db-password.properties + cat application-db-password.properties + shell: bash + env: + DEV_PROP: ${{ secrets.DB_PASSWORD }} + + - name: make application-db-url.properties + run: | + echo "ls -a . & pwd" + pwd + ls -a . + echo "mkdir & touch" + mkdir -p ./src/main/resources + cd ./src/main/resources + touch ./application-db-url.properties + pwd + ls -a . + echo "copy properties" + echo $DEV_PROP >> ./application-db-url.properties + cat application-db-url.properties + shell: bash + env: + DEV_PROP: ${{ secrets.DB_URL }} + + # (4) Gradle build (Test 제외) + - name: Build with Gradle + uses: gradle/gradle-build-action@v2 + with: + gradle-version: 7.5 + arguments: clean build -x test + + #(5) Docker Login + - name: Docker Login + run: + docker login -u $DOCKER_USER -p $DOCKER_PASSWORD + + #(6) Docker Push + - name: Docker Push + run: | + docker build -t $REPO . + docker tag $REPO:latest $DOCKER_USER/$REPO:latest + docker push $DOCKER_USER/$REPO + + #{7} Docker Pull & Run From Hub + - name: Docker Pull & Run From Hub + uses: appleboy/ssh-action@master + with: + host: 3.36.109.80 + username: ubuntu + key: ${{secrets.EC2_PRIVATE_KEY}} + envs: GITHUB_SHA + script: | + docker login -u ${{secrets.DOCKER_USER}} -p ${{secrets.DOCKER_PASSWORD}} + docker pull ${{secrets.DOCKER_USER}}/chadda-repo + docker stop chadda_server + docker rm chadda_server + docker run --detach --name chadda_server -p 80:8080 ${{secrets.DOCKER_USER}}/chadda-repo \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e3282d3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,6 @@ +FROM adoptopenjdk/openjdk11 +MAINTAINER search363 +CMD ["./gradlew","clearn","package"] +ARG JAR_FILE=./build/libs/instagram-searching-0.0.1-SNAPSHOT.jar +COPY ${JAR_FILE} app.jar +ENTRYPOINT ["java","-jar","/app.jar"] \ No newline at end of file diff --git a/src/main/java/com/search/instagramsearching/config/RedisConfig.java b/src/main/java/com/search/instagramsearching/config/RedisConfig.java index 69b4169..775e3fe 100644 --- a/src/main/java/com/search/instagramsearching/config/RedisConfig.java +++ b/src/main/java/com/search/instagramsearching/config/RedisConfig.java @@ -34,23 +34,27 @@ public class RedisConfig { @Value("${spring.redis.host}") public String host; + @Value("${spring.redis.password}") + public String password; + @Autowired public ObjectMapper objectMapper; - @Bean - public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { - RedisTemplate redisTemplate = new RedisTemplate<>(); - redisTemplate.setKeySerializer(new StringRedisSerializer()); - redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); - redisTemplate.setConnectionFactory(connectionFactory); - return redisTemplate; - } +// @Bean +// public RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { +// RedisTemplate redisTemplate = new RedisTemplate<>(); +// redisTemplate.setKeySerializer(new StringRedisSerializer()); +// redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); +// redisTemplate.setConnectionFactory(connectionFactory); +// return redisTemplate; +// } @Bean public RedisConnectionFactory redisConnectionFactory() { RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration(); redisStandaloneConfiguration.setHostName(host); redisStandaloneConfiguration.setPort(port); + redisStandaloneConfiguration.setPassword(password); LettuceConnectionFactory connectionFactory = new LettuceConnectionFactory(redisStandaloneConfiguration); return connectionFactory; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 58b3623..957ebe0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -12,6 +12,7 @@ spring.jpa.show-sql=true spring.batch.job.enabled=false #redis -spring.redis.host=localhost +spring.redis.host=172.17.0.3 spring.redis.port=6379 +spring.redis.password=redispw diff --git a/src/test/java/com/search/instagramsearching/PostsTest.java b/src/test/java/com/search/instagramsearching/PostsTest.java index 8d046e3..91f9c2c 100644 --- a/src/test/java/com/search/instagramsearching/PostsTest.java +++ b/src/test/java/com/search/instagramsearching/PostsTest.java @@ -1,51 +1,51 @@ -package com.search.instagramsearching; - -import com.search.instagramsearching.repository.PostsRepository; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; -import org.springframework.util.StopWatch; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) -public class PostsTest { - @Autowired - private PostsRepository postsRepository; - - private String keyword; - - @BeforeEach - void beforeAll(){keyword="dd";} - - @Test - void testQuery(){ - // given - StopWatch stopWatch = new StopWatch(); - Pageable pageable = PageRequest.of(0, 10); - - // when - stopWatch.start("검색 쿼리"); - postsRepository.search(keyword,pageable); - stopWatch.stop(); - - // then - System.out.println(stopWatch.prettyPrint()); - } - - @Test - void testSearchViewQuery(){ - // given - StopWatch stopWatch = new StopWatch(); - Pageable pageable = PageRequest.of(0, 10); - - // when - stopWatch.start("검색 쿼리"); - postsRepository.searchView(keyword,pageable); - stopWatch.stop(); - - // then - System.out.println(stopWatch.prettyPrint()); - } -} +//package com.search.instagramsearching; +// +//import com.search.instagramsearching.repository.PostsRepository; +//import org.junit.jupiter.api.BeforeEach; +//import org.junit.jupiter.api.Test; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.data.domain.PageRequest; +//import org.springframework.data.domain.Pageable; +//import org.springframework.util.StopWatch; +// +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE) +//public class PostsTest { +// @Autowired +// private PostsRepository postsRepository; +// +// private String keyword; +// +// @BeforeEach +// void beforeAll(){keyword="dd";} +// +// @Test +// void testQuery(){ +// // given +// StopWatch stopWatch = new StopWatch(); +// Pageable pageable = PageRequest.of(0, 10); +// +// // when +// stopWatch.start("검색 쿼리"); +// postsRepository.search(keyword,pageable); +// stopWatch.stop(); +// +// // then +// System.out.println(stopWatch.prettyPrint()); +// } +// +// @Test +// void testSearchViewQuery(){ +// // given +// StopWatch stopWatch = new StopWatch(); +// Pageable pageable = PageRequest.of(0, 10); +// +// // when +// stopWatch.start("검색 쿼리"); +// postsRepository.searchView(keyword,pageable); +// stopWatch.stop(); +// +// // then +// System.out.println(stopWatch.prettyPrint()); +// } +//}