From 9241a64efad175c384299a51c1f8521def7caeb5 Mon Sep 17 00:00:00 2001 From: Kevin Yang <108375176+kevya-google@users.noreply.github.com> Date: Mon, 11 Sep 2023 09:51:56 -0700 Subject: [PATCH] Reorganize rest_server template with new layout and subfolder convention (#180) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Addresses https://github.com/abcxyz/abc/issues/173, also reformats subdirectory contents in accordance to https://github.com/abcxyz/abc/issues/166. Template structure: ``` $ tree -all . ├── contents │ ├── go.mod │ ├── go.sum │ ├── main.go │ ├── main_test.go │ └── README.md ├── deployments │ ├── config │ │ └── autopush-rest-server.env │ ├── contents │ │ └── Dockerfile │ ├── README.md │ ├── spec.yaml │ └── workflows │ ├── ci-rest-server.yaml │ └── deploy-rest-server.yaml ├── README.md └── spec.yaml ``` Rendered template structure: ``` $ tree -all . ├── Dockerfile ├── .github │ ├── config │ │ └── autopush-rest-server.env │ └── workflows │ ├── ci-rest-server.yaml │ └── deploy-rest-server.yaml ├── go.mod ├── go.sum ├── main.go ├── main_test.go └── README.md ``` --- t/rest_server/README.md | 41 ++--------- t/rest_server/contents/README.md | 17 +++++ t/rest_server/{ => contents}/go.mod | 0 t/rest_server/{ => contents}/go.sum | 0 t/rest_server/{ => contents}/main.go | 0 t/rest_server/{ => contents}/main_test.go | 0 t/rest_server/deployments/README.md | 12 ++++ .../config/autopush-rest-server.env} | 0 .../{ => deployments/contents}/Dockerfile | 0 t/rest_server/deployments/spec.yaml | 68 +++++++++++++++++++ .../workflows/ci-rest-server.yaml} | 2 +- .../workflows/deploy-rest-server.yaml} | 8 +-- t/rest_server/spec.yaml | 39 +---------- 13 files changed, 112 insertions(+), 75 deletions(-) create mode 100644 t/rest_server/contents/README.md rename t/rest_server/{ => contents}/go.mod (100%) rename t/rest_server/{ => contents}/go.sum (100%) rename t/rest_server/{ => contents}/main.go (100%) rename t/rest_server/{ => contents}/main_test.go (100%) create mode 100644 t/rest_server/deployments/README.md rename t/rest_server/{.github/config/autopush.env => deployments/config/autopush-rest-server.env} (100%) rename t/rest_server/{ => deployments/contents}/Dockerfile (100%) create mode 100644 t/rest_server/deployments/spec.yaml rename t/rest_server/{.github/workflows/ci.yaml => deployments/workflows/ci-rest-server.yaml} (97%) rename t/rest_server/{.github/workflows/deploy.yaml => deployments/workflows/deploy-rest-server.yaml} (97%) diff --git a/t/rest_server/README.md b/t/rest_server/README.md index f91ffa19..28dc0260 100644 --- a/t/rest_server/README.md +++ b/t/rest_server/README.md @@ -1,43 +1,16 @@ -# Example: REST server +# Template: REST server -Simple HTTP/JSON REST server implemented in Go, using go-chi for HTTP routing. +Template for a simple HTTP/JSON REST server implemented in Go, using go-chi for HTTP routing. -To run this, cd to the root of this git repo, then run these steps: +How to render this template: -1. cd into an empty directory +1. [Install the abc binary](https://github.com/abcxyz/abc#installation). - ```shell - $ mkdir ~/template_tmp - $ cd ~/template_tmp - ``` - -1. Install the `abc` binary - - ```shell - $ go install github.com/abcxyz/abc/cmd/abc@latest - $ abc --help - ``` - - This only works if you have go installed (https://go.dev/doc/install) and have the Go binary directory in your $PATH (try PATH=$PATH:~/go/bin). - -1. Execute the template defined in the `t` directory. -This will output a file named `main.go` in your working directory containing -the transformed program. +1. Render via: ```shell $ abc templates render github.com/abcxyz/abc.git//t/rest_server - ``` - -1. Run the transformed program: - ```shell - $ go run . - [yyyy/mm/dd hh:mm:ss] starting server on 8080 - ``` +1. Follow the steps in the rendered README.md to run the server. -1. In a separate shell, run: - - ```shell - $ curl localhost:8080 - {"message":"hello world"} - ``` \ No newline at end of file +1. Optionally, render the CI/CD workflows as well (see subfolder README for more details). diff --git a/t/rest_server/contents/README.md b/t/rest_server/contents/README.md new file mode 100644 index 00000000..79aeed38 --- /dev/null +++ b/t/rest_server/contents/README.md @@ -0,0 +1,17 @@ +# Simple REST server + +This is a simple HTTP/JSON REST server implemented in Go, using go-chi for HTTP routing. + +How to run this server: + +1. Run the server: + + ```shell + $ go run . + [yyyy/mm/dd hh:mm:ss] starting server on 8080 + ``` +1. In a separate shell, run: + ```shell + $ curl localhost:8080 + {"message":"hello world"} + ``` diff --git a/t/rest_server/go.mod b/t/rest_server/contents/go.mod similarity index 100% rename from t/rest_server/go.mod rename to t/rest_server/contents/go.mod diff --git a/t/rest_server/go.sum b/t/rest_server/contents/go.sum similarity index 100% rename from t/rest_server/go.sum rename to t/rest_server/contents/go.sum diff --git a/t/rest_server/main.go b/t/rest_server/contents/main.go similarity index 100% rename from t/rest_server/main.go rename to t/rest_server/contents/main.go diff --git a/t/rest_server/main_test.go b/t/rest_server/contents/main_test.go similarity index 100% rename from t/rest_server/main_test.go rename to t/rest_server/contents/main_test.go diff --git a/t/rest_server/deployments/README.md b/t/rest_server/deployments/README.md new file mode 100644 index 00000000..ec2c59c5 --- /dev/null +++ b/t/rest_server/deployments/README.md @@ -0,0 +1,12 @@ +# REST server workflows template + +This directory contains the CI/CD GitHub Action workflows for the REST server template. + + $ abc templates render -input="automation_service_account=[automation_service_account]" \ + -input="wif_provider=[wif_provider]" \ + -input="ar_repository=[ar_repository]" \ + -input="ar_location=[ar_location]" \ + -input="cr_service=[cr_service]" \ + -input="region=[region]" \ + -input="project_id=[project_id]" \ + github.com/abcxyz/abc.git//t/rest_server/workflows diff --git a/t/rest_server/.github/config/autopush.env b/t/rest_server/deployments/config/autopush-rest-server.env similarity index 100% rename from t/rest_server/.github/config/autopush.env rename to t/rest_server/deployments/config/autopush-rest-server.env diff --git a/t/rest_server/Dockerfile b/t/rest_server/deployments/contents/Dockerfile similarity index 100% rename from t/rest_server/Dockerfile rename to t/rest_server/deployments/contents/Dockerfile diff --git a/t/rest_server/deployments/spec.yaml b/t/rest_server/deployments/spec.yaml new file mode 100644 index 00000000..caf56926 --- /dev/null +++ b/t/rest_server/deployments/spec.yaml @@ -0,0 +1,68 @@ +# Copyright 2023 The Authors (see AUTHORS file) +# +# 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 +# +# http://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: 'cli.abcxyz.dev/v1alpha1' +kind: 'Template' +desc: 'REST server workflows template' +inputs: + - name: 'automation_service_account' + desc: + 'Automation Service Account (ex: + [account_name]@[project_id].iam.gserviceaccount.com)' + rules: + - rule: 'gcp_matches_service_account(automation_service_account)' + message: 'Service account should match format: + [account_name]@[project_id].iam.gserviceaccount.com)' + - name: 'wif_provider' + desc: + 'Workload Identity Federation Provider (ex: + projects/[project_id]/locations/global/workloadIdentityPools/[WIF_pool_name]/providers/[provider_name])' + - name: 'ar_repository' + desc: 'Artifact Registry Repository (ex: ci-images)' + - name: 'ar_location' + desc: 'Artifact Registry Location (ex: us-docker.pkg.dev)' + - name: 'cr_service' + desc: 'Cloud Run Service (ex: hello-jupiter-api)' + - name: 'region' + desc: 'Cloud Run Region (ex: us-west1)' + - name: 'project_id' + desc: 'GCP Project ID' + rules: + - rule: 'gcp_matches_project_id(project_id)' + message: 'Must be a GCP project ID' +steps: + - desc: 'Include workflow, deployment files' + action: 'include' + params: + paths: ['workflows', 'config', 'contents'] + as: ['.github/workflows', '.github/config', '.'] + - desc: 'Replace sentinel values with real input values' + action: 'string_replace' + params: + paths: ['.'] + replacements: + - to_replace: 'AUTOMATION_SERVICE_ACCOUNT=[automation_service_account]' + with: 'AUTOMATION_SERVICE_ACCOUNT={{.automation_service_account}}' + - to_replace: 'WIF_PROVIDER=[wif_provider]' + with: 'WIF_PROVIDER={{.wif_provider}}' + - to_replace: 'AR_REPOSITORY=[ar_repository]' + with: 'AR_REPOSITORY={{.ar_repository}}' + - to_replace: 'AR_LOCATION=[ar_location]' + with: 'AR_LOCATION={{.ar_location}}' + - to_replace: 'CR_SERVICE=[cr_service]' + with: 'CR_SERVICE={{.cr_service}}' + - to_replace: 'REGION=[region]' + with: 'REGION={{.region}}' + - to_replace: 'PROJECT_ID=[project_id]' + with: 'PROJECT_ID={{.project_id}}' diff --git a/t/rest_server/.github/workflows/ci.yaml b/t/rest_server/deployments/workflows/ci-rest-server.yaml similarity index 97% rename from t/rest_server/.github/workflows/ci.yaml rename to t/rest_server/deployments/workflows/ci-rest-server.yaml index 49ad6e88..6103be66 100644 --- a/t/rest_server/.github/workflows/ci.yaml +++ b/t/rest_server/deployments/workflows/ci-rest-server.yaml @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: 'ci' +name: 'ci rest server' on: push: diff --git a/t/rest_server/.github/workflows/deploy.yaml b/t/rest_server/deployments/workflows/deploy-rest-server.yaml similarity index 97% rename from t/rest_server/.github/workflows/deploy.yaml rename to t/rest_server/deployments/workflows/deploy-rest-server.yaml index a2d741ef..50cab1a0 100644 --- a/t/rest_server/.github/workflows/deploy.yaml +++ b/t/rest_server/deployments/workflows/deploy-rest-server.yaml @@ -12,7 +12,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -name: 'deploy' +name: 'deploy rest server' on: workflow_dispatch: inputs: @@ -32,7 +32,7 @@ concurrency: group: '${{ github.workflow }}-${{ github.head_ref || github.ref }}' cancel-in-progress: true # Define all env variables in the bootstrap files under -# .github/config/{{ environment }}.env +# .github/config/{{ environment }}-rest-server.env # More environments are expected to be added in the future. jobs: image-release: @@ -47,7 +47,7 @@ jobs: - name: 'Load variables from .env file' uses: 'abcxyz/pkg/.github/actions/override-workflow-variables@ca5c712fad5863b8f8fd40b2a8e220f8b41bde01' # ratchet:abcxyz/pkg/.github/actions/override-workflow-variables@main with: - env_file: '.github/config/${{inputs.deployment_environment}}.env' + env_file: '.github/config/${{inputs.deployment_environment}}-rest-server.env' - id: 'auth' name: 'Authenticate to Google Cloud' uses: 'google-github-actions/auth@e8df18b60c5dd38ba618c121b779307266153fbf' # ratchet:google-github-actions/auth@v1 @@ -89,7 +89,7 @@ jobs: - name: 'Load variables from .env file' uses: 'abcxyz/pkg/.github/actions/override-workflow-variables@ca5c712fad5863b8f8fd40b2a8e220f8b41bde01' # ratchet:abcxyz/pkg/.github/actions/override-workflow-variables@main with: - env_file: '.github/config/${{inputs.deployment_environment}}.env' + env_file: '.github/config/${{inputs.deployment_environment}}-rest-server.env' - name: 'Google Auth' id: 'auth' uses: 'google-github-actions/auth@e8df18b60c5dd38ba618c121b779307266153fbf' # ratchet:google-github-actions/auth@v1 diff --git a/t/rest_server/spec.yaml b/t/rest_server/spec.yaml index db9004ff..68bfa2a0 100644 --- a/t/rest_server/spec.yaml +++ b/t/rest_server/spec.yaml @@ -14,43 +14,10 @@ apiVersion: 'cli.abcxyz.dev/v1alpha1' kind: 'Template' -desc: 'A template for a simple HTTP/JSON REST server.' -inputs: - - name: 'automation_service_account' - desc: 'Automation Service Account (ex: [account_name]@[project_id].iam.gserviceaccount.com)' - - name: 'wif_provider' - desc: 'Workload Identity Federation Provider (ex: projects/[project_id]/locations/global/workloadIdentityPools/[WIF_pool_name]/providers/[provider_name])' - - name: 'ar_repository' - desc: 'Artifact Registry Repository (ex: ci-images)' - - name: 'ar_location' - desc: 'Artifact Registry Location (ex: us-docker.pkg.dev)' - - name: 'cr_service' - desc: 'Cloud Run Service (ex: hello-jupiter-api)' - - name: 'region' - desc: 'Cloud Run Region (ex: us-west1)' - - name: 'project_id' - desc: 'GCP Project ID' +desc: 'A template for a simple HTTP/JSON REST server' steps: - desc: 'Include some files and directories' action: 'include' params: - paths: ['.'] - - desc: 'Replace sentinel values with real input values' - action: 'string_replace' - params: - paths: ['.github/config'] - replacements: - - to_replace: 'AUTOMATION_SERVICE_ACCOUNT=[automation_service_account]' - with: 'AUTOMATION_SERVICE_ACCOUNT={{.automation_service_account}}' - - to_replace: 'WIF_PROVIDER=[wif_provider]' - with: 'WIF_PROVIDER={{.wif_provider}}' - - to_replace: 'AR_REPOSITORY=[ar_repository]' - with: 'AR_REPOSITORY={{.ar_repository}}' - - to_replace: 'AR_LOCATION=[ar_location]' - with: 'AR_LOCATION={{.ar_location}}' - - to_replace: 'CR_SERVICE=[cr_service]' - with: 'CR_SERVICE={{.cr_service}}' - - to_replace: 'REGION=[region]' - with: 'REGION={{.region}}' - - to_replace: 'PROJECT_ID=[project_id]' - with: 'PROJECT_ID={{.project_id}}' + paths: ['contents'] + as: ['.']