diff --git a/Dockerfile b/Dockerfile index b5b30924..f5c5c8ba 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,6 @@ -FROM alpine -COPY helloworld.sh / -CMD ["/helloworld.sh"] +FROM python:3.7-slim +RUN pip install flask +WORKDIR /app +COPY app.py /app/app.py +ENTRYPOINT ["python"] +CMD ["/app/app.py"] diff --git a/app.py b/app.py new file mode 100644 index 00000000..7993334e --- /dev/null +++ b/app.py @@ -0,0 +1,10 @@ +from flask import Flask +app = Flask('hello-cloudbuild') + +@app.route('/') +def hello(): + return "Hello World!!\n" + +if __name__ == '__main__': + app.run(host = '0.0.0.0', port = 8080) +Create a container image with Cloud B diff --git a/cloudbuild-delivery.yaml b/cloudbuild-delivery.yaml new file mode 100644 index 00000000..08418ebb --- /dev/null +++ b/cloudbuild-delivery.yaml @@ -0,0 +1,13 @@ +steps: +# This step deploys the new version of our container image +# in the hello-cloudbuild Kubernetes Engine cluster. +- name: 'gcr.io/cloud-builders/kubectl' + id: Deploy + args: + - 'apply' + - '-f' + - 'kubernetes.yaml' + env: + - 'CLOUDSDK_COMPUTE_ZONE=us-central1-b' + - 'CLOUDSDK_CONTAINER_CLUSTER=hello-cloudbuild' + - 'CLOUDSDK_CORE_PROJECT=o-epm-gcp-by-meetup1-ml-t1iylu' diff --git a/cloudbuild.yaml b/cloudbuild.yaml new file mode 100644 index 00000000..aeef17af --- /dev/null +++ b/cloudbuild.yaml @@ -0,0 +1,18 @@ +steps: +# This step builds the container image. +- name: 'gcr.io/cloud-builders/docker' + id: Build + args: + - 'build' + - '-t' + - 'gcr.io/$PROJECT_ID/hello-cloudbuild:$SHORT_SHA' + - '.' + +# This step pushes the image to Container Registry +# The PROJECT_ID and SHORT_SHA variables are automatically +# replaced by Cloud Build. +- name: 'gcr.io/cloud-builders/docker' + id: Push + args: + - 'push' + - 'gcr.io/$PROJECT_ID/hello-cloudbuild:$SHORT_SHA' diff --git a/helloworld.sh b/helloworld.sh deleted file mode 100644 index 79a32fd6..00000000 --- a/helloworld.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -echo "Hello, world!" diff --git a/kubernetes.yaml b/kubernetes.yaml new file mode 100644 index 00000000..a463e274 --- /dev/null +++ b/kubernetes.yaml @@ -0,0 +1,48 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +apiVersion: apps/v1 +kind: Deployment +metadata: + name: hello-cloudbuild + labels: + app: hello-cloudbuild +spec: + replicas: 1 + selector: + matchLabels: + app: hello-cloudbuild + template: + metadata: + labels: + app: hello-cloudbuild + spec: + containers: + - name: hello-cloudbuild + image: gcr.io/GOOGLE_CLOUD_PROJECT/hello-cloudbuild:COMMIT_SHA + ports: + - containerPort: 8080 +--- +kind: Service +apiVersion: v1 +metadata: + name: hello-cloudbuild +spec: + selector: + app: hello-cloudbuild + ports: + - protocol: TCP + port: 80 + targetPort: 8080 + type: LoadBalancer diff --git a/test_app.py b/test_app.py new file mode 100644 index 00000000..fea2ee57 --- /dev/null +++ b/test_app.py @@ -0,0 +1,24 @@ +# Copyright 2018 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# https://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +from app import hello + +class TestHelloApp(unittest.TestCase): + + def test_hello(self): + self.assertEqual(hello(), "Hello World!!\n") + +if __name__ == '__main__': + unittest.main()