diff --git a/.github/workflows/build-image.yml b/.github/workflows/build-image.yml index c92f2049..0e7d6af3 100644 --- a/.github/workflows/build-image.yml +++ b/.github/workflows/build-image.yml @@ -9,6 +9,9 @@ on: env: REGISTRY: ghcr.io +permissions: + contents: read + jobs: build-postgres: runs-on: ubuntu-latest diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml deleted file mode 100644 index d26cf994..00000000 --- a/.github/workflows/deploy.yml +++ /dev/null @@ -1,85 +0,0 @@ -name: Deploy - -on: - workflow_run: - workflows: - - Build Docker Image - types: - - completed - workflow_dispatch: - inputs: - image_tag: - description: Optional image tag for rollback or manual deploy - required: false - type: string - -jobs: - deploy: - if: github.event_name == 'workflow_dispatch' || (github.event_name == 'workflow_run' && github.event.workflow_run.conclusion == 'success') - runs-on: ubuntu-latest - environment: dev - concurrency: - group: dev-deploy - cancel-in-progress: false - - steps: - - name: Validate deployment settings - shell: bash - run: | - [ -n "${{ vars.DEPLOY_PATH }}" ] || { echo "DEPLOY_PATH is required"; exit 1; } - [ -n "${{ vars.SSH_HOST }}" ] || { echo "SSH_HOST is required"; exit 1; } - [ -n "${{ vars.SSH_PORT }}" ] || { echo "SSH_PORT is required"; exit 1; } - [ -n "${{ vars.SSH_USER }}" ] || { echo "SSH_USER is required"; exit 1; } - - - name: Resolve deployment variables - id: deploy - shell: bash - env: - IMAGE_TAG: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.image_tag || '' }} - run: | - core_image_name="${GITHUB_REPOSITORY_OWNER,,}/mailsangja-core" - worker_image_name="${GITHUB_REPOSITORY_OWNER,,}/mailsangja-worker" - postgres_image_name="${GITHUB_REPOSITORY_OWNER,,}/mailsangja-postgres" - - if [ "${{ github.event_name }}" = "workflow_run" ]; then - image_tag="sha-${{ github.event.workflow_run.head_sha }}" - elif [ -n "$IMAGE_TAG" ]; then - image_tag="$IMAGE_TAG" - else - image_tag="sha-${{ github.sha }}" - fi - - echo "core_image_name=$core_image_name" >> "$GITHUB_OUTPUT" - echo "worker_image_name=$worker_image_name" >> "$GITHUB_OUTPUT" - echo "postgres_image_name=$postgres_image_name" >> "$GITHUB_OUTPUT" - echo "image_tag=$image_tag" >> "$GITHUB_OUTPUT" - - - name: Deploy on server - uses: appleboy/ssh-action@v1 - env: - DEPLOY_PATH: ${{ vars.DEPLOY_PATH }} - CORE_IMAGE_NAME: ${{ steps.deploy.outputs.core_image_name }} - WORKER_IMAGE_NAME: ${{ steps.deploy.outputs.worker_image_name }} - POSTGRES_IMAGE_NAME: ${{ steps.deploy.outputs.postgres_image_name }} - APP_IMAGE_TAG: ${{ steps.deploy.outputs.image_tag }} - with: - username: ${{ vars.SSH_USER }} - host: ${{ vars.SSH_HOST }} - port: ${{ vars.SSH_PORT }} - key: ${{ secrets.SSH_PRIVATE_KEY }} - envs: DEPLOY_PATH,CORE_IMAGE_NAME,WORKER_IMAGE_NAME,POSTGRES_IMAGE_NAME,APP_IMAGE_TAG - script: | - set -euo pipefail - - cd "$DEPLOY_PATH" - - cat < .env.release - CORE_IMAGE_NAME=$CORE_IMAGE_NAME - WORKER_IMAGE_NAME=$WORKER_IMAGE_NAME - POSTGRES_IMAGE_NAME=$POSTGRES_IMAGE_NAME - APP_IMAGE_TAG=$APP_IMAGE_TAG - EOF - - docker compose -f compose.production.yaml --env-file .env --env-file .env.release pull postgres core worker - docker compose -f compose.production.yaml --env-file .env --env-file .env.release up -d postgres core worker - docker compose -f compose.production.yaml --env-file .env --env-file .env.release ps core worker diff --git a/.github/workflows/release-pr.yml b/.github/workflows/release-pr.yml new file mode 100644 index 00000000..58faf968 --- /dev/null +++ b/.github/workflows/release-pr.yml @@ -0,0 +1,144 @@ +name: Create Release PR + +on: + workflow_run: + workflows: + - Build Docker Image + types: + - completed + workflow_dispatch: + inputs: + image_tag: + description: Image tag to write, for example sha- + required: true + type: string + source_sha: + description: Source app commit SHA for the release PR body + required: false + type: string + +permissions: + contents: read + +jobs: + create-release-pr: + name: Create release PR + if: github.event_name == 'workflow_dispatch' || github.event.workflow_run.conclusion == 'success' + runs-on: ubuntu-latest + concurrency: + group: mailsangja-release-pr + cancel-in-progress: false + steps: + - name: Resolve image metadata + id: meta + shell: bash + env: + GH_TOKEN: ${{ github.token }} + DISPATCH_IMAGE_TAG: ${{ inputs.image_tag }} + DISPATCH_SOURCE_SHA: ${{ inputs.source_sha }} + WORKFLOW_RUN_BRANCH: ${{ github.event.workflow_run.head_branch }} + WORKFLOW_RUN_SHA: ${{ github.event.workflow_run.head_sha }} + WORKFLOW_RUN_URL: ${{ github.event.workflow_run.html_url }} + run: | + set -euo pipefail + + should_update=true + source_sha="$DISPATCH_SOURCE_SHA" + image_tag="$DISPATCH_IMAGE_TAG" + workflow_run_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}" + + if [ "${GITHUB_EVENT_NAME}" = "workflow_run" ]; then + if [ "$WORKFLOW_RUN_BRANCH" != "main" ]; then + should_update=false + echo "Skip GitOps PR because the source workflow ran on ${WORKFLOW_RUN_BRANCH}, not main." + fi + + source_sha="$WORKFLOW_RUN_SHA" + image_tag="sha-${WORKFLOW_RUN_SHA}" + workflow_run_url="$WORKFLOW_RUN_URL" + + latest_sha="$(gh api "repos/${GITHUB_REPOSITORY}/commits/main" --jq .sha)" + if [ "$latest_sha" != "$source_sha" ]; then + should_update=false + echo "Skip GitOps PR because ${source_sha} is no longer main HEAD (${latest_sha})." + fi + fi + + source_commit_url="" + if [ -n "$source_sha" ]; then + source_commit_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}/commit/${source_sha}" + fi + + { + echo "should_update=${should_update}" + echo "image_tag=${image_tag}" + echo "source_sha=${source_sha}" + echo "source_commit_url=${source_commit_url}" + echo "workflow_run_url=${workflow_run_url}" + } >> "$GITHUB_OUTPUT" + + - name: Checkout GitOps repository + if: steps.meta.outputs.should_update == 'true' + uses: actions/checkout@v6 + with: + repository: mailsangja/mailsangja-infra + token: ${{ secrets.GITOPS_PR_TOKEN }} + persist-credentials: false + ref: main + path: gitops + + - name: Update core and worker image tags + if: steps.meta.outputs.should_update == 'true' + shell: bash + working-directory: gitops + env: + IMAGE_TAG: ${{ steps.meta.outputs.image_tag }} + run: | + set -euo pipefail + + core_image="ghcr.io/mailsangja/mailsangja-core:${IMAGE_TAG}" + worker_image="ghcr.io/mailsangja/mailsangja-worker:${IMAGE_TAG}" + + sed -i -E "s#image: ghcr.io/mailsangja/mailsangja-core[^[:space:]]*#image: ${core_image}#" apps/mailsangja/core.yaml + sed -i -E "s#image: ghcr.io/mailsangja/mailsangja-worker[^[:space:]]*#image: ${worker_image}#" apps/mailsangja/worker.yaml + + grep -F "image: ${core_image}" apps/mailsangja/core.yaml >/dev/null + grep -F "image: ${worker_image}" apps/mailsangja/worker.yaml >/dev/null + + git diff -- apps/mailsangja/core.yaml apps/mailsangja/worker.yaml + + - name: Create or update GitOps pull request + if: steps.meta.outputs.should_update == 'true' + id: create-pull-request + uses: peter-evans/create-pull-request@v8 + with: + path: gitops + token: ${{ secrets.GITOPS_PR_TOKEN }} + branch: automation/mailsangja + base: main + delete-branch: true + commit-message: "release: 메일상자 이미지 업데이트 (${{ steps.meta.outputs.image_tag }})" + title: "release: 메일상자 이미지 업데이트 (${{ steps.meta.outputs.image_tag }})" + body: | + Automated image tag update from `mailsangja-server`. + + Source commit: ${{ steps.meta.outputs.source_commit_url }} + Workflow run: ${{ steps.meta.outputs.workflow_run_url }} + + Updated images: + - `ghcr.io/mailsangja/mailsangja-core:${{ steps.meta.outputs.image_tag }}` + - `ghcr.io/mailsangja/mailsangja-worker:${{ steps.meta.outputs.image_tag }}` + + Scope: + - `apps/mailsangja/core.yaml` + - `apps/mailsangja/worker.yaml` + + `apps/mailsangja/postgres.yaml` is intentionally unchanged. + Merge this PR to let Argo CD sync the new application images. + add-paths: | + apps/mailsangja/core.yaml + apps/mailsangja/worker.yaml + + - name: Print pull request result + if: steps.meta.outputs.should_update == 'true' && steps.create-pull-request.outputs.pull-request-operation != 'none' + run: echo "${{ steps.create-pull-request.outputs.pull-request-url }}" diff --git a/compose.production.yaml b/compose.production.yaml deleted file mode 100644 index b7a83528..00000000 --- a/compose.production.yaml +++ /dev/null @@ -1,198 +0,0 @@ -services: - caddy: - image: caddy:2.11.2-alpine - container_name: mailsangja-caddy - restart: unless-stopped - ports: - - "80:80" - - "443:443" - environment: - TZ: Asia/Seoul - API_DOMAIN: ${API_DOMAIN} - CADDY_EMAIL: ${CADDY_EMAIL} - volumes: - - ./Caddyfile:/etc/caddy/Caddyfile:ro - - mailsangja_caddy_data:/data - - mailsangja_caddy_config:/config - depends_on: - core: - condition: service_started - - postgres: - image: ghcr.io/${POSTGRES_IMAGE_NAME}:${APP_IMAGE_TAG} - container_name: mailsangja-postgres - restart: unless-stopped - environment: - TZ: Asia/Seoul - PGDATA: /var/lib/postgresql - POSTGRES_DB: ${DB_NAME:-mailsangja} - POSTGRES_USER: ${DB_USERNAME:-mailsangja} - POSTGRES_PASSWORD: ${DB_PASSWORD:-mailsangja} - volumes: - - mailsangja_postgres_data:/var/lib/postgresql - - ./db/src/main/resources/extensions.sql:/docker-entrypoint-initdb.d/00-extensions.sql:ro - healthcheck: - test: ["CMD-SHELL", "pg_isready -U ${DB_USERNAME:-mailsangja}"] - interval: 10s - timeout: 5s - retries: 5 - - redis: - image: redis:8.4.0-alpine3.22 - container_name: mailsangja-redis - restart: unless-stopped - environment: - TZ: Asia/Seoul - volumes: - - mailsangja_redis_data:/data - healthcheck: - test: ["CMD", "redis-cli", "ping"] - interval: 10s - timeout: 5s - retries: 5 - - rabbitmq: - image: rabbitmq:4.1.4-management-alpine - container_name: mailsangja-rabbitmq - restart: unless-stopped - environment: - TZ: Asia/Seoul - RABBITMQ_DEFAULT_USER: ${RABBITMQ_USERNAME:-guest} - RABBITMQ_DEFAULT_PASS: ${RABBITMQ_PASSWORD:-guest} - RABBITMQ_DEFAULT_VHOST: ${RABBITMQ_VHOST:-/} - volumes: - - mailsangja_rabbitmq_data:/var/lib/rabbitmq - healthcheck: - test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"] - interval: 10s - timeout: 5s - retries: 5 - - loki: - image: grafana/loki:3.5.8 - container_name: mailsangja-loki - restart: unless-stopped - command: ["-config.file=/etc/loki/loki.yaml"] - environment: - TZ: Asia/Seoul - volumes: - - ./docker/observability/loki/loki.yaml:/etc/loki/loki.yaml:ro - - mailsangja_loki_data:/loki - - tempo: - image: grafana/tempo:2.9.0 - container_name: mailsangja-tempo - restart: unless-stopped - command: ["-config.file=/etc/tempo/tempo.yaml"] - environment: - TZ: Asia/Seoul - volumes: - - ./docker/observability/tempo/tempo.yaml:/etc/tempo/tempo.yaml:ro - - mailsangja_tempo_data:/var/tempo - - alloy: - image: grafana/alloy:v1.11.3 - container_name: mailsangja-alloy - restart: unless-stopped - command: - - run - - /etc/alloy/config.alloy - - --server.http.listen-addr=0.0.0.0:12345 - environment: - TZ: Asia/Seoul - volumes: - - ./docker/observability/alloy/config.alloy:/etc/alloy/config.alloy:ro - - mailsangja_app_logs:/var/log/mailsangja:ro - depends_on: - loki: - condition: service_started - tempo: - condition: service_started - - grafana: - image: grafana/grafana:12.3.0 - container_name: mailsangja-grafana - restart: unless-stopped - environment: - TZ: Asia/Seoul - GF_SECURITY_ADMIN_USER: ${GRAFANA_ADMIN_USER:-admin} - GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD} - GF_AUTH_ANONYMOUS_ENABLED: ${GRAFANA_AUTH_ANONYMOUS_ENABLED:-false} - GF_SERVER_ROOT_URL: ${GRAFANA_ROOT_URL:-https://${API_DOMAIN}/api/admin/dashboard/grafana/} - GF_SERVER_SERVE_FROM_SUB_PATH: ${GRAFANA_SERVE_FROM_SUB_PATH:-true} - ports: - - "${GRAFANA_BIND_ADDRESS:-127.0.0.1}:${GRAFANA_PORT:-3000}:3000" - volumes: - - mailsangja_grafana_data:/var/lib/grafana - - ./docker/observability/grafana/provisioning:/etc/grafana/provisioning:ro - depends_on: - loki: - condition: service_started - tempo: - condition: service_started - - core: - image: ghcr.io/${CORE_IMAGE_NAME}:${APP_IMAGE_TAG} - container_name: mailsangja-core - restart: unless-stopped - environment: - TZ: Asia/Seoul - JAVA_TOOL_OPTIONS: "-Duser.timezone=Asia/Seoul" - CORE_LOG_FILE: /var/log/mailsangja/core.log - MANAGEMENT_TRACING_EXPORT_OTLP_ENABLED: ${MANAGEMENT_TRACING_EXPORT_OTLP_ENABLED:-true} - MANAGEMENT_OPENTELEMETRY_TRACING_EXPORT_OTLP_ENDPOINT: ${MANAGEMENT_OPENTELEMETRY_TRACING_EXPORT_OTLP_ENDPOINT:-http://alloy:4318/v1/traces} - env_file: - - .env - volumes: - - mailsangja_app_logs:/var/log/mailsangja - depends_on: - alloy: - condition: service_started - postgres: - condition: service_healthy - redis: - condition: service_healthy - rabbitmq: - condition: service_healthy - - worker: - image: ghcr.io/${WORKER_IMAGE_NAME}:${APP_IMAGE_TAG} - container_name: mailsangja-worker - restart: unless-stopped - environment: - TZ: Asia/Seoul - JAVA_TOOL_OPTIONS: "-Duser.timezone=Asia/Seoul" - WORKER_LOG_FILE: /var/log/mailsangja/worker.log - MANAGEMENT_TRACING_EXPORT_OTLP_ENABLED: ${MANAGEMENT_TRACING_EXPORT_OTLP_ENABLED:-true} - MANAGEMENT_OPENTELEMETRY_TRACING_EXPORT_OTLP_ENDPOINT: ${MANAGEMENT_OPENTELEMETRY_TRACING_EXPORT_OTLP_ENDPOINT:-http://alloy:4318/v1/traces} - env_file: - - .env - volumes: - - mailsangja_app_logs:/var/log/mailsangja - depends_on: - alloy: - condition: service_started - postgres: - condition: service_healthy - rabbitmq: - condition: service_healthy - -volumes: - mailsangja_postgres_data: - external: true - mailsangja_redis_data: - external: true - mailsangja_rabbitmq_data: - external: true - mailsangja_caddy_data: - external: true - mailsangja_caddy_config: - external: true - mailsangja_app_logs: - external: true - mailsangja_loki_data: - external: true - mailsangja_tempo_data: - external: true - mailsangja_grafana_data: - external: true