Skip to content

Commit

Permalink
add lambda-python wf
Browse files Browse the repository at this point in the history
  • Loading branch information
rbapst-tamedia committed Dec 18, 2024
1 parent 849faab commit f002672
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 0 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/_test-lambda-python.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Build and Deploy Python

on:
pull_request:
branches:
- main
paths:
- '.github/workflows/_test-lambda-python.yaml'
- '.github/workflows/lambda-python.yaml'
- 'lambda/python/**'

jobs:
test:
if: github.event_name == 'pull_request'
runs-on: ubuntu-latest
defaults:
run:
working-directory: tests/lambda/python
steps:
- name: Checkout
uses: actions/checkout@v4

build:
uses: ./.github/workflows/lambda-python.yaml
needs: test
with:
python_version: "1.12"
source_dir: tests/lambda/python
gh_artifact_name: tests-lambda-python
gh_artifact_path: tests/lambda/python/python.zip
gh_artifact_retention_days: 1
54 changes: 54 additions & 0 deletions .github/workflows/lambda-python.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Build Python Lambda
on:
workflow_call:
inputs:
python_version:
description: "Python version"
type: string
default: "3.12"
source_dir:
description: "Directory of the Lambda source code"
type: string
required: true
gh_artifact_name:
description: "Name of the artifact to upload"
type: string
gh_artifact_path:
description: "A file, directory or wildcard pattern that describes what to upload"
type: string
default: "**/*.zip"
gh_artifact_retention_days:
description: "Number of days to retain the artifact"
type: number
default: 30
jobs:
build:
name: Build Python
runs-on: ubuntu-latest
env:
PYTHON_VERSION: ${{ inputs.python_version }}
SOURCE_DIR: ${{ inputs.source_dir }}
defaults:
run:
working-directory: ${{ env.SOURCE_DIR }}
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}

- name: Download Python modules and zip
run: |
pip install -r requirements.txt -t .
zip -r $(basename $PWD).zip .
- name: Archive
uses: actions/upload-artifact@b4b15b8c7c6ac21ea08fcf65892d2ee8f75cf882 # v4
with:
name: ${{ inputs.gh_artifact_name }}
path: ${{ inputs.gh_artifact_path }}
if-no-files-found: error
retention-days: ${{ inputs.gh_artifact_retention_days }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ site/
# Virtual environment
venv/
.venv/
# cache
__pycache__
95 changes: 95 additions & 0 deletions docs/workflows/lambda-python.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: Build Python Lambda
---

<!-- action-docs-header source=".github/workflows/lambda-python.yaml" -->
# Build Python Lambda
<!-- action-docs-header source=".github/workflows/lambda-python.yaml" -->

## Description

Simple workflow to build a Python Lambda function and upload the artifact to the GitHub artifact store.

All modules defined in the `packages.txt` file will be installed and packaged into a zip file.

<!-- action-docs-inputs source=".github/workflows/lambda-python.yaml" -->
## Inputs

| name | description | type | required | default |
| --- | --- | --- | --- | --- |
| `python_version` | <p>Python version</p> | `string` | `false` | `3.12` |
| `source_dir` | <p>Directory of the Lambda source code</p> | `string` | `true` | `""` |
| `gh_artifact_name` | <p>Name of the artifact to upload</p> | `string` | `false` | `""` |
| `gh_artifact_path` | <p>A file, directory or wildcard pattern that describes what to upload</p> | `string` | `false` | `**/*.zip` |
| `gh_artifact_retention_days` | <p>Number of days to retain the artifact</p> | `number` | `false` | `30` |
<!-- action-docs-inputs source=".github/workflows/lambda-python.yaml" -->

<!-- action-docs-outputs source=".github/workflows/lambda-python.yaml" -->

<!-- action-docs-outputs source=".github/workflows/lambda-python.yaml" -->

<!-- action-docs-usage source=".github/workflows/lambda-python.yaml" project="tx-pts-dai/github-workflows/.github/workflows/lambda-python.yaml" version="v1" -->
## Usage

```yaml
jobs:
job1:
uses: tx-pts-dai/github-workflows/.github/workflows/lambda-python.yaml@v1
with:
python_version:
# Python version
#
# Type: string
# Required: false
# Default: 3.12

source_dir:
# Directory of the Lambda source code
#
# Type: string
# Required: true
# Default: ""

gh_artifact_name:
# Name of the artifact to upload
#
# Type: string
# Required: false
# Default: ""

gh_artifact_path:
# A file, directory or wildcard pattern that describes what to upload
#
# Type: string
# Required: false
# Default: **/lambda.zip

gh_artifact_retention_days:
# Number of days to retain the artifact
#
# Type: number
# Required: false
# Default: 30
```
<!-- action-docs-usage source=".github/workflows/lambda-python.yaml" project="tx-pts-dai/github-workflows/.github/workflows/lambda-python.yaml" version="v1" -->

# Example

```
name: Build
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
uses: tx-pts-dai/github-workflows/.github/workflows/lambda-python.yaml@v1
with:
python_version: "3.12"
source_dir: lambdas/first_lambda
```

# FAQ
Empty file added tests/lambda/python/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/lambda/python/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import requests

h = 'https://www.example.com'
r = requests.get(h)
s = r.status_code
print(f"{h} returns {s} status code")
1 change: 1 addition & 0 deletions tests/lambda/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
requests == 2.32.3

0 comments on commit f002672

Please sign in to comment.