Skip to content

Commit fe165c5

Browse files
authored
Merge pull request #8 from UWIT-IAM/template-updates
Template updates
2 parents 25f48f0 + 7b5cd3f commit fe165c5

16 files changed

+104
-135
lines changed

.github/workflows/deploy.template.yml

+36-12
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ jobs:
3838
- name: Install and configure poetry
3939
run: |
4040
pipx install poetry
41-
poetry config virtualenvs.create false
4241
4342
- if: github.event.inputs.cluster == 'prod'
4443
run: |
@@ -51,19 +50,40 @@ jobs:
5150
- name: Install package
5251
run: poetry install
5352

54-
- name: Install jq for fingerprinter
55-
run: sudo apt-get -y install jq
56-
5753
- name: Update env with promotion version that was provided
5854
if: github.event.inputs.version
5955
run: echo "target_version=${{ github.event.inputs.version }}" >> $GITHUB_ENV
6056

6157
- name: Update env with promotion version if not provided
6258
if: '! env.target_version'
59+
shell: bash
6360
run: |
64-
source ./scripts/globals.sh
65-
target_version=$(get_promotion_version ${{ github.event.inputs.cluster }})
66-
echo "target_version=${target_version}" >> $GITHUB_ENV
61+
# $1 will be package name, $2 will be current version
62+
set $(poetry version)
63+
64+
case "${{ github.event.inputs.cluster }}" in
65+
dev)
66+
version="$2"
67+
echo "Would set version to '$version' (from poetry version)"
68+
;;
69+
eval)
70+
# substitute '_' for '-' in APP_NAME
71+
url="https://${1//_/-}.iamdev.s.uw.edu/status"
72+
version=$(curl --silent $url | python -c "import json, sys; print(json.load(sys.stdin)['version'])")
73+
echo "After consulting dev the eval version will be ${version}"
74+
;;
75+
prod)
76+
# substitute '_' for '-' in APP_NAME
77+
url="https://${1//_/-}.iameval.s.uw.edu/status"
78+
version=$(curl --silent $url | python -c "import json, sys; print(json.load(sys.stdin)['version'])")
79+
echo "After consulting eval the prod version will be ${version}"
80+
;;
81+
*)
82+
echo "Invalid cluster! Pick one of dev|eval|prod"
83+
exit 1
84+
;;
85+
esac
86+
echo "target_version=${version}" >> $GITHUB_ENV
6787
6888
- name: Auth to Google Cloud
6989
# important! this 'auth' is referenced as `steps.auth` on the next job
@@ -80,10 +100,14 @@ jobs:
80100
run: |-
81101
echo '${{ steps.auth.outputs.access_token }}' | docker login -u oauth2accesstoken --password-stdin https://us-docker.pkg.dev
82102
83-
- name: Deploy version ${{ env.target_version }}
103+
- name: Tag version ${{ env.target_version }} for ${{ github.event.inputs.cluster }}
84104
id: deploy
85105
run: |
86-
echo "::notice::Deploying appid version ${{ env.target_version }} to ${{ github.event.inputs.cluster }}"
87-
./scripts/build.sh \
88-
--deploy ${{ inputs.cluster }} \
89-
-dversion ${{ env.target_version }}
106+
# timestamp and deploy_tag are not DRY - see also release-on-push-to-main.yaml
107+
timestamp=$(date --utc +%Y.%m.%d.%H.%M.%S)
108+
deploy_tag="deploy-${{ github.event.inputs.cluster }}.${timestamp}.v${{ env.target_version }}"
109+
echo "::notice::Deploying appid version ${{ env.target_version }} to ${{ github.event.inputs.cluster }} as ${deploy_tag}"
110+
# this will create a new tag (deploy_tag) on an existing tag (env.target_version)
111+
docker buildx imagetools create \
112+
--tag us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}:${deploy_tag} \
113+
us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}:${{ env.target_version }}

.github/workflows/finalize-template.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Finalize Template
33
on:
44
workflow_dispatch:
55
inputs:
6-
app-name:
6+
app_name:
77
required: false
88
description: >
99
This is the name of your app; if left blank, it will match the

.github/workflows/pull-request.template.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
file: ./Dockerfile
7272
push: true
7373
target: dependencies
74-
tags: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}.dependencies:${{ env.pr_tag }}
74+
tags: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}.dependencies:${{ env.pr_tag }}
7575
secret-files: |
7676
"gcloud_auth_credentials=${{ steps.auth.outputs.credentials_file_path }}"
7777
@@ -82,7 +82,7 @@ jobs:
8282
file: ./Dockerfile
8383
push: true
8484
target: app
85-
tags: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}.app:${{ env.pr_tag }}
85+
tags: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}.app:${{ env.pr_tag }}
8686

8787
- name: Build and push Docker image (tests)
8888
uses: docker/build-push-action@v5
@@ -91,11 +91,11 @@ jobs:
9191
file: ./Dockerfile
9292
push: true
9393
target: tests
94-
tags: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}.tests:${{ env.pr_tag }}
94+
tags: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}.tests:${{ env.pr_tag }}
9595

9696
- uses: mshick/add-pr-comment@v2
9797
env:
98-
image: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}.app:${{ env.pr_tag }}
98+
image: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}.app:${{ env.pr_tag }}
9999
with:
100100
repo-token: ${{ secrets.GITHUB_TOKEN }}
101101
allow-repeats: false

.github/workflows/release-on-push-to-main.template.yaml

+3-6
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,14 @@ jobs:
5656

5757
- name: Build and push Docker image
5858
uses: docker/build-push-action@v5
59-
env:
60-
DEPLOYMENT_ID: deploy-dev.${{ steps.get-version.outputs.timestamp }}.v${{ steps.get-version.outputs.version }}
6159
with:
62-
build-args: DEPLOYMENT_ID=${{ env.DEPLOYMENT_ID }}
6360
context: .
6461
file: ./Dockerfile
6562
push: true
6663
target: app
6764
tags: |
68-
us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}:${{ steps.get-version.outputs.version }}
69-
us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}:${{ env.DEPLOYMENT_ID }}
65+
us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}:${{ steps.get-version.outputs.version }}
66+
us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}:deploy-dev.${{ steps.get-version.outputs.timestamp }}.v${{ steps.get-version.outputs.version }}
67+
us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}:latest
7068
secret-files: |
7169
"gcloud_auth_credentials=${{ steps.auth.outputs.credentials_file_path }}"
72-

Dockerfile Dockerfile.template

+4-5
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@ RUN --mount=type=secret,id=gcloud_auth_credentials \
1313
poetry install --only main --no-root --no-interaction
1414

1515
FROM dependencies AS app
16-
17-
ARG DEPLOYMENT_ID
18-
ARG APP_MODULE=example_app
16+
# If you change your app directory to e.g., use src/ you MUST
17+
# change the APP_MODULE here to match OR supply a --build-arg
18+
ARG APP_MODULE=${template:app_name_underscore}
1919
ARG FLASK_PORT=5000
2020
ENV FLASK_ENV=development \
2121
PYTHONPATH=${APP_MODULE} \
22-
FLASK_APP=${APP_MODULE}.app \
23-
DEPLOYMENT_ID=${DEPLOYMENT_ID}
22+
FLASK_APP=${APP_MODULE}.app
2423
EXPOSE ${FLASK_PORT}
2524
COPY ${APP_MODULE}/ ./${APP_MODULE}
2625
# install root package now that we've copied it

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ click the button above that says "Use this template"
1111

1212
You will be asked to name your new repository. To take full advantage
1313
of the automation and kubernetes capabilities, you should create your new
14-
repository under the "UWIT-IAM" namespace; this gives you access to
14+
repository under the "UWIT-IAM" namespace; this gives you access to
1515
secrets that are consumable in your Github actions.
1616

1717
Once you have done that, follow the instructions for
@@ -22,12 +22,12 @@ Once you have done that, follow the instructions for
2222
If you are reading this, and you are not in the `flask-example` template repository,
2323
then you haven't yet finalized your template!
2424

25-
To finalize the template, visit your repository on GitHub,
25+
To finalize the template, visit your repository on GitHub,
2626
then click on "Actions."
2727

2828
Click the "Finalize Template" workflow, and click "Run this workflow."
2929

30-
A pull request will be generated for you to review and merge! This message will
30+
A pull request will be generated for you to review and merge! This message will
3131
self-destruct after running that workflow.
3232

3333
## Updating the template
@@ -37,15 +37,15 @@ This very basic templating engine does not allow for conditional logic.
3737
To use an argument name inside a document, make sure the document is named `<name>.
3838
template.<ext>`, the final file name will be `<name>.<ext>`.
3939

40-
You can use any supported argument with the format: `${template:<arg_name>}`; all
41-
values are treated as strings.
40+
You can use any supported argument with the format: `${template:<arg_name>}`; all
41+
values are treated as strings.
4242

4343
Functionally:
4444

4545
```
4646
# foo.template.yaml
4747
48-
- policy-name: ${template:app_name}-policy
48+
- policy-name: ${template:app_name_hyphen}-policy
4949
```
5050

5151
becomes:
@@ -60,8 +60,8 @@ becomes:
6060
### About templating templates...
6161

6262
Please note that `.template.` files are interpolated before any other templating
63-
engine does anything; you may freely nest the `${template:<arg_name>}` syntax inside
64-
other strings that other templating engines might use.
63+
engine does anything; you may freely nest the `${template:<arg_name>}` syntax inside
64+
other strings that other templating engines might use.
6565

6666
## Supported Values
6767

example_app/app.template.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import importlib.metadata as importlib_metadata # Python ^3.9 change
32

43
from flask import Flask, jsonify
@@ -9,7 +8,7 @@
98
@app.route("/", methods=("GET",))
109
def index():
1110
print("Hello there")
12-
return 'OK', 200
11+
return "OK", 200
1312

1413

1514
APP_VERSION = None
@@ -19,13 +18,15 @@ def index():
1918
def status():
2019
global APP_VERSION
2120
if APP_VERSION is None:
22-
APP_VERSION = importlib_metadata.version("${template:app_name}")
21+
try:
22+
APP_VERSION = importlib_metadata.version("${template:app_name_underscore}")
23+
except importlib_metadata.PackageNotFoundError:
24+
"Something went wrong locating the package that is this application. Was it installed via poetry?"
2325

24-
deployment_id = os.environ.get("DEPLOYMENT_ID")
25-
status = 200 if deployment_id else 503
26+
status = 200 if APP_VERSION else 503
2627

27-
return jsonify({"deployment_id": deployment_id, "version": APP_VERSION}), status
28+
return jsonify({"version": APP_VERSION}), status
2829

2930

3031
if __name__ == "__main__":
31-
app.run(host='0.0.0.0', port=5000)
32+
app.run(host="0.0.0.0", port=5000)

fingerprints.template.yaml

-23
This file was deleted.

kubernetes-config/README.template.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Template Kubernetes Configuration
22

3-
The basic kubernetes configuration provided here will subscribe you
4-
to the [basic-web-service helm chart].
3+
The basic kubernetes configuration provided here will subscribe you
4+
to the [basic-web-service helm chart].
55

66
After finalizing your template, you should copy the files in this directory
77
into the [gcp-k8] repository, in the `dev/${template:app_name}` directory.
88

99
Unless you change the values generated for you, your app will
10-
expect to run at `https://${template:app_name}.iamdev.s.uw.edu`.
10+
expect to run at `https://${template:app_name_hyphen}.iamdev.s.uw.edu`.
1111

1212

1313
[gcp-k8]: https://github.com/uwit-iam/gcp-k8

kubernetes-config/automation.template.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
apiVersion: image.toolkit.fluxcd.io/v1beta1
55
kind: ImagePolicy
66
metadata:
7-
name: ${template:app_name}-policy
7+
name: ${template:app_name_hyphen}-policy
88
namespace: default # must be 'default' in MCI, even if app itself is not in default
99
spec:
1010
imageRepositoryRef:
11-
name: ${template:app_name}-gar
11+
name: ${template:app_name_hyphen}-gar
1212
filterTags:
1313
pattern: '^deploy-dev.(?P<ts>[0-9\.]+)\.v.+$'
1414
extract: '$ts'
@@ -19,10 +19,10 @@ spec:
1919
apiVersion: image.toolkit.fluxcd.io/v1beta1
2020
kind: ImageRepository
2121
metadata:
22-
name: ${template:app_name}-gar
22+
name: ${template:app_name_hyphen}-gar
2323
namespace: default # must be 'default' in MCI, even if app itself is not in default
2424
spec:
25-
image: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}
25+
image: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}
2626
interval: 2m0s
2727
secretRef:
2828
name: flux-mci-registry-credential

kubernetes-config/helm-release.template.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
apiVersion: helm.toolkit.fluxcd.io/v2beta1
1010
kind: HelmRelease
1111
metadata:
12-
name: ${template:app_name}
12+
name: ${template:app_name_hyphen}
1313
namespace: default
1414
spec:
1515
values:
1616
app:
17-
name: ${template:app_name}
17+
name: ${template:app_name_hyphen}
1818
clusterDomain: iamdev.s.uw.edu
1919
replicaCount: 1
2020
ports:
@@ -45,8 +45,8 @@ spec:
4545
memory: "64M"
4646
cpu: "100m"
4747
image:
48-
name: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name}
49-
tag: latest # {"$imagepolicy": "default:${template:app_name}-policy:tag"}
48+
name: us-docker.pkg.dev/uwit-mci-iam/containers/${template:app_name_hyphen}
49+
tag: latest # {"$imagepolicy": "default:${template:app_name_hyphen}-policy:tag"}
5050

5151
chart:
5252
spec:

pyproject.template.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[tool.poetry]
2-
name = "${template:app_name}"
2+
name = "${template:app_name_underscore}"
33
version = "0.1.0"
44
description = ""
55
authors = []
@@ -23,3 +23,7 @@ priority = "explicit"
2323
[build-system]
2424
requires = ["poetry-core>=1.0.0"]
2525
build-backend = "poetry.core.masonry.api"
26+
27+
[tool.black]
28+
# coordinated with setup.cfg max-line-length
29+
line-length = 119

0 commit comments

Comments
 (0)