Skip to content

Commit

Permalink
Add Python CI workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
hmstepanek committed Apr 19, 2024
1 parent 93f0aca commit 6a6e668
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
121 changes: 121 additions & 0 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# Copyright 2010 New Relic, Inc.
#
# 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.
---
name: Test Python init container image

on:
# Allow manual trigger.
workflow_dispatch:
push:
# Run tests on push to main or PR.
branches:
- main
pull_request:
# Do not run when a tag is created.
tags-ignore:
- "**"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Build
run: go build -o myapp .
- name: Docker Build and Push
uses: docker/build-push-action@v2
with:
context: .
push: true
tags: your-docker-registry/your-repo:latest
concurrency:
group: ${{ github.ref || github.run_id }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # 4.1.1
with:
persist-credentials: false
fetch-depth: 0

- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action@d70bba72b1f3fd22344832f00baa16ece964efeb # 3.3.0

- name: Generate Docker metadata (tags and labels)
id: meta
uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # 5.5.1
with:
images: ghcr.io/${{ github.repository }}-python
flavor: |
prefix=
suffix=
latest=false
tags: |
type=raw,value=latest,enable={{is_default_branch}}
type=schedule,pattern={{date 'YYYY-MM-DD'}}
type=sha,format=short,prefix=sha-
type=sha,format=long,prefix=sha-
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@e92390c5fb421da1463c202d546fed0ec5c39f20 # 3.1.0
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and publish init container image
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # 5.3.0
with:
push: ${{ github.event_name != 'pull_request' }}
context: .github/containers
platforms: ${{ (format('refs/heads/{0}', github.event.repository.default_branch) == github.ref) && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Build and publish test app image
uses: docker/build-push-action@2cdde995de11925a030ce8070c3d77a52ffcf1c0 # 5.3.0
with:
push: ${{ github.event_name != 'pull_request' }}
context: .github/containers
platforms: ${{ (format('refs/heads/{0}', github.event.repository.default_branch) == github.ref) && 'linux/amd64,linux/arm64' || 'linux/amd64' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

- name: Start minikube
uses: medyagh/setup-minikube@317d92317e473a10540357f1f4b2878b80ee7b95 # 0.0.16

- name: Build image
run: |
export SHELL=/bin/bash
minikube -p minikube docker-env
make build-image
echo -n "verifying images:"
docker images
- name: Deploy to minikube
run: |
kubectl apply -f deploy/deploy-minikube.yaml
# Wait 30 seconds for the cluster to start and then hit an application endpoint.
- name: Test service URLs
run: |
curl $(minikube service --wait 30 discover --url)/version
curl http://localhost:5000
15 changes: 15 additions & 0 deletions python/tests/customresource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
apiVersion: newrelic.com/v1alpha1
kind: Instrumentation
metadata:
labels:
app.kubernetes.io/name: instrumentation
app.kubernetes.io/created-by: newrelic-agent-operator
name: newrelic-instrumentation
spec:
python:
image: ghcr.io/${{ github.repository }}-python
env:
# Put the agent in developer mode so it doesn't try to connect to the real collector.
- name: NEW_RELIC_DEV_MODE
value: true
11 changes: 11 additions & 0 deletions python/tests/flask.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import newrelic.agent
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
transaction = newrelic.agent.current_transaction()
if not transaction:
flask.abort(417)
return "<p>Hello, World!</p>"
34 changes: 34 additions & 0 deletions python/tests/python_test_app_deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: flask-hello-world
spec:
selector:
matchLabels:
app: flask-hello-world
replicas: 1
template:
metadata:
labels:
app: flask-hello-world
annotations:
instrumentation.newrelic.com/inject-python: "true"
spec:
containers:
- name: flask-hello-world
image: ghcr.io/anuraaga/flask-hello-world:latest
ports:
- containerPort: 5000
---
apiVersion: v1
kind: Service
metadata:
name: flask-hello-world-service
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 5000
selector:
app: flask-hello-world

0 comments on commit 6a6e668

Please sign in to comment.