Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit 71725b9

Browse files
authored
MRG: Merge pull request #14 from octue/test/test-new-log-handling
Deploy using GitHub actions
2 parents 686a0df + 3df64ad commit 71725b9

File tree

5 files changed

+163
-34
lines changed

5 files changed

+163
-34
lines changed

.github/workflows/cd.yaml

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: cd
2+
3+
on:
4+
push:
5+
branches: main
6+
7+
workflow_dispatch:
8+
inputs:
9+
debug_enabled:
10+
description: "Enable tmate debug"
11+
type: boolean
12+
default: "false"
13+
14+
jobs:
15+
info:
16+
runs-on: ubuntu-latest
17+
18+
timeout-minutes: 5
19+
20+
permissions:
21+
contents: read
22+
23+
outputs:
24+
branch_tag_kebab: ${{ steps.get-deployment-info.outputs.branch_tag_kebab }}
25+
branch_tag_screaming: ${{ steps.get-deployment-info.outputs.branch_tag_screaming}}
26+
image_latest_artefact: ${{ steps.get-deployment-info.outputs.image_latest_artefact}}
27+
image_latest_tag: ${{ steps.get-deployment-info.outputs.image_latest_tag }}
28+
image_version_artefact: ${{ steps.get-deployment-info.outputs.image_version_artefact}}
29+
image_version_tag: ${{ steps.get-deployment-info.outputs.image_version_tag }}
30+
short_sha: ${{ steps.get-deployment-info.outputs.short_sha }}
31+
gcp_project_name: ${{ steps.get-deployment-info.outputs.gcp_project_name}}
32+
gcp_project_number: ${{ steps.get-deployment-info.outputs.gcp_project_number}}
33+
gcp_region: ${{ steps.get-deployment-info.outputs.gcp_region}}
34+
gcp_resource_affix: ${{ steps.get-deployment-info.outputs.gcp_resource_affix}}
35+
gcp_service_name: ${{ steps.get-deployment-info.outputs.gcp_service_name}}
36+
version: ${{ steps.get-deployment-info.outputs.version }}
37+
version_slug: ${{ steps.get-deployment-info.outputs.version_slug }}
38+
39+
steps:
40+
- name: Checkout
41+
uses: actions/checkout@v3
42+
43+
- name: Install poetry
44+
uses: snok/install-poetry@v1
45+
46+
- name: Get deployment info
47+
id: get-deployment-info
48+
uses: octue/[email protected]
49+
with:
50+
gcp_project_name: octue-sdk-python
51+
gcp_project_number: 437801218871
52+
gcp_region: europe-west1
53+
gcp_resource_affix: octue
54+
gcp_service_name: example-service-cloud-run
55+
gcp_environment: main
56+
57+
build:
58+
runs-on: ubuntu-latest
59+
timeout-minutes: 60
60+
needs: info
61+
permissions:
62+
id-token: write
63+
contents: read
64+
65+
steps:
66+
- name: Checkout
67+
uses: actions/checkout@v3
68+
69+
- name: Set up QEMU
70+
uses: docker/setup-qemu-action@v2
71+
72+
- name: Set up Docker Buildx
73+
uses: docker/setup-buildx-action@v2
74+
75+
- name: Authenticate with GCP Workload Identity
76+
id: auth
77+
uses: google-github-actions/auth@v0
78+
with:
79+
# NOTE: If setting create_credentials_file=true, .dockerignore file must include `gha-creds-*.json` to avoid baking these credentials into build
80+
create_credentials_file: true
81+
workload_identity_provider: projects/${{ needs.info.outputs.gcp_project_number }}/locations/global/workloadIdentityPools/github-actions-pool/providers/github-actions-provider
82+
service_account: github-actions@${{ needs.info.outputs.gcp_project_name }}.iam.gserviceaccount.com
83+
84+
- name: Setup gcloud
85+
uses: "google-github-actions/setup-gcloud@v0"
86+
87+
- name: Configure Docker for GCP
88+
run: gcloud auth configure-docker ${{ needs.info.outputs.gcp_region }}-docker.pkg.dev
89+
90+
- name: Setup tmate session [DEBUG]
91+
if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.debug_enabled == 'true'}}
92+
uses: mxschmitt/action-tmate@v3
93+
94+
- name: Get default Octue Cloud Run Dockerfile
95+
run: wget https://raw.githubusercontent.com/octue/octue-sdk-python/main/octue/cloud/deployment/google/cloud_run/Dockerfile
96+
97+
- name: Build and push container with latest and version tags
98+
# Note: We don't push containers with shas because we'd end up with terabytes in storage (an image for every commit)
99+
uses: docker/build-push-action@v2
100+
with:
101+
context: .
102+
platforms: linux/amd64
103+
file: ./Dockerfile
104+
push: true
105+
cache-from: type=gha
106+
cache-to: type=gha,mode=max
107+
tags: |
108+
${{ needs.info.outputs.image_version_artefact}}
109+
${{ needs.info.outputs.image_latest_artefact}}
110+
111+
- name: Deploy to Cloud Run service
112+
id: deploy-service
113+
uses: google-github-actions/deploy-cloudrun@v0
114+
with:
115+
env_vars: |
116+
OCTUE_SERVICE_NAMESPACE=${{ needs.info.outputs.gcp_resource_affix }}
117+
OCTUE_SERVICE_NAME=${{ needs.info.outputs.gcp_service_name }}
118+
OCTUE_SERVICE_REVISION_TAG=${{ needs.info.outputs.version_slug }}
119+
COMPUTE_PROVIDER=GOOGLE_CLOUD_RUN
120+
service: ${{ needs.info.outputs.gcp_resource_affix }}-${{ needs.info.outputs.gcp_service_name }}
121+
image: ${{ needs.info.outputs.image_version_artefact }}
122+
region: ${{ needs.info.outputs.gcp_region }}
123+
tag: v${{ needs.info.outputs.version_slug }}
124+
flags: "--allow-unauthenticated"
125+
126+
- name: Show deployed service URL
127+
run: echo "${{ steps.deploy-service.outputs.url }}"
128+
129+
- name: Create topic and subscription
130+
uses: octue/[email protected]
131+
with:
132+
project_name: ${{ needs.info.outputs.gcp_project_name }}
133+
service_namespace: ${{ needs.info.outputs.gcp_resource_affix }}
134+
service_name: ${{ needs.info.outputs.gcp_service_name }}
135+
service_revision_tag: ${{ needs.info.outputs.version_slug }}
136+
push_endpoint: ${{ steps.deploy-service.outputs.url }}

octue.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
name: example-service-cloud-run
44
app_source_path: example_service_cloud_run
55
project_name: octue-sdk-python
6-
region: europe-west2
6+
region: europe-west1
77
repository_owner: octue
88
repository_name: example-service-cloud-run
99
minimum_instances: 0

poetry.lock

Lines changed: 14 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "example-service-cloud-run"
3-
version = "0.2.0"
3+
version = "0.2.1"
44
description = "An example Octue service for deploying to Google Cloud Run."
55
authors = ["Marcus Lugg <[email protected]>"]
66
license = "MIT"
@@ -9,7 +9,7 @@ packages = [{include = "example_service_cloud_run"}]
99

1010
[tool.poetry.dependencies]
1111
python = "^3.9"
12-
octue = "0.43.2"
12+
octue = "0.45.0"
1313

1414
[tool.poetry.group.dev.dependencies]
1515
gcp-storage-emulator = "2022.4.13"

tests/test_app.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
REPOSITORY_ROOT = os.path.dirname(os.path.dirname(__file__))
1111
TWINE_PATH = os.path.join(REPOSITORY_ROOT, "twine.json")
12-
TEST_BUCKET_NAME = "octue-test-bucket"
12+
TEST_BUCKET_NAME = "octue-sdk-python-test-bucket"
1313

1414

1515
class TestApp(unittest.TestCase):
@@ -24,10 +24,18 @@ def test_app(self):
2424
runner = Runner(
2525
app_src=os.path.join(REPOSITORY_ROOT, "example_service_cloud_run"),
2626
twine=TWINE_PATH,
27+
# Output location is taken from the app configuration automatically in a service but not if `Runner` is used
28+
# by itself.
29+
output_location="gs://octue-sdk-python-test-bucket/example_output_datasets",
2730
)
2831

2932
with patch("google.cloud.storage.blob.Blob.generate_signed_url", mock_generate_signed_url):
30-
analysis = runner.run(input_values={"n_iterations": 3})
33+
with self.assertLogs() as logging_context:
34+
analysis = runner.run(input_values={"n_iterations": 3})
35+
36+
# Check log messages from app and submodules are emitted.
37+
self.assertEqual(logging_context.records[0].name, "app")
38+
self.assertEqual(logging_context.records[1].name, "example_service_cloud_run.submodule")
3139

3240
# Check the output values.
3341
self.assertEqual(analysis.output_values, [1, 2, 3, 4, 5])

0 commit comments

Comments
 (0)