Skip to content

Commit

Permalink
Merge pull request #11 from barclayd/deploy/working-gcp
Browse files Browse the repository at this point in the history
  • Loading branch information
barclayd authored Nov 3, 2024
2 parents cba4942 + bc6036c commit 9e59ec5
Show file tree
Hide file tree
Showing 18 changed files with 193 additions and 50 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Default platform for all builds
DOCKER_DEFAULT_PLATFORM=linux/amd64
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DEFAULT_INPUT_FILE_PATH=./test/data/input.txt
OUTPUT_FILE_PATH=./test/data/output.txt
DEFAULT_OUTPUT_FILE_PATH=./output.txt
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN bun install
COPY . .

EXPOSE 3000
CMD ["bun", "run", "start"]
CMD ["bun", "start"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ To run the Mars Rover simulation in production mode:
bun start
```

This will output the final positions of the rovers to the file specified in the `OUTPUT_FILE_PATH` environment variable, upon success.
This will output the final positions of the rovers to the file specified in the `DEFAULT_OUTPUT_FILE_PATH` environment variable, upon success.

The `OUTPUT_FILE_PATH` is deleted if it exists before the application starts to ensure an accurate output for every run.
The `DEFAULT_OUTPUT_FILE_PATH` is deleted if it exists before the application starts to ensure an accurate output for every run.

### Development Mode

Expand Down
20 changes: 14 additions & 6 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
steps:
# Build the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/mars-rover-439913/mars-rover:$COMMIT_SHA', '.']

args: [
'build',
'-t', 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:$COMMIT_SHA',
'-t', 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:latest',
'.'
]

# Push the container image
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/mars-rover-439913/mars-rover:$COMMIT_SHA']

args: ['push', 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:$COMMIT_SHA']
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:latest']

# Deploy to Cloud Run
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'run'
- 'deploy'
- 'mars-rover'
- '--image'
- 'gcr.io/mars-rover-439913/mars-rover:$COMMIT_SHA'
- 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:$COMMIT_SHA'
- '--region'
- 'europe-west2'
- '--platform'
- 'managed'

images:
- 'gcr.io/mars-rover-439913/mars-rover:$COMMIT_SHA'
- 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:$COMMIT_SHA'
- 'europe-west2-docker.pkg.dev/$PROJECT_ID/mars-rover/mars-rover:latest'
22 changes: 22 additions & 0 deletions deploy/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
81 changes: 81 additions & 0 deletions deploy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
provider "google" {
project = var.project_id
region = var.region
}

# Enable required APIs
resource "google_project_service" "cloud_build_api" {
service = "cloudbuild.googleapis.com"
disable_on_destroy = false
}

resource "google_project_service" "cloud_run_api" {
service = "run.googleapis.com"
disable_on_destroy = false
}

resource "google_project_service" "artifact_registry_api" {
service = "artifactregistry.googleapis.com"
disable_on_destroy = false
}

# Artifact Registry
resource "google_artifact_registry_repository" "docker_repo" {
provider = google
location = var.region
repository_id = var.docker_repository_id
format = "DOCKER"
description = "Docker repository for Mars Rover"
depends_on = [google_project_service.artifact_registry_api]
}

# Cloud Build Trigger
resource "google_cloudbuild_trigger" "filename-trigger" {
name = "Github"
location = var.region

github {
owner = var.github_owner
name = var.github_repository
push {
branch = "^main$"
}
}

filename = "cloudbuild.yaml"

service_account = "projects/${var.project_id}/serviceAccounts/[email protected]"

depends_on = [google_project_service.cloud_build_api]
}

# Cloud Run Service
resource "google_cloud_run_service" "cloud_run_service" {
name = var.github_repository
location = var.region

template {
spec {
containers {
image = "${var.region}-docker.pkg.dev/${var.project_id}/${var.docker_repository_id}/${var.github_repository}:latest"
ports {
container_port = var.port
}
}
}
}

depends_on = [
google_artifact_registry_repository.docker_repo,
google_project_service.cloud_run_api
]
}

# Allow unauthenticated access
resource "google_cloud_run_service_iam_member" "noauth" {
location = google_cloud_run_service.cloud_run_service.location
project = var.project_id
service = google_cloud_run_service.cloud_run_service.name
role = "roles/run.invoker"
member = "allUsers"
}
14 changes: 14 additions & 0 deletions deploy/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
output "cloud_build_url" {
description = "URL to view Cloud Build history"
value = "https://console.cloud.google.com/cloud-build/builds;region=global?project=${var.project_id}"
}

output "cloud_run_url" {
description = "URL to view Cloud Run history"
value = "https://console.cloud.google.com/run/detail/${var.project_id}/services/mars-rover?project=${var.project_id}"
}

output "cloud_run_service_url" {
description = "URL to view Cloud Run service"
value = "https://mars-rover.${var.region}.run.app"
}
31 changes: 31 additions & 0 deletions deploy/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
variable "project_id" {
description = "GCP Project ID"
type = string
}

variable "region" {
description = "GCP Region"
type = string
default = "europe-west1"
}

variable "docker_repository_id" {
description = "Docker Repository ID"
type = string
}

variable "github_owner" {
description = "GitHub Owner"
type = string
}

variable "github_repository" {
description = "GitHub Repository"
type = string
}

variable "port" {
description = "Port"
type = number
default = 3000
}
7 changes: 0 additions & 7 deletions prod/main.tf

This file was deleted.

19 changes: 0 additions & 19 deletions prod/output.tf

This file was deleted.

4 changes: 0 additions & 4 deletions prod/variables.tf

This file was deleted.

21 changes: 16 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { simulateMission } from './mars';
import { measurePerformance } from './utils';
import { getCommandLineArgs } from './utils/bun';
import { getCommandLineArgs, readFile } from './utils/bun';

const { filePath, isDev } = getCommandLineArgs();

measurePerformance(
() => simulateMission(filePath ?? Bun.env.DEFAULT_INPUT_FILE_PATH, isDev),
'Mission Simulation Complete 🚀',
);
Bun.serve({
async fetch() {
await measurePerformance(
async () => await simulateMission(filePath ?? Bun.env.DEFAULT_INPUT_FILE_PATH, isDev),
'Mission Simulation Complete 🚀',
);
return new Response(await readFile(Bun.env.DEFAULT_OUTPUT_FILE_PATH ?? './output.txt'), {
headers: {
'Content-Type': 'text/plain',
},
});
},
});


2 changes: 1 addition & 1 deletion src/utils/bun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const readFile = async (filePath: string) => {
};

export const writeOutputToFile = async (output: string) => {
const path = Bun.env.OUTPUT_FILE_PATH;
const path = Bun.env.DEFAULT_OUTPUT_FILE_PATH;

if (!path) {
throw new Error('Output file path is required');
Expand Down
4 changes: 2 additions & 2 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export const formatRoverPositions = (
.join('\n\n');
};

export const measurePerformance = (fn: () => void, label = 'Operation') => {
export const measurePerformance = async (fn: () => Promise<void>, label = 'Operation') => {
console.time(label);
const result = fn();
const result = await fn();
console.timeEnd(label);
return result;
};
6 changes: 5 additions & 1 deletion test/integration/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { beforeEach, expect, mock, test } from 'bun:test';
import { beforeAll, beforeEach, expect, mock, test } from 'bun:test';
import { simulateMission } from '../../src/mars';
import { readFile, removePreviousOutputFile } from '../utils';

beforeAll(() => {
process.env.DEFAULT_OUTPUT_FILE_PATH = './test/data/output.txt';
});

beforeEach(async () => {
console.error = mock(() => {});
await removePreviousOutputFile();
Expand Down
2 changes: 1 addition & 1 deletion test/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const readFile = async (filePath: string) => {
};

export const removePreviousOutputFile = async () => {
const path = Bun.env.OUTPUT_FILE_PATH;
const path = Bun.env.DEFAULT_OUTPUT_FILE_PATH;

if (!path) {
throw new Error('File path is required');
Expand Down

0 comments on commit 9e59ec5

Please sign in to comment.