Last active
December 4, 2020 23:23
-
-
Save sburns/1b5f6ea6810f9f34ddf57bcea53024c9 to your computer and use it in GitHub Desktop.
This is an example GitHub Actions workflow to continuously deploy a Lambda function.
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
name: example CICD | |
on: | |
push: | |
branches: | |
- master | |
paths: | |
# only commits containing changes under these paths will trigger this workflow | |
- 'services/example/**' | |
- 'lib/example/**' | |
- '.github/workflows/example.yml' | |
pull_request: | |
branches: | |
- '*' | |
paths: | |
- 'services/example/**' | |
- 'lib/example/**' | |
- '.github/workflows/example.yml' | |
env: | |
TF_IN_AUTOMATION: 'true' | |
AWS_DEFAULT_REGION: 'us-east-1' | |
TF_VAR_upload_key: lambda_uploads/example-${{ github.sha }}.zip | |
TF_VAR_upload_bucket: YOUR_BUCKET | |
jobs: | |
format: | |
runs-on: ubuntu-latest | |
name: Terraform Linting | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Install tfenv | |
run: | | |
git clone https://github.com/tfutils/tfenv.git ~/.tfenv | |
echo "$HOME/.tfenv/bin" >> $GITHUB_PATH | |
- name: Install Terraform | |
working-directory: services/example | |
run: | | |
tfenv install | |
terraform --version | |
- name: Linting | |
working-directory: services/example | |
run: | | |
terraform fmt -no-color -check -list -recursive | |
build: | |
runs-on: ubuntu-latest | |
name: Build | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Install python 3.7 | |
uses: actions/setup-python@v1 | |
with: | |
python-version: '3.7.x' | |
architecture: 'x64' | |
- name: Requirements & Package | |
working-directory: services/example | |
run: | | |
make venv | |
. venv/bin/activate | |
make package | |
ls -la | |
- name: Upload artifact to S3 | |
working-directory: services/example | |
run: | | |
# Upload to S3 | |
aws s3 cp lambda.zip s3://${TF_VAR_upload_bucket}/${TF_VAR_upload_key} | |
# Apply a tag on the object, opting it into a lifecycle | |
aws s3api put-object-tagging \ | |
--bucket ${TF_VAR_upload_bucket} \ | |
--key ${TF_VAR_upload_key} \ | |
--tagging '{"TagSet": [{"Key": "Lifecycle", "Value": "cicd_cleanup"}]}' | |
terraform: | |
needs: ['format', 'build'] | |
runs-on: ubuntu-latest | |
name: Deploy | |
env: | |
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
TF_WORKSPACE: 'dev' | |
steps: | |
- uses: actions/checkout@v2 | |
- name: Install tfenv | |
run: | | |
git clone https://github.com/tfutils/tfenv.git ~/.tfenv | |
echo "$HOME/.tfenv/bin" >> $GITHUB_PATH | |
- name: Install Terraform | |
working-directory: services/example | |
run: | | |
tfenv install | |
terraform --version | |
- name: Init | |
working-directory: services/example | |
run: | | |
terraform init -no-color -input=false | |
terraform validate -no-color | |
- name: Plan & Apply (Dev) | |
if: github.event_name == 'pull_request' | |
working-directory: services/example | |
run: | | |
terraform plan -no-color -input=false | |
terraform apply -no-color -auto-approve -input=false | |
- name: Plan & Apply (Prod) | |
if: github.event_name == 'push' | |
working-directory: services/example | |
env: | |
TF_WORKSPACE: 'prod' | |
run: | | |
terraform plan -no-color -input=false | |
terraform apply -no-color -input=false -auto-approve |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It should be noted that terraform is the main requirement here, the
terraform apply
step on 117/126 actually alter the running code.