Forked from pydevops/artifact-registry-cloud-run-demo.sh
Created
July 27, 2023 10:35
-
-
Save mitchallen/9fa95aa7f08614be40c3f850382b5a0e to your computer and use it in GitHub Desktop.
Google Cloud Platform demo of Artifact Registry deployment and Cloud Run app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
export PROJECT_ID=$(gcloud config get-value project) | |
export PROJECT_USER=$(gcloud config get-value core/account) # set current user | |
export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)") | |
export IDNS=${PROJECT_ID}.svc.id.goog # workflow identity domain | |
export GCP_REGION="us-east1" # CHANGEME (OPT) | |
export GCP_ZONE="us-east1-c" # CHANGEME (OPT) | |
export NETWORK_NAME="default" | |
# enable apis | |
gcloud services enable compute.googleapis.com \ | |
artifactregistry.googleapis.com \ | |
cloudbuild.googleapis.com \ | |
storage.googleapis.com \ | |
run.googleapis.com | |
# set defaults | |
gcloud config set compute/region $GCP_REGION | |
gcloud config set compute/zone $GCP_ZONE | |
# create repository | |
export REPO_NAME=team1 | |
gcloud artifacts repositories create $REPO_NAME \ | |
--repository-format=docker \ | |
--location=$GCP_REGION \ | |
--description="Docker repository" | |
# configure auth | |
gcloud auth configure-docker ${GCP_REGION}-docker.pkg.dev | |
############################################################ | |
# push a test image to the repo | |
############################################################ | |
export TEST_IMAGE="us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0" | |
export IMAGE_NAME="hello-app" | |
export TAG_NAME="tag1" | |
docker pull $TEST_IMAGE | |
docker tag $TEST_IMAGE \ | |
${GCP_REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${TAG_NAME} | |
docker push ${GCP_REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME}:${TAG_NAME} | |
########################################################### | |
# Hello demo app with Dockerfile | |
########################################################### | |
export IMAGE_NAME2="hello-go" | |
export TAG_NAME2="tag2" | |
export SERVICE_NAME="hello-service" | |
# create app | |
cat > main.go << EOF | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"os" | |
) | |
func main() { | |
// register hello function to handle all requests | |
mux := http.NewServeMux() | |
mux.HandleFunc("/", hello) | |
// use PORT environment variable, or default to 8080 | |
port := os.Getenv("PORT") | |
if port == "" { | |
port = "8080" | |
} | |
// start the web server on port and accept requests | |
log.Printf("Server listening on port %s", port) | |
log.Fatal(http.ListenAndServe(":"+port, mux)) | |
} | |
// hello responds to the request with a plain-text "Hello, world" message. | |
func hello(w http.ResponseWriter, r *http.Request) { | |
log.Printf("Serving request: %s", r.URL.Path) | |
host, _ := os.Hostname() | |
fmt.Fprintf(w, "Hello, world!\n") | |
fmt.Fprintf(w, "Version: 1.0.0\n") | |
fmt.Fprintf(w, "Hostname: %s\n", host) | |
} | |
EOF | |
# create Dockerfile | |
cat > Dockerfile << EOF | |
FROM golang:1.8-alpine | |
ADD . /go/src/hello-app | |
RUN go install hello-app | |
FROM alpine:latest | |
COPY --from=0 /go/bin/hello-app . | |
ENV PORT 8080 | |
CMD ["./hello-app"] | |
EOF | |
# build / push image to artifact registry (using local Dockerfile) | |
gcloud builds submit --tag ${GCP_REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME2}:${TAG_NAME2} | |
# deploy app to Cloud Run | |
gcloud run deploy $SERVICE_NAME \ | |
--platform managed \ | |
--region $GCP_REGION \ | |
--allow-unauthenticated \ | |
--image ${GCP_REGION}-docker.pkg.dev/${PROJECT_ID}/${REPO_NAME}/${IMAGE_NAME2}:${TAG_NAME2} | |
# confirm service is running | |
gcloud run services list \ | |
--platform managed \ | |
--region $GCP_REGION | |
# test URL | |
export SVC_URL=$(gcloud run services describe $SERVICE_NAME --platform managed --region $GCP_REGION --format="value(status.url)") | |
curl -X GET $SVC_URL | |
# Hello, world! | |
# Version: 1.0.0 | |
# Hostname: localhost |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment