Skip to content

Commit 2db69e2

Browse files
authored
Merge pull request #3 from cse-labs/erisch/service/v1
Adding Github Actions Workflow for Pipeline Config
2 parents 453a23e + 0797971 commit 2db69e2

File tree

12 files changed

+213
-14
lines changed

12 files changed

+213
-14
lines changed

.github/workflows/azure-dev.yaml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
on:
2+
workflow_dispatch:
3+
push:
4+
branches:
5+
- main
6+
paths:
7+
- 'azure.yaml'
8+
- 'infra/**'
9+
- 'src/**'
10+
- 'tests/**'
11+
- '.github/workflows/azure-dev.yml'
12+
13+
permissions:
14+
id-token: write
15+
contents: read
16+
17+
jobs:
18+
build:
19+
runs-on: ubuntu-latest
20+
env:
21+
AZURE_CLIENT_ID: ${{ vars.AZURE_CLIENT_ID }}
22+
AZURE_TENANT_ID: ${{ vars.AZURE_TENANT_ID }}
23+
AZURE_SUBSCRIPTION_ID: ${{ vars.AZURE_SUBSCRIPTION_ID }}
24+
AZURE_ENV_NAME: ${{ vars.AZURE_ENV_NAME }}
25+
AZURE_LOCATION: ${{ vars.AZURE_LOCATION }}
26+
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
27+
GITOPS_REPO: ${{ vars.GITOPS_REPO }}
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v3
31+
- name: Install azd
32+
uses: Azure/[email protected]
33+
- name: pre validation
34+
shell: bash
35+
run: |
36+
for varName in GITHUB_TOKEN AZURE_ENV_NAME AZURE_LOCATION AZURE_SUBSCRIPTION_ID GITOPS_REPO AZURE_TENANT_ID; do
37+
varVal=$(eval echo "\${$varName}")
38+
[[ -z $varVal ]] && {
39+
echo "💥 Error! Required env variable or secret '$varName' is not set!"
40+
envUnset=true
41+
}
42+
done
43+
if [ "$envUnset" = true ]; then
44+
exit 1
45+
fi
46+
47+
- name: Kubectl Install
48+
uses: azure/setup-kubectl@v3
49+
50+
- name: Log in with Azure
51+
if: ${{ env.AZURE_CLIENT_ID != '' }}
52+
run: |
53+
azd auth login --no-prompt `
54+
--client-id "$Env:AZURE_CLIENT_ID" `
55+
--federated-credential-provider "github" `
56+
--tenant-id "$Env:AZURE_TENANT_ID"
57+
shell: pwsh
58+
59+
- name: Azure login (for the preprovision script)
60+
uses: azure/login@v1
61+
with:
62+
client-id: ${{ env.AZURE_CLIENT_ID }}
63+
subscription-id: ${{ env.AZURE_SUBSCRIPTION_ID }}
64+
tenant-id: ${{ env.AZURE_TENANT_ID }}
65+
66+
- name: Github CLI install
67+
run: |
68+
type -p curl >/dev/null || apt install curl -y
69+
sudo curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
70+
&& chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
71+
&& echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
72+
&& sudo apt update \
73+
&& sudo apt install gh jq gettext-base -y
74+
75+
- name: Azure Dev Provision
76+
run: |
77+
git config --global user.name github-actions
78+
git config --global user.email [email protected]
79+
80+
azd up --no-prompt

scripts/postpipelineconfig.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/bin/bash
2+
PWD=$(dirname "$0")
3+
4+
function sourceAzdEnvVars() {
5+
while IFS='=' read -r key value; do
6+
value=$(echo "$value" | sed 's/^"//' | sed 's/"$//')
7+
export "$key=$value"
8+
done <<EOF
9+
$(azd env get-values)
10+
EOF
11+
}
12+
13+
GIT_CONTROLLER_URL=$(git remote get-url origin)
14+
15+
function pre_validation () {
16+
local git_url=$1
17+
18+
[[ ! -z $git_url ]] && {
19+
echo "💥 Error! Git remote is missing. This script should run after azd pipeline config!"
20+
exit
21+
}
22+
23+
for varName in AZURE_AKS_CLUSTER_NAME GITHUB_TOKEN; do
24+
varVal=$(eval echo "\${$varName}")
25+
[[ -z $varVal ]] && {
26+
echo "💥 Error! Required variable '$varName' is not set!"
27+
varUnset=true
28+
}
29+
done
30+
}
31+
32+
sourceAzdEnvVars
33+
pre_validation
34+
GIT_CONTROLLER_REPO=$(echo $GIT_CONTROLLER_URL | sed 's|https://github.com/||')
35+
36+
gh secret set GH_TOKEN --body "${GITHUB_TOKEN}" --repo $GIT_CONTROLLER_REPO
37+
38+
for env in AZURE_AKS_CLUSTER_NAME; do
39+
echo "setting env variable: ${env} in repo: ${GIT_CONTROLLER_REPO}"
40+
envVal=$(eval echo "\${$env}")
41+
gh variable set $env --body "${envVal}" --repo $GIT_CONTROLLER_REPO
42+
done

src/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ COPY requirements.txt .
66

77
RUN pip3 install -r requirements.txt
88

9-
COPY . /app
9+
COPY . .
1010

1111
EXPOSE 5000
12-
ENTRYPOINT [ "python3", "api/app.py" ]
12+
ENTRYPOINT ["gunicorn", "-c", "gunicorn.conf.py", "app:app"]

src/api/__init__.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import os
2+
3+
from flask import Flask
4+
from opencensus.ext.azure.trace_exporter import AzureExporter
5+
from opencensus.ext.flask.flask_middleware import FlaskMiddleware
6+
from opencensus.trace.samplers import ProbabilitySampler
7+
8+
app_insights_connection_string = os.getenv('APPLICATIONINSIGHTS_CONNECTION_STRING')
9+
10+
def create_app():
11+
flask = Flask(__name__)
12+
13+
from . import app
14+
flask.register_blueprint(app.bp, threaded=True)
15+
_ = FlaskMiddleware(\
16+
flask, \
17+
exporter=AzureExporter(\
18+
connection_string=app_insights_connection_string), \
19+
sampler=ProbabilitySampler(rate=1.0)
20+
)
21+
22+
return flask

src/api/app.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1-
from flask import Flask, jsonify
1+
import logging
2+
3+
from flask import jsonify, Blueprint
4+
from opencensus.trace import config_integration
5+
from opencensus.trace.samplers import AlwaysOnSampler
6+
from opencensus.trace.tracer import Tracer
27
import randomname
38

4-
app = Flask(__name__)
9+
bp = Blueprint("names", __name__)
10+
config_integration.trace_integrations(['logging'])
11+
logging.basicConfig(format='%(asctime)s traceId=%(traceId)s spanId=%(spanId)s %(message)s')
12+
tracer = Tracer(sampler=AlwaysOnSampler())
13+
logger = logging.getLogger(__name__)
14+
logger.setLevel(logging.DEBUG)
515

6-
@app.route('/')
16+
@bp.route("/")
717
def hello_world():
818
# Generate a random name including a first name and adjective
919
random_name = randomname.generate()
20+
with tracer.span(name=__name__):
21+
logger.info("Random Name Selected: - %s", random_name)
22+
1023
json = {"name": random_name}
11-
return jsonify(json)
1224

13-
if __name__ == '__main__':
14-
app.run(debug=True,host='0.0.0.0')
25+
return jsonify(json)

src/api/manifests/values.yaml.tmpl

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
nameOverride: ${SERVICE_NAME}-istio-svc
22
fullnameOverride: ${SERVICE_NAME}-istio-svc
3+
virtualServicePort: 5000
4+
35
replicas: 1
46

57
istio:
@@ -13,4 +15,18 @@ image:
1315
pullSecret: private-registry
1416

1517
service:
16-
port: 5000
18+
port: 5000
19+
20+
secretStore:
21+
enabled: true
22+
keyvaultName: ${AZURE_KEY_VAULT_NAME}
23+
tenantId: ${AZURE_TENANT_ID}
24+
useVMManagedIdentity: "true"
25+
usePodIdentity: "false"
26+
cloudName: AzureCloud
27+
secrets:
28+
- envName: APPLICATIONINSIGHTS_CONNECTION_STRING
29+
objectName: appInsightsConnectionString
30+
alias: appsinsights
31+
identity:
32+
clientId: ${AZURE_AKS_KV_PROVIDER_CLIENT_ID}

src/app.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from api import create_app
2+
3+
app = create_app()

src/azure.yaml

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/gunicorn.conf.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bind = "[::]:5000"
2+
workers = 4
3+
threads = 4
4+
timeout = 120

0 commit comments

Comments
 (0)