Skip to content

Commit

Permalink
Add caching + don't push :latest tag on feature branches
Browse files Browse the repository at this point in the history
  • Loading branch information
dbackeus committed Jun 7, 2024
1 parent d2bc17e commit 5dd2768
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
20 changes: 16 additions & 4 deletions .github/workflows/depot-build-and-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@ on: push

jobs:
docker:
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
permissions:
contents: read
pages: write
id-token: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- uses: depot/setup-action@v1

- uses: docker/login-action@v2
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- uses: depot/build-push-action@v1
- name: Build and push with sha + latest tag on master
if: github.ref == 'refs/heads/master'
uses: depot/build-push-action@v1
with:
project: b4qlt63xvg
platforms: linux/amd64,linux/arm64
push: true
tags: |
reclaimthestack/rails-example:latest
reclaimthestack/rails-example:sha-${{ github.sha }}
- name: Build and push with sha tag on feature branches
if: github.ref != 'refs/heads/master'
uses: depot/build-push-action@v1
with:
project: b4qlt63xvg
platforms: linux/amd64,linux/arm64
push: true
tags: |
reclaimthestack/rails-example:sha-${{ github.sha }}
30 changes: 20 additions & 10 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class PostsController < ApplicationController
before_action :set_post, only: %i[ show edit update destroy ]
before_action :enable_caching, only: %i[index show new edit]
skip_before_action :verify_authenticity_token

# GET /posts
def index
Expand All @@ -8,6 +9,7 @@ def index

# GET /posts/1
def show
@post = Post.find(params[:id])
end

# GET /posts/new
Expand All @@ -17,6 +19,7 @@ def new

# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end

# POST /posts
Expand All @@ -32,6 +35,8 @@ def create

# PATCH/PUT /posts/1
def update
@post = Post.find(params[:id])

if @post.update(post_params)
redirect_to @post, notice: "Post was successfully updated."
else
Expand All @@ -41,18 +46,23 @@ def update

# DELETE /posts/1
def destroy
@post.destroy!
post = Post.find(params[:id])

post.destroy!

redirect_to posts_url, notice: "Post was successfully destroyed.", status: :see_other
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end

# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:title, :body)
end
def post_params
params.require(:post).permit(:title, :body)
end

def enable_caching
# don't cache cookies (note: Cloudflare won't cache responses with cookies)
request.session_options[:skip] = true

expires_in 1.hour, public: true unless params[:nocache]
end
end

0 comments on commit 5dd2768

Please sign in to comment.