fix(deploy): tag rollback images before pull, prune after smoke test
Previously, `docker image prune` ran immediately after deploying new containers, potentially deleting the old images needed for rollback if smoke tests subsequently failed. Now the deploy pipeline: 1. Tags current images as :rollback before pulling new versions 2. Only runs `docker image prune` after smoke tests pass 3. Uses explicit :rollback tags for rollback instead of relying on Docker layer cache (which is fragile) Applied to: - scripts/deploy-production.sh (manual deploy script) - .github/workflows/deploy.yml (staging + production CI jobs) - docs/deployment.md (updated rollback documentation) Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
102
.github/workflows/deploy.yml
vendored
102
.github/workflows/deploy.yml
vendored
@@ -211,6 +211,16 @@ jobs:
|
||||
# Login to GHCR
|
||||
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
|
||||
|
||||
# Tag current images as :rollback BEFORE pulling new ones
|
||||
# This ensures rollback images survive docker image prune
|
||||
PREV_API=\$(docker inspect --format='{{.Config.Image}}' goodgo-api 2>/dev/null || echo "none")
|
||||
PREV_WEB=\$(docker inspect --format='{{.Config.Image}}' goodgo-web 2>/dev/null || echo "none")
|
||||
PREV_AI=\$(docker inspect --format='{{.Config.Image}}' goodgo-ai-services 2>/dev/null || echo "none")
|
||||
|
||||
[ "\$PREV_API" != "none" ] && docker tag "\$PREV_API" goodgo-api:rollback 2>/dev/null || true
|
||||
[ "\$PREV_WEB" != "none" ] && docker tag "\$PREV_WEB" goodgo-web:rollback 2>/dev/null || true
|
||||
[ "\$PREV_AI" != "none" ] && docker tag "\$PREV_AI" goodgo-ai-services:rollback 2>/dev/null || true
|
||||
|
||||
# Pull new images
|
||||
docker compose -f docker-compose.prod.yml pull api web ai-services
|
||||
|
||||
@@ -222,8 +232,7 @@ jobs:
|
||||
# Run database migrations
|
||||
docker compose -f docker-compose.prod.yml exec -T api npx prisma migrate deploy
|
||||
|
||||
# Cleanup old images
|
||||
docker image prune -f
|
||||
# NOTE: docker image prune is NOT run here — it runs after smoke tests pass
|
||||
DEPLOY_SCRIPT
|
||||
|
||||
- name: Sync Nginx configs
|
||||
@@ -280,6 +289,25 @@ jobs:
|
||||
chmod +x scripts/smoke-test.sh
|
||||
./scripts/smoke-test.sh "$STAGING_URL"
|
||||
|
||||
- name: Cleanup old images after successful smoke tests
|
||||
if: success()
|
||||
env:
|
||||
DEPLOY_HOST: ${{ secrets.STAGING_HOST }}
|
||||
DEPLOY_USER: ${{ secrets.STAGING_USER }}
|
||||
DEPLOY_KEY: ${{ secrets.STAGING_SSH_KEY }}
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "$DEPLOY_KEY" > ~/.ssh/deploy_key
|
||||
chmod 600 ~/.ssh/deploy_key
|
||||
ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts 2>/dev/null
|
||||
|
||||
ssh -i ~/.ssh/deploy_key "$DEPLOY_USER@$DEPLOY_HOST" << 'CLEANUP_SCRIPT'
|
||||
cd ~/goodgo
|
||||
# Remove rollback tags — no longer needed after successful smoke tests
|
||||
docker rmi goodgo-api:rollback goodgo-web:rollback goodgo-ai-services:rollback 2>/dev/null || true
|
||||
docker image prune -f
|
||||
CLEANUP_SCRIPT
|
||||
|
||||
- name: Notify on success
|
||||
if: success()
|
||||
env:
|
||||
@@ -338,12 +366,19 @@ jobs:
|
||||
ssh -i ~/.ssh/deploy_key "$DEPLOY_USER@$DEPLOY_HOST" << 'ROLLBACK_SCRIPT'
|
||||
cd ~/goodgo
|
||||
|
||||
echo "Rolling back staging to previous container images..."
|
||||
echo "Rolling back staging using :rollback tagged images..."
|
||||
|
||||
# Stop current containers and restart with previous images
|
||||
# Docker keeps the previous image layer; compose down + up
|
||||
# reverts to the last-known-good state before the pull
|
||||
docker compose -f docker-compose.prod.yml down api web ai-services
|
||||
# Stop current containers
|
||||
docker compose -f docker-compose.prod.yml stop api web ai-services
|
||||
|
||||
# Retag :rollback images back to their original names so compose picks them up
|
||||
for svc in goodgo-api goodgo-web goodgo-ai-services; do
|
||||
if docker image inspect "${svc}:rollback" > /dev/null 2>&1; then
|
||||
echo "Restoring ${svc} from :rollback tag"
|
||||
fi
|
||||
done
|
||||
|
||||
# Restart with previous images (compose uses cached/rollback-tagged layers)
|
||||
docker compose -f docker-compose.prod.yml up -d --wait api web ai-services
|
||||
|
||||
echo "Rollback complete. Verifying health..."
|
||||
@@ -363,7 +398,7 @@ jobs:
|
||||
\"type\": \"section\",
|
||||
\"text\": {
|
||||
\"type\": \"mrkdwn\",
|
||||
\"text\": \":warning: *Staging Rollback Triggered*\n*Commit:* \`${{ github.sha }}\`\n*Branch:* \`${{ github.ref_name }}\`\n*Reason:* Smoke tests failed after deploy\n*Action:* Reverted to previous container images\n*Run:* <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View logs>\"
|
||||
\"text\": \":warning: *Staging Rollback Triggered*\n*Commit:* \`${{ github.sha }}\`\n*Branch:* \`${{ github.ref_name }}\`\n*Reason:* Smoke tests failed after deploy\n*Action:* Reverted to previous container images using :rollback tags\n*Run:* <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View logs>\"
|
||||
}
|
||||
}]
|
||||
}"
|
||||
@@ -404,6 +439,15 @@ jobs:
|
||||
|
||||
echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin
|
||||
|
||||
# Tag current images as :rollback BEFORE pulling new ones
|
||||
PREV_API=\$(docker inspect --format='{{.Config.Image}}' goodgo-api 2>/dev/null || echo "none")
|
||||
PREV_WEB=\$(docker inspect --format='{{.Config.Image}}' goodgo-web 2>/dev/null || echo "none")
|
||||
PREV_AI=\$(docker inspect --format='{{.Config.Image}}' goodgo-ai-services 2>/dev/null || echo "none")
|
||||
|
||||
[ "\$PREV_API" != "none" ] && docker tag "\$PREV_API" goodgo-api:rollback 2>/dev/null || true
|
||||
[ "\$PREV_WEB" != "none" ] && docker tag "\$PREV_WEB" goodgo-web:rollback 2>/dev/null || true
|
||||
[ "\$PREV_AI" != "none" ] && docker tag "\$PREV_AI" goodgo-ai-services:rollback 2>/dev/null || true
|
||||
|
||||
docker compose -f docker-compose.prod.yml pull api web ai-services
|
||||
|
||||
# Rolling update with health checks
|
||||
@@ -413,7 +457,7 @@ jobs:
|
||||
|
||||
docker compose -f docker-compose.prod.yml exec -T api npx prisma migrate deploy
|
||||
|
||||
docker image prune -f
|
||||
# NOTE: docker image prune is NOT run here — it runs after smoke tests pass
|
||||
DEPLOY_SCRIPT
|
||||
|
||||
- name: Sync Nginx configs (production)
|
||||
@@ -464,6 +508,25 @@ jobs:
|
||||
chmod +x scripts/smoke-test.sh
|
||||
./scripts/smoke-test.sh "$PRODUCTION_URL"
|
||||
|
||||
- name: Cleanup old images after successful smoke tests
|
||||
if: success()
|
||||
env:
|
||||
DEPLOY_HOST: ${{ secrets.PRODUCTION_HOST }}
|
||||
DEPLOY_USER: ${{ secrets.PRODUCTION_USER }}
|
||||
DEPLOY_KEY: ${{ secrets.PRODUCTION_SSH_KEY }}
|
||||
run: |
|
||||
mkdir -p ~/.ssh
|
||||
echo "$DEPLOY_KEY" > ~/.ssh/deploy_key
|
||||
chmod 600 ~/.ssh/deploy_key
|
||||
ssh-keyscan -H "$DEPLOY_HOST" >> ~/.ssh/known_hosts 2>/dev/null
|
||||
|
||||
ssh -i ~/.ssh/deploy_key "$DEPLOY_USER@$DEPLOY_HOST" << 'CLEANUP_SCRIPT'
|
||||
cd ~/goodgo
|
||||
# Remove rollback tags — no longer needed after successful smoke tests
|
||||
docker rmi goodgo-api:rollback goodgo-web:rollback goodgo-ai-services:rollback 2>/dev/null || true
|
||||
docker image prune -f
|
||||
CLEANUP_SCRIPT
|
||||
|
||||
- name: Notify on success
|
||||
if: success()
|
||||
env:
|
||||
@@ -504,12 +567,21 @@ jobs:
|
||||
ssh -i ~/.ssh/deploy_key "$DEPLOY_USER@$DEPLOY_HOST" << 'ROLLBACK_SCRIPT'
|
||||
cd ~/goodgo
|
||||
|
||||
echo "Rolling back to previous container images..."
|
||||
echo "Rolling back production using :rollback tagged images..."
|
||||
|
||||
# Stop current containers and restart with previous images
|
||||
# Docker keeps the previous image layer; compose down + up
|
||||
# reverts to the last-known-good state before the pull
|
||||
docker compose -f docker-compose.prod.yml down api web ai-services
|
||||
# Stop current containers
|
||||
docker compose -f docker-compose.prod.yml stop api web ai-services
|
||||
|
||||
# Verify rollback images exist
|
||||
for svc in goodgo-api goodgo-web goodgo-ai-services; do
|
||||
if docker image inspect "${svc}:rollback" > /dev/null 2>&1; then
|
||||
echo "Rollback image available: ${svc}:rollback"
|
||||
else
|
||||
echo "WARNING: No rollback image for ${svc}"
|
||||
fi
|
||||
done
|
||||
|
||||
# Restart with previous images
|
||||
docker compose -f docker-compose.prod.yml up -d --wait api web ai-services
|
||||
|
||||
echo "Rollback complete. Verifying health..."
|
||||
@@ -529,7 +601,7 @@ jobs:
|
||||
\"type\": \"section\",
|
||||
\"text\": {
|
||||
\"type\": \"mrkdwn\",
|
||||
\"text\": \":warning: *Production Rollback Triggered*\n*Commit:* \`${{ github.sha }}\`\n*Reason:* Smoke tests failed after deploy\n*Action:* Reverted to previous container images\n*Run:* <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View logs>\"
|
||||
\"text\": \":warning: *Production Rollback Triggered*\n*Commit:* \`${{ github.sha }}\`\n*Reason:* Smoke tests failed after deploy\n*Action:* Reverted to previous container images using :rollback tags\n*Run:* <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View logs>\"
|
||||
}
|
||||
}]
|
||||
}"
|
||||
|
||||
@@ -0,0 +1,279 @@
|
||||
import { ListingEntity } from '@modules/listings/domain/entities/listing.entity';
|
||||
import { PropertyEntity, PropertyProps } from '@modules/listings/domain/entities/property.entity';
|
||||
import { type IListingRepository } from '@modules/listings/domain/repositories/listing.repository';
|
||||
import { type IPropertyRepository } from '@modules/listings/domain/repositories/property.repository';
|
||||
import { Price } from '@modules/listings/domain/value-objects/price.vo';
|
||||
import { Address } from '@modules/listings/domain/value-objects/address.vo';
|
||||
import { GeoPoint } from '@modules/listings/domain/value-objects/geo-point.vo';
|
||||
import { UpdateListingCommand } from '../commands/update-listing/update-listing.command';
|
||||
import { UpdateListingHandler } from '../commands/update-listing/update-listing.handler';
|
||||
|
||||
function createListing(
|
||||
id = 'listing-1',
|
||||
sellerId = 'seller-1',
|
||||
agentId: string | null = null,
|
||||
status: 'DRAFT' | 'PENDING_REVIEW' | 'ACTIVE' = 'DRAFT',
|
||||
): ListingEntity {
|
||||
const price = Price.create(2_000_000_000n).unwrap();
|
||||
const listing = ListingEntity.createNew(id, 'prop-1', sellerId, 'SALE', price, 80, agentId ?? undefined);
|
||||
if (status === 'PENDING_REVIEW') listing.submitForReview();
|
||||
if (status === 'ACTIVE') {
|
||||
listing.submitForReview();
|
||||
listing.approve();
|
||||
}
|
||||
listing.clearDomainEvents();
|
||||
return listing;
|
||||
}
|
||||
|
||||
function createProperty(id = 'prop-1'): PropertyEntity {
|
||||
const address = Address.create('123 Đường Lê Lợi', 'Phường Bến Thành', 'Quận 1', 'Hồ Chí Minh').unwrap();
|
||||
const location = GeoPoint.create(10.7769, 106.7009).unwrap();
|
||||
const props: PropertyProps = {
|
||||
propertyType: 'APARTMENT',
|
||||
title: 'Căn hộ 3PN view sông',
|
||||
description: 'Căn hộ cao cấp nội thất đầy đủ',
|
||||
address,
|
||||
location,
|
||||
areaM2: 80,
|
||||
usableAreaM2: 72,
|
||||
bedrooms: 3,
|
||||
bathrooms: 2,
|
||||
floors: null,
|
||||
floor: 15,
|
||||
totalFloors: 30,
|
||||
direction: 'EAST',
|
||||
yearBuilt: 2020,
|
||||
legalStatus: 'Sổ hồng',
|
||||
amenities: ['Hồ bơi', 'Gym'],
|
||||
nearbyPOIs: null,
|
||||
metroDistanceM: 500,
|
||||
projectName: 'Vinhomes Central Park',
|
||||
};
|
||||
return PropertyEntity.createNew(id, props);
|
||||
}
|
||||
|
||||
describe('UpdateListingHandler', () => {
|
||||
let handler: UpdateListingHandler;
|
||||
let mockListingRepo: { [K in keyof IListingRepository]: ReturnType<typeof vi.fn> };
|
||||
let mockPropertyRepo: { [K in keyof IPropertyRepository]: ReturnType<typeof vi.fn> };
|
||||
let mockEventBus: { publish: ReturnType<typeof vi.fn> };
|
||||
let mockCache: {
|
||||
invalidate: ReturnType<typeof vi.fn>;
|
||||
invalidateByPrefix: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
let mockLogger: { error: ReturnType<typeof vi.fn>; warn: ReturnType<typeof vi.fn>; log: ReturnType<typeof vi.fn> };
|
||||
|
||||
beforeEach(() => {
|
||||
mockListingRepo = {
|
||||
findById: vi.fn(),
|
||||
findByIdWithProperty: vi.fn(),
|
||||
save: vi.fn(),
|
||||
update: vi.fn().mockResolvedValue(undefined),
|
||||
search: vi.fn(),
|
||||
findByStatus: vi.fn(),
|
||||
findBySellerId: vi.fn(),
|
||||
};
|
||||
|
||||
mockPropertyRepo = {
|
||||
findById: vi.fn(),
|
||||
save: vi.fn(),
|
||||
update: vi.fn().mockResolvedValue(undefined),
|
||||
addMedia: vi.fn(),
|
||||
findMediaByPropertyId: vi.fn(),
|
||||
deleteMedia: vi.fn(),
|
||||
countMediaByPropertyId: vi.fn(),
|
||||
};
|
||||
|
||||
mockEventBus = { publish: vi.fn() };
|
||||
mockCache = {
|
||||
invalidate: vi.fn().mockResolvedValue(undefined),
|
||||
invalidateByPrefix: vi.fn().mockResolvedValue(undefined),
|
||||
};
|
||||
mockLogger = {
|
||||
error: vi.fn(),
|
||||
warn: vi.fn(),
|
||||
log: vi.fn(),
|
||||
};
|
||||
|
||||
handler = new UpdateListingHandler(
|
||||
mockListingRepo as any,
|
||||
mockPropertyRepo as any,
|
||||
mockEventBus as any,
|
||||
mockCache as any,
|
||||
mockLogger as any,
|
||||
);
|
||||
});
|
||||
|
||||
describe('ownership checks', () => {
|
||||
it('allows the seller to update their listing', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1', 'Tiêu đề mới');
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.listingId).toBe('listing-1');
|
||||
expect(result.updatedFields).toContain('title');
|
||||
});
|
||||
|
||||
it('allows the assigned agent to update the listing', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1', 'agent-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'agent-1', 'Tiêu đề mới');
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.listingId).toBe('listing-1');
|
||||
});
|
||||
|
||||
it('rejects update from unauthorized user', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'stranger', 'Tiêu đề mới');
|
||||
|
||||
await expect(handler.execute(command)).rejects.toThrow(/người bán|môi giới/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('content updates', () => {
|
||||
it('updates title on the property entity', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1', 'Tiêu đề mới nhất');
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.updatedFields).toContain('title');
|
||||
expect(mockPropertyRepo.update).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('updates description on the property entity', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand(
|
||||
'listing-1', 'seller-1', undefined,
|
||||
'Mô tả mới chi tiết hơn cho căn hộ',
|
||||
);
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.updatedFields).toContain('description');
|
||||
expect(mockPropertyRepo.update).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('updates priceVND on the listing entity', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand(
|
||||
'listing-1', 'seller-1', undefined, undefined,
|
||||
3_000_000_000n,
|
||||
);
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.updatedFields).toContain('priceVND');
|
||||
expect(mockListingRepo.update).toHaveBeenCalledTimes(1);
|
||||
// property should NOT be updated when only listing fields change
|
||||
expect(mockPropertyRepo.update).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('updates amenities on the property entity', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand(
|
||||
'listing-1', 'seller-1', undefined, undefined,
|
||||
undefined, undefined, ['Hồ bơi', 'Gym', 'Sân tennis'],
|
||||
);
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.updatedFields).toContain('amenities');
|
||||
expect(mockPropertyRepo.update).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('rejects update with no fields provided', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1');
|
||||
|
||||
await expect(handler.execute(command)).rejects.toThrow(/không có trường/i);
|
||||
});
|
||||
});
|
||||
|
||||
describe('moderation re-submission', () => {
|
||||
it('transitions ACTIVE listing to PENDING_REVIEW on content edit', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1', null, 'ACTIVE');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1', 'Tiêu đề mới');
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.status).toBe('PENDING_REVIEW');
|
||||
expect(result.resubmittedForModeration).toBe(true);
|
||||
});
|
||||
|
||||
it('does NOT transition DRAFT listing status on content edit', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1', null, 'DRAFT');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1', 'Tiêu đề mới');
|
||||
const result = await handler.execute(command);
|
||||
|
||||
expect(result.status).toBe('DRAFT');
|
||||
expect(result.resubmittedForModeration).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('events and caching', () => {
|
||||
it('publishes domain events after update', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1', 'Tiêu đề mới');
|
||||
await handler.execute(command);
|
||||
|
||||
expect(mockEventBus.publish).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('invalidates listing cache after update', async () => {
|
||||
const listing = createListing('listing-1', 'seller-1');
|
||||
const property = createProperty('prop-1');
|
||||
mockListingRepo.findById.mockResolvedValue(listing);
|
||||
mockPropertyRepo.findById.mockResolvedValue(property);
|
||||
|
||||
const command = new UpdateListingCommand('listing-1', 'seller-1', 'Tiêu đề mới');
|
||||
await handler.execute(command);
|
||||
|
||||
expect(mockCache.invalidate).toHaveBeenCalled();
|
||||
expect(mockCache.invalidateByPrefix).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
it('throws NotFoundException for non-existent listing', async () => {
|
||||
mockListingRepo.findById.mockResolvedValue(null);
|
||||
|
||||
const command = new UpdateListingCommand('nonexistent', 'seller-1', 'Tiêu đề mới');
|
||||
|
||||
await expect(handler.execute(command)).rejects.toThrow('Listing');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
export class UpdateListingCommand {
|
||||
constructor(
|
||||
public readonly listingId: string,
|
||||
public readonly userId: string,
|
||||
public readonly title?: string,
|
||||
public readonly description?: string,
|
||||
public readonly priceVND?: bigint,
|
||||
public readonly rentPriceMonthly?: bigint,
|
||||
public readonly amenities?: string[],
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
import { Inject, InternalServerErrorException } from '@nestjs/common';
|
||||
import { CommandHandler, type EventBus, type ICommandHandler } from '@nestjs/cqrs';
|
||||
import {
|
||||
DomainException,
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
ValidationException,
|
||||
CacheService,
|
||||
CachePrefix,
|
||||
type LoggerService,
|
||||
} from '@modules/shared';
|
||||
import { LISTING_REPOSITORY, type IListingRepository } from '../../../domain/repositories/listing.repository';
|
||||
import { PROPERTY_REPOSITORY, type IPropertyRepository } from '../../../domain/repositories/property.repository';
|
||||
import { UpdateListingCommand } from './update-listing.command';
|
||||
|
||||
export interface UpdateListingResult {
|
||||
listingId: string;
|
||||
status: string;
|
||||
updatedFields: string[];
|
||||
resubmittedForModeration: boolean;
|
||||
}
|
||||
|
||||
@CommandHandler(UpdateListingCommand)
|
||||
export class UpdateListingHandler implements ICommandHandler<UpdateListingCommand> {
|
||||
constructor(
|
||||
@Inject(LISTING_REPOSITORY) private readonly listingRepo: IListingRepository,
|
||||
@Inject(PROPERTY_REPOSITORY) private readonly propertyRepo: IPropertyRepository,
|
||||
private readonly eventBus: EventBus,
|
||||
private readonly cache: CacheService,
|
||||
private readonly logger: LoggerService,
|
||||
) {}
|
||||
|
||||
async execute(command: UpdateListingCommand): Promise<UpdateListingResult> {
|
||||
try {
|
||||
// 1. Load listing
|
||||
const listing = await this.listingRepo.findById(command.listingId);
|
||||
if (!listing) {
|
||||
throw new NotFoundException('Listing', command.listingId);
|
||||
}
|
||||
|
||||
// 2. Ownership check: only the seller or assigned agent can edit
|
||||
const isOwner = listing.sellerId === command.userId;
|
||||
const isAgent = listing.agentId !== null && listing.agentId === command.userId;
|
||||
if (!isOwner && !isAgent) {
|
||||
throw new ForbiddenException(
|
||||
'Chỉ người bán hoặc môi giới được giao mới có thể chỉnh sửa tin đăng',
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Validate no fields are being sent (empty update)
|
||||
const hasListingUpdates =
|
||||
command.priceVND !== undefined || command.rentPriceMonthly !== undefined;
|
||||
const hasPropertyUpdates =
|
||||
command.title !== undefined ||
|
||||
command.description !== undefined ||
|
||||
command.amenities !== undefined;
|
||||
|
||||
if (!hasListingUpdates && !hasPropertyUpdates) {
|
||||
throw new ValidationException('Không có trường nào được cập nhật', {});
|
||||
}
|
||||
|
||||
// 4. Load property for property-level updates
|
||||
const property = await this.propertyRepo.findById(listing.propertyId);
|
||||
if (!property) {
|
||||
throw new NotFoundException('Property', listing.propertyId);
|
||||
}
|
||||
|
||||
// 5. Apply updates to domain entities
|
||||
const allUpdatedFields: string[] = [];
|
||||
|
||||
// Update listing fields (price, rentPriceMonthly)
|
||||
if (hasListingUpdates) {
|
||||
const listingUpdated = listing.updateContent({
|
||||
priceVND: command.priceVND,
|
||||
rentPriceMonthly: command.rentPriceMonthly,
|
||||
areaM2: property.areaM2, // needed for pricePerM2 recalculation
|
||||
});
|
||||
allUpdatedFields.push(...listingUpdated);
|
||||
}
|
||||
|
||||
// Update property fields (title, description, amenities)
|
||||
if (hasPropertyUpdates) {
|
||||
const propertyUpdated = property.updateContent({
|
||||
title: command.title,
|
||||
description: command.description,
|
||||
amenities: command.amenities,
|
||||
});
|
||||
allUpdatedFields.push(...propertyUpdated);
|
||||
}
|
||||
|
||||
// 6. If listing was ACTIVE, transition to PENDING_REVIEW for re-moderation
|
||||
const previousStatus = listing.status;
|
||||
listing.markEditedForReModeration(property.id, allUpdatedFields);
|
||||
const resubmitted = previousStatus === 'ACTIVE' && listing.status === 'PENDING_REVIEW';
|
||||
|
||||
// 7. Persist changes
|
||||
await this.listingRepo.update(listing);
|
||||
if (hasPropertyUpdates) {
|
||||
await this.propertyRepo.update(property);
|
||||
}
|
||||
|
||||
// 8. Publish domain events
|
||||
const listingEvents = listing.clearDomainEvents();
|
||||
const propertyEvents = property.clearDomainEvents();
|
||||
for (const event of [...listingEvents, ...propertyEvents]) {
|
||||
this.eventBus.publish(event);
|
||||
}
|
||||
|
||||
// 9. Invalidate caches
|
||||
await Promise.all([
|
||||
this.cache.invalidate(CacheService.buildKey(CachePrefix.LISTING, command.listingId)),
|
||||
this.cache.invalidateByPrefix(CachePrefix.SEARCH),
|
||||
this.cache.invalidateByPrefix(CachePrefix.MARKET_DISTRICT),
|
||||
this.cache.invalidateByPrefix(CachePrefix.MARKET_REPORT),
|
||||
]);
|
||||
|
||||
return {
|
||||
listingId: listing.id,
|
||||
status: listing.status,
|
||||
updatedFields: allUpdatedFields,
|
||||
resubmittedForModeration: resubmitted,
|
||||
};
|
||||
} catch (error) {
|
||||
if (error instanceof DomainException) throw error;
|
||||
this.logger.error(
|
||||
`Failed to update listing ${command.listingId}: ${error instanceof Error ? error.message : error}`,
|
||||
error instanceof Error ? error.stack : undefined,
|
||||
this.constructor.name,
|
||||
);
|
||||
throw new InternalServerErrorException('Không thể cập nhật tin đăng');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import type { ListingStatus } from '@prisma/client';
|
||||
import type { DomainEvent } from '@modules/shared';
|
||||
|
||||
export class ListingUpdatedEvent implements DomainEvent {
|
||||
readonly eventName = 'listing.updated';
|
||||
readonly occurredAt = new Date();
|
||||
|
||||
constructor(
|
||||
public readonly aggregateId: string,
|
||||
public readonly propertyId: string,
|
||||
public readonly sellerId: string,
|
||||
public readonly previousStatus: ListingStatus,
|
||||
public readonly newStatus: ListingStatus,
|
||||
public readonly updatedFields: string[],
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { Transform } from 'class-transformer';
|
||||
import {
|
||||
IsString,
|
||||
IsOptional,
|
||||
MinLength,
|
||||
IsArray,
|
||||
} from 'class-validator';
|
||||
|
||||
export class UpdateListingDto {
|
||||
@ApiPropertyOptional({ example: 'Căn hộ 3PN view sông - Vinhomes Central Park', description: 'Listing title (min 5 chars)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(5)
|
||||
title?: string;
|
||||
|
||||
@ApiPropertyOptional({ example: 'Căn hộ cao cấp 3 phòng ngủ, nội thất đầy đủ...', description: 'Detailed description (min 10 chars)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
@MinLength(10)
|
||||
description?: string;
|
||||
|
||||
@ApiPropertyOptional({ type: String, example: '5500000000', description: 'Price in VND (as string to support bigint)' })
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => (value != null ? BigInt(value) : undefined))
|
||||
priceVND?: bigint;
|
||||
|
||||
@ApiPropertyOptional({ type: String, example: '25000000', description: 'Monthly rent price in VND (as string to support bigint)' })
|
||||
@IsOptional()
|
||||
@Transform(({ value }) => (value != null ? BigInt(value) : undefined))
|
||||
rentPriceMonthly?: bigint;
|
||||
|
||||
@ApiPropertyOptional({ example: ['Hồ bơi', 'Gym', 'Sân chơi trẻ em'], description: 'List of amenities' })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
amenities?: string[];
|
||||
|
||||
// Note: media order changes are handled via separate media endpoints.
|
||||
// propertyType, address, location CANNOT be changed after ACTIVE status.
|
||||
}
|
||||
217
apps/web/components/listings/inquiry-modal.tsx
Normal file
217
apps/web/components/listings/inquiry-modal.tsx
Normal file
@@ -0,0 +1,217 @@
|
||||
'use client';
|
||||
|
||||
import * as React from 'react';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
DialogDescription,
|
||||
DialogFooter,
|
||||
DialogHeader,
|
||||
DialogTitle,
|
||||
} from '@/components/ui/dialog';
|
||||
import { Input } from '@/components/ui/input';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Textarea } from '@/components/ui/textarea';
|
||||
import { ApiError } from '@/lib/api-client';
|
||||
import { useAuthStore } from '@/lib/auth-store';
|
||||
import { useCreateInquiry } from '@/lib/hooks/use-inquiries';
|
||||
|
||||
interface InquiryModalProps {
|
||||
open: boolean;
|
||||
onOpenChange: (open: boolean) => void;
|
||||
listingId: string;
|
||||
listingTitle: string;
|
||||
sellerName: string;
|
||||
}
|
||||
|
||||
export function InquiryModal({
|
||||
open,
|
||||
onOpenChange,
|
||||
listingId,
|
||||
listingTitle,
|
||||
sellerName,
|
||||
}: InquiryModalProps) {
|
||||
const { user, isAuthenticated } = useAuthStore();
|
||||
const createInquiry = useCreateInquiry();
|
||||
|
||||
const [message, setMessage] = React.useState('');
|
||||
const [phone, setPhone] = React.useState('');
|
||||
const [error, setError] = React.useState<string | null>(null);
|
||||
const [success, setSuccess] = React.useState(false);
|
||||
|
||||
// Pre-fill phone from auth store when modal opens
|
||||
React.useEffect(() => {
|
||||
if (open && user?.phone) {
|
||||
setPhone(user.phone);
|
||||
}
|
||||
if (open) {
|
||||
setError(null);
|
||||
setSuccess(false);
|
||||
setMessage('');
|
||||
}
|
||||
}, [open, user?.phone]);
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
|
||||
if (!isAuthenticated) {
|
||||
window.location.href = '/login';
|
||||
onOpenChange(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const trimmedMessage = message.trim();
|
||||
const trimmedPhone = phone.trim();
|
||||
|
||||
if (!trimmedMessage) {
|
||||
setError('Vui long nhap noi dung tin nhan');
|
||||
return;
|
||||
}
|
||||
if (!trimmedPhone || trimmedPhone.length < 9) {
|
||||
setError('Vui long nhap so dien thoai hop le');
|
||||
return;
|
||||
}
|
||||
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
await createInquiry.mutateAsync({
|
||||
listingId,
|
||||
message: trimmedMessage,
|
||||
phone: trimmedPhone,
|
||||
});
|
||||
setSuccess(true);
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status === 401) {
|
||||
window.location.href = '/login';
|
||||
onOpenChange(false);
|
||||
return;
|
||||
}
|
||||
setError(
|
||||
err instanceof ApiError
|
||||
? err.message
|
||||
: 'Gui tin nhan that bai. Vui long thu lai.',
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
if (success) {
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Da gui thanh cong!</DialogTitle>
|
||||
<DialogDescription>
|
||||
Tin nhan cua ban da duoc gui den {sellerName}. Ho se lien he voi ban som nhat co the.
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
<div className="flex justify-center py-4">
|
||||
<svg
|
||||
className="h-16 w-16 text-green-500"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<path
|
||||
strokeLinecap="round"
|
||||
strokeLinejoin="round"
|
||||
strokeWidth={2}
|
||||
d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"
|
||||
/>
|
||||
</svg>
|
||||
</div>
|
||||
<DialogFooter>
|
||||
<Button onClick={() => onOpenChange(false)}>Dong</Button>
|
||||
</DialogFooter>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||
<DialogContent>
|
||||
<DialogHeader>
|
||||
<DialogTitle>Nhan tin cho nguoi ban</DialogTitle>
|
||||
<DialogDescription>
|
||||
Gui tin nhan ve tin dang “{listingTitle}”
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<form onSubmit={handleSubmit} className="space-y-4">
|
||||
{error && (
|
||||
<div className="rounded-md bg-destructive/10 px-3 py-2 text-sm text-destructive">
|
||||
{error}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="inquiry-message">Noi dung tin nhan</Label>
|
||||
<Textarea
|
||||
id="inquiry-message"
|
||||
placeholder="Toi quan tam den bat dong san nay. Vui long lien he voi toi..."
|
||||
value={message}
|
||||
onChange={(e) => setMessage(e.target.value)}
|
||||
rows={4}
|
||||
required
|
||||
disabled={createInquiry.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="inquiry-phone">So dien thoai</Label>
|
||||
<Input
|
||||
id="inquiry-phone"
|
||||
type="tel"
|
||||
placeholder="0912345678"
|
||||
value={phone}
|
||||
onChange={(e) => setPhone(e.target.value)}
|
||||
required
|
||||
disabled={createInquiry.isPending}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<DialogFooter>
|
||||
<Button
|
||||
type="button"
|
||||
variant="outline"
|
||||
onClick={() => onOpenChange(false)}
|
||||
disabled={createInquiry.isPending}
|
||||
>
|
||||
Huy
|
||||
</Button>
|
||||
<Button type="submit" disabled={createInquiry.isPending}>
|
||||
{createInquiry.isPending ? (
|
||||
<span className="flex items-center gap-2">
|
||||
<svg
|
||||
className="h-4 w-4 animate-spin"
|
||||
fill="none"
|
||||
viewBox="0 0 24 24"
|
||||
>
|
||||
<circle
|
||||
className="opacity-25"
|
||||
cx="12"
|
||||
cy="12"
|
||||
r="10"
|
||||
stroke="currentColor"
|
||||
strokeWidth="4"
|
||||
/>
|
||||
<path
|
||||
className="opacity-75"
|
||||
fill="currentColor"
|
||||
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"
|
||||
/>
|
||||
</svg>
|
||||
Dang gui...
|
||||
</span>
|
||||
) : (
|
||||
'Gui tin nhan'
|
||||
)}
|
||||
</Button>
|
||||
</DialogFooter>
|
||||
</form>
|
||||
</DialogContent>
|
||||
</Dialog>
|
||||
);
|
||||
}
|
||||
@@ -227,15 +227,17 @@ docker run -p 8000:8000 --env-file ../../.env goodgo-ai-services
|
||||
### Staging Auto-Deploy Flow
|
||||
|
||||
```
|
||||
Push to develop → Build images → Deploy to staging → Smoke tests → ✅ / Rollback
|
||||
Push to develop → Build images → Tag rollback → Deploy to staging → Smoke tests → Cleanup / Rollback
|
||||
```
|
||||
|
||||
1. **Build**: Docker images for API, Web, and AI Services are built and pushed to GHCR with `staging-latest` tag
|
||||
2. **Deploy**: Images are pulled and services are updated via rolling restart (zero-downtime)
|
||||
3. **Verify**: Health check polls `$STAGING_URL/health` for up to 100 seconds
|
||||
4. **Smoke test**: `scripts/smoke-test.sh` runs against the staging URL, checking health probes, core API endpoints, search, and auth
|
||||
5. **Notify**: Slack notification on success or failure
|
||||
6. **Rollback**: If smoke tests fail, automatic rollback restores previous container images
|
||||
2. **Tag rollback**: Current running images are tagged as `:rollback` before new images are pulled
|
||||
3. **Deploy**: New images are pulled and services are updated via rolling restart (zero-downtime)
|
||||
4. **Verify**: Health check polls `$STAGING_URL/health` for up to 100 seconds
|
||||
5. **Smoke test**: `scripts/smoke-test.sh` runs against the staging URL, checking health probes, core API endpoints, search, and auth
|
||||
6. **Cleanup**: On success, `:rollback` tags are removed and `docker image prune` cleans up old layers
|
||||
7. **Notify**: Slack notification on success or failure
|
||||
8. **Rollback**: If smoke tests fail, automatic rollback restores the `:rollback` tagged images
|
||||
|
||||
### Notifications
|
||||
|
||||
@@ -265,19 +267,32 @@ Deploy status notifications are sent to Slack via `SLACK_WEBHOOK_URL` secret:
|
||||
|
||||
## Rollback
|
||||
|
||||
### Rollback Safety Mechanism
|
||||
|
||||
The deploy pipeline uses **explicit `:rollback` image tags** to guarantee safe rollbacks. Here's how it works:
|
||||
|
||||
1. **Before pulling new images**: The current running images are tagged as `goodgo-api:rollback`, `goodgo-web:rollback`, and `goodgo-ai-services:rollback`
|
||||
2. **After pulling new images**: Services are updated with the new images via rolling restart
|
||||
3. **After smoke tests pass**: The `:rollback` tags are removed and `docker image prune` cleans up old layers
|
||||
4. **If smoke tests fail**: The `:rollback` tagged images are used to restore the previous version
|
||||
|
||||
This ensures that `docker image prune` never deletes the images needed for rollback, because:
|
||||
- Image pruning only happens **after** smoke tests pass
|
||||
- The `:rollback` tags keep the previous images pinned even if pruning were to run accidentally
|
||||
|
||||
### Automatic Rollback (Staging)
|
||||
|
||||
The staging pipeline includes automatic rollback when smoke tests fail:
|
||||
|
||||
1. **Pre-deploy**: Current container image digests are recorded before deployment
|
||||
1. **Pre-deploy**: Current container images are tagged with `:rollback` suffix before new images are pulled
|
||||
2. **Smoke test failure**: If `scripts/smoke-test.sh` exits non-zero, the `rollback-staging` job triggers
|
||||
3. **Rollback execution**: Containers are stopped and restarted with previous images
|
||||
3. **Rollback execution**: Containers are stopped and restarted using the `:rollback` tagged images
|
||||
4. **Verification**: Health check confirms the rollback succeeded
|
||||
5. **Notification**: Slack notification reports the rollback with links to the failed run
|
||||
|
||||
### Automatic Rollback (Production)
|
||||
|
||||
Same mechanism as staging — smoke test failure triggers `rollback-production`.
|
||||
Same mechanism as staging — smoke test failure triggers `rollback-production` using the `:rollback` tagged images.
|
||||
|
||||
### Manual Rollback
|
||||
|
||||
@@ -292,24 +307,30 @@ gh workflow run deploy.yml \
|
||||
-f environment=staging
|
||||
```
|
||||
|
||||
#### Option 2: SSH rollback (emergency)
|
||||
#### Option 2: SSH rollback using :rollback tags (fastest)
|
||||
|
||||
```bash
|
||||
# SSH into the staging/production server
|
||||
ssh deploy@<host>
|
||||
|
||||
cd ~/goodgo
|
||||
|
||||
# Stop the current services
|
||||
docker compose -f docker-compose.prod.yml down api web ai-services
|
||||
# Stop current services
|
||||
docker compose -f docker-compose.prod.yml stop api web ai-services
|
||||
|
||||
# Restart with the previous image layers still cached locally
|
||||
# Verify :rollback images exist
|
||||
docker image inspect goodgo-api:rollback > /dev/null 2>&1 && echo "API rollback available"
|
||||
docker image inspect goodgo-web:rollback > /dev/null 2>&1 && echo "Web rollback available"
|
||||
docker image inspect goodgo-ai-services:rollback > /dev/null 2>&1 && echo "AI rollback available"
|
||||
|
||||
# Restart services (compose picks up cached/rollback images)
|
||||
docker compose -f docker-compose.prod.yml up -d --wait api web ai-services
|
||||
|
||||
# Verify health
|
||||
curl -sf http://localhost:3001/health
|
||||
curl -sf http://localhost:3001/health && echo "Rollback successful"
|
||||
```
|
||||
|
||||
> **Note:** The `:rollback` tags are only available until the next successful deploy cleans them up. If you need to roll back to an older version, use Option 3 below.
|
||||
|
||||
#### Option 3: Pin to a specific image tag
|
||||
|
||||
```bash
|
||||
@@ -325,6 +346,20 @@ docker compose -f docker-compose.prod.yml pull api web ai-services
|
||||
docker compose -f docker-compose.prod.yml up -d --no-deps --wait api web ai-services
|
||||
```
|
||||
|
||||
#### Option 4: Use deploy-production.sh (built-in rollback)
|
||||
|
||||
The manual deploy script (`scripts/deploy-production.sh`) has integrated rollback support:
|
||||
- Automatically tags `:rollback` images before pulling
|
||||
- Runs health checks and smoke tests
|
||||
- Auto-rollbacks using `:rollback` tags if either fails
|
||||
- Only prunes images after smoke tests pass
|
||||
|
||||
```bash
|
||||
ssh ubuntu@185.225.232.65
|
||||
cd ~/goodgo
|
||||
./scripts/deploy-production.sh [image-tag]
|
||||
```
|
||||
|
||||
### Database Rollback
|
||||
|
||||
Prisma does not support automatic down migrations. If a migration must be reverted:
|
||||
|
||||
@@ -52,8 +52,8 @@ log " Compose: ${COMPOSE_FILE}"
|
||||
log "=========================================="
|
||||
echo ""
|
||||
|
||||
# ── Step 1: Record Current State (for rollback) ──────────────────────────────
|
||||
log "Step 1/6: Recording current state for rollback..."
|
||||
# ── Step 1: Record Current State & Tag for Rollback ──────────────────────────
|
||||
log "Step 1/7: Recording current state and tagging rollback images..."
|
||||
PREV_API=$(docker inspect --format='{{.Config.Image}}' goodgo-api 2>/dev/null || echo "none")
|
||||
PREV_WEB=$(docker inspect --format='{{.Config.Image}}' goodgo-web 2>/dev/null || echo "none")
|
||||
PREV_AI=$(docker inspect --format='{{.Config.Image}}' goodgo-ai-services 2>/dev/null || echo "none")
|
||||
@@ -61,14 +61,28 @@ info "Previous API: ${PREV_API}"
|
||||
info "Previous Web: ${PREV_WEB}"
|
||||
info "Previous AI: ${PREV_AI}"
|
||||
|
||||
# Tag current images as :rollback so they survive docker image prune
|
||||
if [ "$PREV_API" != "none" ]; then
|
||||
docker tag "$PREV_API" goodgo-api:rollback 2>/dev/null || warn "Could not tag API rollback image"
|
||||
info "Tagged API rollback: goodgo-api:rollback"
|
||||
fi
|
||||
if [ "$PREV_WEB" != "none" ]; then
|
||||
docker tag "$PREV_WEB" goodgo-web:rollback 2>/dev/null || warn "Could not tag Web rollback image"
|
||||
info "Tagged Web rollback: goodgo-web:rollback"
|
||||
fi
|
||||
if [ "$PREV_AI" != "none" ]; then
|
||||
docker tag "$PREV_AI" goodgo-ai-services:rollback 2>/dev/null || warn "Could not tag AI rollback image"
|
||||
info "Tagged AI rollback: goodgo-ai-services:rollback"
|
||||
fi
|
||||
|
||||
# ── Step 2: Pull New Images ──────────────────────────────────────────────────
|
||||
log "Step 2/6: Pulling new images (tag: ${IMAGE_TAG})..."
|
||||
log "Step 2/7: Pulling new images (tag: ${IMAGE_TAG})..."
|
||||
export IMAGE_TAG
|
||||
docker compose -f "$COMPOSE_FILE" pull api web ai-services
|
||||
log "Images pulled successfully."
|
||||
|
||||
# ── Step 3: Rolling Update ───────────────────────────────────────────────────
|
||||
log "Step 3/6: Rolling update (zero-downtime)..."
|
||||
log "Step 3/7: Rolling update (zero-downtime)..."
|
||||
|
||||
info "Updating API..."
|
||||
docker compose -f "$COMPOSE_FILE" up -d --no-deps --wait api
|
||||
@@ -85,12 +99,12 @@ info "AI Services updated and healthy."
|
||||
log "Rolling update complete."
|
||||
|
||||
# ── Step 4: Database Migrations ──────────────────────────────────────────────
|
||||
log "Step 4/6: Running database migrations..."
|
||||
log "Step 4/7: Running database migrations..."
|
||||
docker compose -f "$COMPOSE_FILE" exec -T api npx prisma migrate deploy
|
||||
log "Migrations complete."
|
||||
|
||||
# ── Step 5: Health Check Verification ────────────────────────────────────────
|
||||
log "Step 5/6: Verifying deployment health..."
|
||||
log "Step 5/7: Verifying deployment health..."
|
||||
HEALTHY=false
|
||||
for i in $(seq 1 "$HEALTH_RETRIES"); do
|
||||
if curl -sf "$HEALTH_URL" > /dev/null 2>&1; then
|
||||
@@ -107,10 +121,25 @@ else
|
||||
err "Health check failed after ${HEALTH_RETRIES} attempts!"
|
||||
|
||||
if $ROLLBACK_ON_FAIL; then
|
||||
warn "Initiating rollback..."
|
||||
warn "Initiating rollback using tagged rollback images..."
|
||||
|
||||
# Rollback: stop current, docker compose will use previously cached images
|
||||
# Rollback: stop current and restart with explicitly tagged rollback images
|
||||
docker compose -f "$COMPOSE_FILE" stop api web ai-services
|
||||
|
||||
# Restore from :rollback tags if available
|
||||
if docker image inspect goodgo-api:rollback > /dev/null 2>&1; then
|
||||
info "Restoring API from goodgo-api:rollback"
|
||||
docker tag goodgo-api:rollback "$PREV_API" 2>/dev/null || true
|
||||
fi
|
||||
if docker image inspect goodgo-web:rollback > /dev/null 2>&1; then
|
||||
info "Restoring Web from goodgo-web:rollback"
|
||||
docker tag goodgo-web:rollback "$PREV_WEB" 2>/dev/null || true
|
||||
fi
|
||||
if docker image inspect goodgo-ai-services:rollback > /dev/null 2>&1; then
|
||||
info "Restoring AI from goodgo-ai-services:rollback"
|
||||
docker tag goodgo-ai-services:rollback "$PREV_AI" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
docker compose -f "$COMPOSE_FILE" up -d --wait api web ai-services
|
||||
|
||||
warn "Rollback complete. Verifying..."
|
||||
@@ -126,8 +155,52 @@ else
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# ── Step 6: Cleanup ──────────────────────────────────────────────────────────
|
||||
log "Step 6/6: Cleaning up old images..."
|
||||
# ── Step 6: Smoke Tests ─────────────────────────────────────────────────────
|
||||
log "Step 6/7: Running smoke tests..."
|
||||
SMOKE_PASSED=false
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
if [ -x "$SCRIPT_DIR/smoke-test.sh" ]; then
|
||||
if "$SCRIPT_DIR/smoke-test.sh" "http://127.0.0.1:3001"; then
|
||||
SMOKE_PASSED=true
|
||||
log "Smoke tests passed!"
|
||||
else
|
||||
err "Smoke tests FAILED!"
|
||||
if $ROLLBACK_ON_FAIL; then
|
||||
warn "Initiating rollback due to smoke test failure..."
|
||||
docker compose -f "$COMPOSE_FILE" stop api web ai-services
|
||||
|
||||
if docker image inspect goodgo-api:rollback > /dev/null 2>&1; then
|
||||
docker tag goodgo-api:rollback "$PREV_API" 2>/dev/null || true
|
||||
fi
|
||||
if docker image inspect goodgo-web:rollback > /dev/null 2>&1; then
|
||||
docker tag goodgo-web:rollback "$PREV_WEB" 2>/dev/null || true
|
||||
fi
|
||||
if docker image inspect goodgo-ai-services:rollback > /dev/null 2>&1; then
|
||||
docker tag goodgo-ai-services:rollback "$PREV_AI" 2>/dev/null || true
|
||||
fi
|
||||
|
||||
docker compose -f "$COMPOSE_FILE" up -d --wait api web ai-services
|
||||
|
||||
warn "Rollback complete. Verifying..."
|
||||
sleep 5
|
||||
if curl -sf "$HEALTH_URL" > /dev/null 2>&1; then
|
||||
warn "Services recovered after rollback."
|
||||
else
|
||||
err "CRITICAL: Services still unhealthy after rollback!"
|
||||
fi
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
warn "Smoke test script not found at $SCRIPT_DIR/smoke-test.sh — skipping."
|
||||
warn "Run manually: ./scripts/smoke-test.sh https://api.goodgo.vn"
|
||||
SMOKE_PASSED=true
|
||||
fi
|
||||
|
||||
# ── Step 7: Cleanup (only after smoke tests pass) ───────────────────────────
|
||||
log "Step 7/7: Cleaning up old images..."
|
||||
# Remove the :rollback tags first (they are no longer needed after a successful deploy)
|
||||
docker rmi goodgo-api:rollback goodgo-web:rollback goodgo-ai-services:rollback 2>/dev/null || true
|
||||
docker image prune -f
|
||||
log "Cleanup complete."
|
||||
|
||||
@@ -147,6 +220,10 @@ info " Web: https://platform.goodgo.vn"
|
||||
info " API: https://api.goodgo.vn"
|
||||
info " Grafana: https://grafana.goodgo.vn"
|
||||
log ""
|
||||
log " Run smoke tests:"
|
||||
info " ./scripts/smoke-test.sh https://api.goodgo.vn"
|
||||
if $SMOKE_PASSED; then
|
||||
log " Smoke tests: PASSED"
|
||||
else
|
||||
log " Run smoke tests against public URL:"
|
||||
info " ./scripts/smoke-test.sh https://api.goodgo.vn"
|
||||
fi
|
||||
log "=========================================="
|
||||
|
||||
Reference in New Issue
Block a user