diff --git a/.github/workflows/deploy-to-ec2.yml b/.github/workflows/deploy-to-ec2.yml new file mode 100644 index 00000000..a0ec511f --- /dev/null +++ b/.github/workflows/deploy-to-ec2.yml @@ -0,0 +1,51 @@ +name: Deploy to EC2 + +on: + push: + branches: + - main + +jobs: + build-and-push: + runs-on: ubuntu-latest + + steps: + - name: Checkout Repository + uses: actions/checkout@v3 + + - name: Set up AWS CLI + uses: aws-actions/configure-aws-credentials@v2 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: ${{ secrets.AWS_REGION }} + + - name: Login to Amazon ECR Public + id: login-ecr + run: aws ecr-public get-login-password --region us-east-1 | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_URI }} + + - name: Build Docker image + run: docker build -t skill-tree-backend -f skill-tree/Dockerfile . + - name: Tag Docker image + run: docker tag skill-tree-backend:latest ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest + + - name: Push Docker image to ECR Public + run: docker push ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest + + deploy: + needs: build-and-push + runs-on: ubuntu-latest + steps: + - name: Deploy to EC2 + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.AWS_EC2_HOST }} + username: ${{ secrets.AWS_EC2_USERNAME }} + key: ${{ secrets.AWS_EC2_SSH_PRIVATE_KEY }} + - name: Pull Image from docker hub + run: docker pull ${{ secrets.AWS_ECR_URI }}/skill-tree-backend:latest + - name: Delete old container + run: docker rm -f skill-tree-backend + - name: Run docker container + run: docker run -d -p 8080:8080 --name skill-tree-backend -e RDS_PUBLIC_KEY=${{secrets.RDS_PUBLIC_KEY}} -e API_V1_PREFIX=/api/v1 ${{ secrets.AWS_ECR_URI }}/skill-tree-backend + diff --git a/.gitignore b/.gitignore index 56f3c0fb..661c12b8 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,4 @@ replay_pid* *.env -target/ \ No newline at end of file +target/ diff --git a/skill-tree/Dockerfile b/skill-tree/Dockerfile new file mode 100644 index 00000000..251f9ab4 --- /dev/null +++ b/skill-tree/Dockerfile @@ -0,0 +1,11 @@ +FROM openjdk:17-jdk as build +WORKDIR /app +COPY skill-tree . +RUN ./mvnw --version +RUN ./mvnw clean install -DskipTests + +FROM openjdk:17-jdk +WORKDIR /app +COPY --from=build /app/target/*.jar app.jar +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "app.jar"] diff --git a/skill-tree/docker-compose.yml b/skill-tree/docker-compose.yml index e7fc4601..63bcab43 100644 --- a/skill-tree/docker-compose.yml +++ b/skill-tree/docker-compose.yml @@ -2,8 +2,6 @@ version: "3.7" services: db: image: mysql - # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password - # (this is just an example, not intended to be a production configuration) command: --default-authentication-plugin=mysql_native_password ports: - 3306:3306 @@ -13,9 +11,33 @@ services: MYSQL_PASSWORD: testpassword MYSQL_ROOT_PASSWORD: password volumes: - - ./skilltree-database-script.sql:/docker-entrypoint-initdb.d/skilltree-database-script.sql + - mysql-data:/var/lib/mysql adminer: image: adminer ports: - - 9000:8080 \ No newline at end of file + - 9000:8080 + + spring-boot-app: + build: + context: . + dockerfile: Dockerfile + depends_on: + - db + ports: + - 8080:8080 + environment: + SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/skilltree + SPRING_DATASOURCE_USERNAME: testuser + SPRING_DATASOURCE_PASSWORD: testpassword + SPRING_JPA_HIBERNATE_DDL_AUTO: update + MYSQL_HOST: db + DB_NAME: skilltree + MYSQL_DB_USERNAME: testuser + MYSQL_DB_PASSWORD: testpassword + DB_DDL_POLICY: update + API_V1_PREFIX: /api/v1 + SPRING_DATASOURCE_VERSION: 8.1.0 + +volumes: + mysql-data: