-
Notifications
You must be signed in to change notification settings - Fork 185
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
GH_TOKEN= |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out/ | ||
.env |
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"] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[prune] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
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. |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
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 |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
* 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package main | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
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) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
function ensure_group () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be merged into |
||
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 | ||
} | ||
|
There was a problem hiding this comment.
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