Skip to content

Commit

Permalink
👷 Proper caching strategy & builds versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
aliberts committed Dec 8, 2023
1 parent 705518d commit 9a112fc
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 12 deletions.
28 changes: 16 additions & 12 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
name: Deploy code to AWS lambda
name: Deploy Code

on:
push:
branches: [ dev ]
tags:
- "v*.*.*"

Expand All @@ -17,28 +18,29 @@ jobs:
POETRY_HOME: /home/runner/.local/bin/
POETRY: /home/runner/.local/bin/bin/poetry
PROJECT_NAME: inews
STAGE: ${{ github.ref_name == 'main' && 'prod' || 'dev' }}
STAGE: ${{ startsWith(github.ref, 'refs/tags/v') && 'prod' || github.ref_name == 'dev' && 'dev' }}
steps:
###############################################
# Checkout
###############################################
- name: Git clone the repository
uses: actions/checkout@v4


###############################################
# Export Requirements (cached)
###############################################
- name: Cache requirements
id: req-cache
uses: actions/cache@v3
uses: actions/cache/restore@v3
with:
path: ${{ github.workspace }}/requirements.txt
key: requirements-${{ hashFiles('poetry.lock') }}

- name: Cache Poetry installation
id: poetry-cache
if: steps.req-cache.outputs.cache-hit != 'true'
uses: actions/cache@v3
uses: actions/cache/restore@v3
with:
path: ${{ env.POETRY_HOME }}
key: poetry-${{ env.POETRY_VERSION }}-${{ env.POETRY_EXPORT_VERSION }}
Expand Down Expand Up @@ -74,9 +76,9 @@ jobs:
uses: aws-actions/amazon-ecr-login@v2


###############################################
# Build, Push & Deploy
###############################################
# ###############################################
# # Build (cached), Push & Deploy
# ###############################################
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

Expand All @@ -87,7 +89,9 @@ jobs:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
with:
images: ${{ env.ECR_REGISTRY }}/${{ env.PROJECT_NAME }}-${{ env.STAGE }}
tags: type=semver,pattern={{raw}}
tags: |
type=semver,pattern={{raw}},enable=${{ env.STAGE == 'prod' }}
type=raw,value=latest,enable=${{ env.STAGE == 'dev' }}
- name: Build and push
uses: docker/build-push-action@v5
Expand All @@ -97,14 +101,14 @@ jobs:
provenance: false
tags: ${{ steps.meta.outputs.tags }}
platforms: linux/amd64
cache-from: type=gha
cache-to: type=gha
cache-from: type=gha,scope=global

- name: Update Lambda code
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
IMAGE: ${{ env.PROJECT_NAME }}-${{ env.STAGE }}
VERSION: ${{ steps.meta.outputs.version }}
run: |
aws lambda update-function-code \
--function-name ${{ env.PROJECT_NAME }}-${{ env.STAGE }} \
--image-uri $ECR_REGISTRY/${{ env.PROJECT_NAME }}-${{ env.STAGE }}:${{github.ref_name}} \
> /dev/null
--image-uri ${{ env.ECR_REGISTRY }}/${{ env.IMAGE }}:${{ env.VERSION }} > /dev/null
81 changes: 81 additions & 0 deletions .github/workflows/update_caches.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
name: Update Caches

on:
push:
branches: [ main ]

permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout
jobs:
UpdateCaches:
runs-on: ubuntu-latest
env:
POETRY_VERSION: 1.7.1
POETRY_EXPORT_VERSION: 1.6.0
POETRY_HOME: /home/runner/.local/bin/
POETRY: /home/runner/.local/bin/bin/poetry
PROJECT_NAME: inews
steps:
###############################################
# Checkout
###############################################
- name: Git clone the repository
uses: actions/checkout@v4

###############################################
# Cache Poetry & Requirements
###############################################
- name: Cache requirements
id: req-cache
uses: actions/cache@v3
with:
path: ${{ github.workspace }}/requirements.txt
key: requirements-${{ hashFiles('poetry.lock') }}

- name: Cache Poetry installation
id: poetry-cache
if: steps.req-cache.outputs.cache-hit != 'true'
uses: actions/cache@v3
with:
path: ${{ env.POETRY_HOME }}
key: poetry-${{ env.POETRY_VERSION }}-${{ env.POETRY_EXPORT_VERSION }}

- name: Install Poetry
if: |
steps.req-cache.outputs.cache-hit != 'true' &&
steps.poetry-cache.outputs.cache-hit != 'true'
run: |
python3 -m venv $POETRY_HOME
$POETRY_HOME/bin/pip install poetry==$POETRY_VERSION
$POETRY self add poetry-plugin-export==$POETRY_EXPORT_VERSION
- name: Export requirements.txt
if: steps.req-cache.outputs.cache-hit != 'true'
run: |
$POETRY config warnings.export false
$POETRY export -f requirements.txt --output requirements.txt --without-hashes
###############################################
# Cache Image Build
###############################################
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.PROJECT_NAME }}

- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: false
provenance: false
tags: ${{ steps.meta.outputs.tags }}
platforms: linux/amd64
cache-from: type=gha,scope=global
cache-to: type=gha,mode=max,scope=global

0 comments on commit 9a112fc

Please sign in to comment.