Skip to content

add webapp & container registry/builder sample #215

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apps/webapp-registry/.env.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
IMAGE_NAME=go-sample
IMAGE_TAG=latest

AZURE_BASE_NAME=go-webapp-tester
AZURE_DEFAULT_LOCATION=westus2

# required for continous container build setup
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove GH_TOKEN from environment

GH_TOKEN=
2 changes: 2 additions & 0 deletions apps/webapp-registry/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
out/
.env
13 changes: 13 additions & 0 deletions apps/webapp-registry/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM golang:1.10.3-stretch
ARG PACKAGE=github.com/Azure-Samples/azure-sdk-for-go-samples/apps/basic_web_app

RUN mkdir /app # for built artifacts
RUN mkdir -p $GOPATH/src/${PACKAGE}
WORKDIR $GOPATH/src/${PACKAGE}

# better for build cache to specify necessary files
ADD ["Gopkg.*","main.go", "./"]
RUN go get -u -v github.com/golang/dep/cmd/dep && dep ensure -v
RUN go build -v -o /app/server .

CMD ["/app/server"]
20 changes: 20 additions & 0 deletions apps/webapp-registry/Gopkg.lock

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

7 changes: 7 additions & 0 deletions apps/webapp-registry/Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[prune]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can safely be removed. Project has no external dependencies that need to be locked.

go-tests = true
unused-packages = true

[[constraint]]
branch = "master"
name = "golang.org/x/net"
20 changes: 20 additions & 0 deletions apps/webapp-registry/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Copyright Microsoft Corporation

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
USE OR OTHER DEALINGS IN THE SOFTWARE.
32 changes: 32 additions & 0 deletions apps/webapp-registry/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
PACKAGE = github.com/Azure-Samples/azure-sdk-for-go-samples/apps/basic_web_app
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make is never used. Can be deleted.


REGISTRY = local
IMAGE = go-sample
TAG = latest

PORT = 8080
IMAGE_URI = $(REGISTRY)/$(IMAGE):$(TAG)
C = $(IMAGE)-tester
HOST = localhost:$(PORT)

binary:
mkdir -p out
go build -o out/server .
./out/server &
curl http://$(HOST)/?name=josh && echo ""
pkill --euid $(USER) --newest --exact server

container:
docker build -t $(IMAGE_URI) .
# docker push $(IMAGE_URI)
docker run -d --rm \
--name $C \
--publish "$(PORT):8080" \
$(IMAGE_URI)
curl "http://$(HOST)/?name=josh" && echo ""
docker container logs $C
docker container stop $C
echo "start a new container with"
echo " \`docker run [-d|-it] -p $(PORT):8080 $(IMAGE_URI)\`"

.PHONY: binary container
64 changes: 64 additions & 0 deletions apps/webapp-registry/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Web App

Continuously build and deploy a Go web app using Azure App Service and Azure
Container Registry.

This package includes a basic Go web app and scripts to set up infrastructure
for it in Azure.

Requires [Azure CLI][].

## Try It!

Set configuration in a local .env file in the package root by copying
`.env.tpl` to `.env`. Change `AZURE_BASE_NAME` to something relatively unique
to you; for example you might include your name. Then run ./[setup.sh][] to
build and deploy your container to Container Registry and App Service.

If run within the Azure Go SDK samples repo, this will carry out a one-off
build and push to your registry, which will trigger refresh of the App Service
Web App. If you stick with the script defaults, you can visit your app at
`https://${AZURE_BASE_NAME}-webapp.azurewebsites.net/`.

### Continuous Build and Deploy

Follow these steps to set up continuous build and deploy:

1. Copy the contents of this package to your own fresh git repo and push it to GitHub.
2. Specify an image name in env var `IMAGE_NAME` (e.g. in `.env`) that matches
your GitHub 'org/repo' structure.
3. Run `./setup.sh`. It will arrange continuous build and deploy for you from
the specified repo/image name.

**NOTE**: Container Registry Build requires a [GitHub personal access
token](https://github.com/settings/tokens); you need to get one from the linked
page and set it in a local environment variable `GH_TOKEN`, e.g. `export
GH_TOKEN=mylongtokenstring`. You can also add it to your local `.env` file for
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Security risk; don't suggest adding to env.

persistence.

To test continuous integration, now make a change and `git push` it to your
repo. The Container Registry build task should detect the change, rebuild your
container, and notify App Service; which should then refresh and reload your
container image and app.

## More Details

[setup.sh][] ensures an Azure resource group, container registry, app service
plan, and container-based web app are provisioned and connected in the
subscription currently logged in to [Azure CLI][].

It uses the following environment variables to choose names:

* IMAGE\_NAME: Name of container image (aka "repo").
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMAGE_NAME is not a natural choice here; maybe REPO_NAME, and spell out that the repo name is used for the image name?

* IMAGE\_TAG: Tag for container image.
* AZURE\_BASE\_NAME: Prefix for Azure resources.
* AZURE\_DEFAULT\_LOCATION: Location for Azure resources.

These names can be specified in a .env file in the root of the package. If a
`.env` file isn't found, `.env.tpl` is copied to `.env` and used.

Explicit parameters can also be passed, see comments at beginning of
[setup.sh][] for details.

[Azure CLI]: https://github.com/Azure/azure-cli
[setup.sh]: ./setup.sh
33 changes: 33 additions & 0 deletions apps/webapp-registry/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shared between here and #216. It may not be a bad idea to merge these PRs together, and have setup.sh or a PowerShell script take an argument; deploy-X where X is some type of app deployment. They all use the same environment variables, and individual deploys could be broken out into a scripts directory and called from the setup.sh (or sourced into it.)


import (
"fmt"
"golang.org/x/net/html"
"log"
"net/http"
"os"
)

func main() {
// find port selected by service, default to 8080
var port string
var found bool
port, found = os.LookupEnv("PORT")
if found == true {
// prepend a colon
port = fmt.Sprintf(":%s", port)
} else {
port = ":8080"
}

http.HandleFunc("/", tester)

log.Fatal(http.ListenAndServe(port, nil))
}

// tester
func tester(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s!", html.EscapeString(
r.URL.Query().Get("name")))
log.Printf("request received, details follow:\n%+v\n", r)
}
15 changes: 15 additions & 0 deletions apps/webapp-registry/scripts/rm_helpers.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function ensure_group () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be merged into setup.sh or some common utility location. Shared between here and #216

local group_name=$1
local group_id

group_id=$(az group show --name $group_name --query 'id' --output tsv 2> /dev/null)

if [[ -z $group_id ]]; then
group_id=$(az group create \
--name $group_name \
--location $location \
--query 'id' --output tsv)
fi
echo $group_id
}

Loading