From 6caaaaba7f6a2cf2cc499c0a6236c948e3188ed7 Mon Sep 17 00:00:00 2001 From: Fl1s Date: Sat, 11 Oct 2025 20:30:33 +0700 Subject: [PATCH 1/4] ci: gitlab-ci.yml --- .github/actions/docker-build-push/action.yml | 50 ------- .github/workflows/build.yml | 88 ----------- .gitlab/.gitlab-ci.yml | 147 +++++++++++++++++++ {.github => .gitlab}/CODEOWNERS | 0 4 files changed, 147 insertions(+), 138 deletions(-) delete mode 100644 .github/actions/docker-build-push/action.yml delete mode 100644 .github/workflows/build.yml create mode 100644 .gitlab/.gitlab-ci.yml rename {.github => .gitlab}/CODEOWNERS (100%) diff --git a/.github/actions/docker-build-push/action.yml b/.github/actions/docker-build-push/action.yml deleted file mode 100644 index a206002..0000000 --- a/.github/actions/docker-build-push/action.yml +++ /dev/null @@ -1,50 +0,0 @@ -name: docker-build-push - -description: Builds and pushes Docker image to GHCR - -inputs: - service: - description: Service name - required: true - registry: - description: Container registry - default: ghcr.io - project: - description: Project name - default: turron - push: - description: Push images to registry (true/false) - default: 'false' - env: - description: Environment (dev/prod) - default: 'dev' - -runs: - using: "composite" - steps: - - name: Set image tags - shell: bash - run: | - echo "IMAGE_NAME=${{ inputs.registry }}/fl1s/${{ inputs.project }}/${{ inputs.service }}" >> $GITHUB_ENV - echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV - echo "DATE_TAG=$(date +%Y%m%d)" >> $GITHUB_ENV - if [ "${{ inputs.env }}" = "prod" ]; then - VERSION=$(cat .config/VERSION 2>/dev/null || echo "v1.0") - echo "VERSION_TAG=$VERSION" >> $GITHUB_ENV - else - echo "VERSION_TAG=dev-$SHORT_SHA" >> $GITHUB_ENV - fi - - - name: Docker build - shell: bash - run: | - cd ${{ inputs.service }} - docker build -t $IMAGE_NAME:latest -t $IMAGE_NAME:$VERSION_TAG -t $IMAGE_NAME:dev-$DATE_TAG . - - - name: Docker push - if: ${{ inputs.push == 'true' }} - shell: bash - run: | - docker push $IMAGE_NAME:latest - docker push $IMAGE_NAME:$VERSION_TAG - docker push $IMAGE_NAME:dev-$DATE_TAG \ No newline at end of file diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 17fdc02..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,88 +0,0 @@ -name: Build & Deploy - -permissions: - contents: read - packages: write - -on: - push: - branches: - - main - pull_request: - branches: - - main - -jobs: - build-and-test: - runs-on: ubuntu-latest - strategy: - matrix: - service: - - eureka-server - - upload-service - - extraction-service - - hashing-service - - search-service - - api-gateway -# - client - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Set up Java 21 - if: matrix.service != 'client' - uses: actions/setup-java@v4 - with: - java-version: '21' - distribution: 'temurin' - - - name: Cache Gradle dependencies - if: matrix.service != 'client' - uses: actions/cache@v4 - with: - path: ~/.gradle/caches - key: gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} - restore-keys: | - gradle- - - - name: Cache Node modules - if: matrix.service == 'client' - uses: actions/cache@v4 - with: - path: client/node_modules - key: node-${{ hashFiles('client/package-lock.json') }} - restore-keys: | - node- - - - name: Build service ${{ matrix.service }} - run: | - cd ${{ matrix.service }} - if [ "${{ matrix.service }}" == "client" ]; then - npm install - npm run build - else - ./gradlew clean build - fi - - - name: Login to GHCR - uses: docker/login-action@v3 - with: - registry: ghcr.io - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - - name: Build & Push Docker image (dev) - if: github.event_name == 'pull_request' - uses: ./.github/actions/docker-build-push - with: - service: ${{ matrix.service }} - env: dev - push: 'true' - - - name: Build & Push Docker image (prod) - if: github.event_name == 'push' && github.ref == 'refs/heads/main' - uses: ./.github/actions/docker-build-push - with: - service: ${{ matrix.service }} - env: prod - push: 'true' \ No newline at end of file diff --git a/.gitlab/.gitlab-ci.yml b/.gitlab/.gitlab-ci.yml new file mode 100644 index 0000000..ac56a88 --- /dev/null +++ b/.gitlab/.gitlab-ci.yml @@ -0,0 +1,147 @@ +stages: + - build + - docker + - deploy + +variables: + REGISTRY: ghcr.io + PROJECT: turron + DATE_TAG: "$(date +%Y%m%d)" + SHORT_SHA: "${CI_COMMIT_SHORT_SHA}" + +# cache for gradle and node +cache: + key: "${CI_JOB_NAME}" + paths: + - ~/.gradle/caches/ + - client/node_modules/ + +# universal java_build +.build_java_service: + stage: build + image: gradle:8-jdk21 + script: + - cd $SERVICE + - ./gradlew clean build + +# universal frontend_build +.build_client: + stage: build + image: node:20 + script: + - cd client + - npm install + - npm run build + +# universal build_push +.docker_build_push: + stage: docker + image: docker:27 + services: + - docker:dind + variables: + DOCKER_HOST: tcp://docker:2375/ + DOCKER_TLS_CERTDIR: "" + script: + - IMAGE_NAME="$REGISTRY/fl1s/$PROJECT/$SERVICE" + - if [ "$ENVIRONMENT" = "prod" ]; then VERSION_TAG=$(cat .config/VERSION 2>/dev/null || echo "v1.0"); else VERSION_TAG="dev-$SHORT_SHA"; fi + - docker build -t $IMAGE_NAME:latest -t $IMAGE_NAME:$VERSION_TAG -t $IMAGE_NAME:dev-$DATE_TAG $SERVICE + - echo "$CI_JOB_TOKEN" | docker login $REGISTRY -u $CI_REGISTRY_USER --password-stdin + - if [ "$PUSH" = "true" ]; then + docker push $IMAGE_NAME:latest && + docker push $IMAGE_NAME:$VERSION_TAG && + docker push $IMAGE_NAME:dev-$DATE_TAG; + fi + +# build jobs + +eureka-server: + extends: .build_java_service + variables: + SERVICE: eureka-server + +upload-service: + extends: .build_java_service + variables: + SERVICE: upload-service + +extraction-service: + extends: .build_java_service + variables: + SERVICE: extraction-service + +hashing-service: + extends: .build_java_service + variables: + SERVICE: hashing-service + +search-service: + extends: .build_java_service + variables: + SERVICE: search-service + +api-gateway: + extends: .build_java_service + variables: + SERVICE: api-gateway + +client: + extends: .build_client + +# jobs + +# dev: merge req. +docker_dev: + extends: .docker_build_push + variables: + ENVIRONMENT: dev + PUSH: "true" + rules: + - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' + parallel: + matrix: + - SERVICE: eureka-server + - SERVICE: upload-service + - SERVICE: extraction-service + - SERVICE: hashing-service + - SERVICE: search-service + - SERVICE: api-gateway +# - SERVICE: client + +# prod: merge to main +docker_prod: + extends: .docker_build_push + variables: + ENVIRONMENT: prod + PUSH: "true" + rules: + - if: '$CI_COMMIT_BRANCH == "main"' + parallel: + matrix: + - SERVICE: eureka-server + - SERVICE: upload-service + - SERVICE: extraction-service + - SERVICE: hashing-service + - SERVICE: search-service + - SERVICE: api-gateway +# - SERVICE: client + +deploy_prod: + stage: deploy + image: alpine:latest + rules: + - if: '$CI_COMMIT_BRANCH == "main"' + before_script: + - apk add --no-cache openssh-client + script: + - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - >/dev/null 2>&1 || true + - mkdir -p ~/.ssh + - chmod 700 ~/.ssh + - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config + - ssh $SSH_USER@$SSH_HOST " + cd turron && + docker-compose pull && + docker-compose up -d --remove-orphans + " + only: + - main diff --git a/.github/CODEOWNERS b/.gitlab/CODEOWNERS similarity index 100% rename from .github/CODEOWNERS rename to .gitlab/CODEOWNERS -- GitLab From 23fc66bbdaa6523e90db5d6292586c9a7fdefb6f Mon Sep 17 00:00:00 2001 From: Fl1s Date: Sat, 11 Oct 2025 20:37:01 +0700 Subject: [PATCH 2/4] ci: 'only: main' line removed --- .gitlab/.gitlab-ci.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitlab/.gitlab-ci.yml b/.gitlab/.gitlab-ci.yml index ac56a88..3aa68bb 100644 --- a/.gitlab/.gitlab-ci.yml +++ b/.gitlab/.gitlab-ci.yml @@ -142,6 +142,4 @@ deploy_prod: cd turron && docker-compose pull && docker-compose up -d --remove-orphans - " - only: - - main + " \ No newline at end of file -- GitLab From 651ec1a1c69f199987d76ecd59b2c86dc05be24f Mon Sep 17 00:00:00 2001 From: Fl1s Date: Sat, 11 Oct 2025 20:47:07 +0700 Subject: [PATCH 3/4] cd: add gitlab-runner to docker-compose.yaml --- docker-compose.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/docker-compose.yaml b/docker-compose.yaml index c1a1035..b76cf85 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,4 +1,11 @@ services: + gitlab-runner: + image: gitlab/gitlab-runner:alpine + container_name: gitlab-runner + restart: always + volumes: + - ./.gitlab/gitlab-runner/config:/etc/gitlab-runner + - /var/run/docker.sock:/var/run/docker.sock nginx: image: nginx:stable-alpine container_name: nginx -- GitLab From a169a426e91727676f440bccb43a2340091e8f07 Mon Sep 17 00:00:00 2001 From: Fl1s Date: Sat, 11 Oct 2025 20:55:13 +0700 Subject: [PATCH 4/4] cd: add gitlab-runner-register to docker-compose --- docker-compose.yaml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/docker-compose.yaml b/docker-compose.yaml index b76cf85..0524337 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,10 +1,19 @@ services: + gitlab-runner-register: + image: gitlab/gitlab-runner:alpine + container_name: gitlab-runner-register + volumes: + - ./.gitlab/gitlab-runner:/etc/gitlab-runner + entrypoint: [ "gitlab-runner", "register" ] + stdin_open: true # -it + tty: true # -it + restart: "no" # --rm gitlab-runner: image: gitlab/gitlab-runner:alpine container_name: gitlab-runner restart: always volumes: - - ./.gitlab/gitlab-runner/config:/etc/gitlab-runner + - ./.gitlab/gitlab-runner:/etc/gitlab-runner - /var/run/docker.sock:/var/run/docker.sock nginx: image: nginx:stable-alpine -- GitLab