Last active
March 14, 2018 21:39
-
-
Save tdmalone/50998ff4c9b2d0c60c82a5a4baeca83d to your computer and use it in GitHub Desktop.
.travis.yml example for Python Lambda microservice testing and deployment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
language: python | |
services: docker | |
python: 3.6 | |
env: | |
global: | |
- AWS_ACCESS_KEY_ID=... | |
- AWS_DEFAULT_REGION=ap-southeast-2 | |
- LAMBDA_NAME=... | |
- LAMBDA_DESCRIPTION=... | |
- LAMBDA_TIMEOUT=... | |
- LAMBDA_ROLE=arn:aws:iam::873114526714:role/genericLambdaRole | |
- LAMBDA_ALIAS=prod | |
- LAMBDA_RUNTIME=python3.6 | |
- LAMBDA_MODULE=lambda_function | |
- LAMBDA_HANDLER=lambda_handler | |
- PYPI_USERNAME=tdmalone | |
# AWS_SECRET_ACCESS_KEY | |
- secure: ... | |
# PYPI_PASSWORD | |
- secure: ... | |
install: | |
- pip install --requirement requirements.txt | |
- curl --output ~/.pylint.rc https://raw.githubusercontent.com/tdmalone/dotfiles/master/.pylintrc | |
script: | |
- make lint | |
- make test | |
# Copy the Lambda function as a workaround for a PyPi deployment. | |
before_deploy: | |
- mkdir "${LAMBDA_NAME}" | |
- cp lambda_function.py "${LAMBDA_NAME}"/ | |
deploy: | |
# Deploy to $LATEST on dev branch. (i.e. don't publish a new version). | |
- on: | |
branch: dev | |
publish: false | |
provider: lambda | |
function_name: $LAMBDA_NAME | |
region: $AWS_DEFAULT_REGION | |
role: $LAMBDA_ROLE | |
description: $LAMBDA_DESCRIPTION | |
runtime: $LAMBDA_RUNTIME | |
timeout: $LAMBDA_TIMEOUT | |
module_name: $LAMBDA_MODULE | |
handler_name: $LAMBDA_HANDLER | |
access_key_id: $AWS_ACCESS_KEY_ID | |
secret_access_key: $AWS_SECRET_ACCESS_KEY | |
skip_cleanup: true | |
# Deploy and publish a new version on master branch. | |
- on: | |
branch: master | |
publish: true | |
provider: lambda | |
function_name: $LAMBDA_NAME | |
region: $AWS_DEFAULT_REGION | |
role: $LAMBDA_ROLE | |
description: $LAMBDA_DESCRIPTION | |
runtime: $LAMBDA_RUNTIME | |
timeout: $LAMBDA_TIMEOUT | |
handler_name: handler | |
access_key_id: $AWS_ACCESS_KEY_ID | |
secret_access_key: $AWS_SECRET_ACCESS_KEY | |
skip_cleanup: true | |
# Deploy to pypi on master with a new tag. | |
- on: | |
branch: master | |
tags: true | |
provider: pypi | |
username: $PYPI_USERNAME | |
password: $PYPI_PASSWORD | |
skip_cleanup: true | |
after_deploy: | |
# Set a Lambda alias to the most recently deployed version. | |
- if [ "master" = "${TRAVIS_BRANCH}" ]; then | |
pip install awscli --upgrade --user; | |
export MOST_RECENT=$(aws lambda list-versions-by-function --function "${LAMBDA_NAME}" --max-items 10000 | node -e "let stdin=''; process.stdin.on('data',(chunk)=>{stdin+=chunk}).on('end',()=>{console.log(JSON.parse(stdin).Versions.pop().Version)})"); | |
aws lambda update-alias --function-name "${LAMBDA_NAME}" --name "${LAMBDA_ALIAS}" --function-version "${MOST_RECENT}"; | |
fi; | |
notifications: | |
email: false | |
webhooks: | |
urls: https://api.tm.id.au/v2/travis/jobStatus | |
slack: | |
on_start: always | |
rooms: | |
- secure: ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment