From bffa4d8c94172fe5f6ad24cc5567f50477b66bce Mon Sep 17 00:00:00 2001 From: hendisantika Date: Tue, 12 Mar 2024 12:44:20 +0700 Subject: [PATCH] =?UTF-8?q?#142=20-=20Setup=20CI/CD=20:one:=20=F0=9F=AB=B8?= =?UTF-8?q?=F0=9F=8C=80=E2=9C=8F=EF=B8=8F=F0=9F=93=97=20:octocat:?= =?UTF-8?q?=F0=9F=90=A7=F0=9F=90=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/app-workflow.yml | 109 +++++++++++++++++++++++++++++ docker-compose.yml | 3 +- nginx.conf | 11 +++ 3 files changed, 122 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/app-workflow.yml create mode 100644 nginx.conf diff --git a/.github/workflows/app-workflow.yml b/.github/workflows/app-workflow.yml new file mode 100644 index 0000000..85ec9fa --- /dev/null +++ b/.github/workflows/app-workflow.yml @@ -0,0 +1,109 @@ +name: BMI application workflow +on: + pull_request: + branches: [ master ] + paths: + - 'src/**' + - '.github/workflows/app-workflow.yml' + push: + branches: [ master ] + paths: + - 'src/**' + - '.github/workflows/app-workflow.yml' + +jobs: + project-build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'corretto' + + - name: Set up the Maven dependencies caching + uses: actions/cache@v4 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Install Maven dependencies + run: mvn clean install + + - name: Run tests + run: mvn clean --batch-mode --update-snapshots verify + + docker-build: + if: ${{ github.ref == 'refs/heads/master' }} + runs-on: ubuntu-latest + needs: + - project-build + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK 21 + uses: actions/setup-java@v4 + with: + java-version: '21' + distribution: 'corretto' + + - name: Cache Maven packages + uses: actions/cache@v4 + with: + path: ~/.m2 + key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }} + restore-keys: ${{ runner.os }}-m2 + + - name: Set up QEMU + uses: docker/setup-qemu-action@v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Log into the Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Scrape build info + run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + + - name: Build and push + uses: docker/build-push-action@v5 + with: + context: . + file: Dockerfile + push: true + labels: ${{ steps.meta.outputs.labels }} + # tags: hendisantika/bmi:${{ steps.meta.outputs.tags }} + # tags: ${{ steps.meta.outputs.tags }} + # tags: hendisantika/bmi:latest + tags: hendisantika/bmi:${{ github.run_number }} + build-args: | + GIT_VERSION_TAG=${{ env.RELEASE_VERSION }} + GIT_COMMIT_MESSAGE=${{ github.event.head_commit.message }} + GIT_VERSION_HASH=${{ github.sha }} + + deploy: + if: ${{ github.ref == 'refs/heads/master' }} + runs-on: ubuntu-latest + needs: + - docker-build + steps: + - uses: actions/checkout@v4 + - name: Add Server key + run: | + touch key.txt && echo "${{ secrets.SERVER_KEY }}" > key.txt + chmod 600 key.txt + - name: Deploy the application + env: + SERVER_HOST: ${{ secrets.SERVER_HOST }} + SERVER_PORT: ${{ secrets.SERVER_PORT }} + SERVER_USER: ${{ secrets.SERVER_USERNAME }} + run: | + set -e + ./deploy.sh diff --git a/docker-compose.yml b/docker-compose.yml index 37c652e..f0b3e0a 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,6 +8,7 @@ services: - mysql networks: - spring-boot-app + mysql: image: mysql:latest environment: @@ -17,7 +18,7 @@ services: - "3307:3306" networks: - spring-boot-app - - + reverse-proxy: image: nginx:latest ports: diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e2b8280 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,11 @@ +events { +} + +http { + server { + listen 80; + location / { + proxy_pass http://app:9000; + } + } +}