Skip to content

Commit

Permalink
Merge pull request #28 from optimumBA/optimize-caching
Browse files Browse the repository at this point in the history
Reduce cache saving time
  • Loading branch information
almirsarajcic authored Nov 29, 2024
2 parents c24910b + 15e3353 commit 0df8f70
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 63 deletions.
67 changes: 46 additions & 21 deletions .github/github_workflows.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@ defmodule GithubWorkflows do
See https://hexdocs.pm/github_workflows_generator.
"""

# App and environment configuration
@app_name_prefix "phx-tools"
@environment_name "pr-${{ github.event.number }}"
@preview_app_name "#{@app_name_prefix}-#{@environment_name}"
@preview_app_host "#{@preview_app_name}.fly.dev"
@repo_name "phx_tools"

# Test configuration
@shells ["bash", "fish", "zsh"]

# Cache configuration
@cache_key_prefix_prefix "${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}"
@mix_cache_key_prefix "#{@cache_key_prefix_prefix}-mix"
@mix_cache_path ~S"""
_build
deps
"""
@plt_cache_key_prefix "#{@cache_key_prefix_prefix}-plt"
@plt_cache_path "priv/plts"

def get do
%{
"main.yml" => main_workflow(),
Expand Down Expand Up @@ -100,6 +113,11 @@ defmodule GithubWorkflows do
name: "Compile",
env: [MIX_ENV: "test"],
run: "mix compile"
],
[
name: "Save dependencies cache",
uses: "actions/cache/save@v4",
with: save_cache_opts(@mix_cache_key_prefix, @mix_cache_path)
]
]
)
Expand Down Expand Up @@ -219,19 +237,13 @@ defmodule GithubWorkflows do
end

defp dialyzer_job do
cache_key_prefix =
"${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-plt"

elixir_job("Dialyzer",
needs: :compile,
steps: [
[
name: "Restore PLT cache",
uses: "actions/cache@v3",
with:
[
path: "priv/plts"
] ++ cache_opts(cache_key_prefix)
uses: "actions/cache/restore@v4",
with: cache_opts(@plt_cache_key_prefix, @plt_cache_path)
],
[
name: "Create PLTs",
Expand All @@ -242,6 +254,11 @@ defmodule GithubWorkflows do
name: "Run dialyzer",
env: [MIX_ENV: "test"],
run: "mix dialyzer"
],
[
name: "Save PLT cache",
uses: "actions/cache/save@v4",
with: save_cache_opts(@plt_cache_key_prefix, @plt_cache_path)
]
]
)
Expand All @@ -251,9 +268,6 @@ defmodule GithubWorkflows do
needs = Keyword.get(opts, :needs)
steps = Keyword.get(opts, :steps, [])

cache_key_prefix =
"${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix"

job = [
name: name,
"runs-on": "ubuntu-latest",
Expand All @@ -270,14 +284,9 @@ defmodule GithubWorkflows do
]
],
[
uses: "actions/cache@v3",
with:
[
path: ~S"""
_build
deps
"""
] ++ cache_opts(cache_key_prefix)
name: "Restore dependencies cache",
uses: "actions/cache/restore@v4",
with: cache_opts(@mix_cache_key_prefix, @mix_cache_path)
]
] ++ steps
]
Expand Down Expand Up @@ -323,7 +332,7 @@ defmodule GithubWorkflows do
checkout_step(),
[
name: "Restore npm cache",
uses: "actions/cache@v3",
uses: "actions/cache/restore@v4",
id: "npm-cache",
with: [
path: "node_modules",
Expand All @@ -338,6 +347,14 @@ defmodule GithubWorkflows do
[
name: "Run Prettier",
run: "npx prettier -c ."
],
[
name: "Save npm cache",
uses: "actions/cache/save@v4",
with: [
path: "node_modules",
key: "${{ runner.os }}-prettier"
]
]
]
]
Expand Down Expand Up @@ -521,15 +538,23 @@ defmodule GithubWorkflows do
]
end

defp cache_opts(prefix) do
defp cache_opts(prefix, path) do
[
key: "#{prefix}-${{ github.sha }}",
path: path,
"restore-keys": ~s"""
#{prefix}-
"""
]
end

defp save_cache_opts(prefix, path) do
[
key: "#{prefix}-${{ github.sha }}",
path: path
]
end

defp fly_env do
[
FLY_API_TOKEN: "${{ secrets.FLY_API_TOKEN }}",
Expand Down
68 changes: 47 additions & 21 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Install Elixir dependencies
Expand All @@ -34,6 +35,13 @@ jobs:
env:
MIX_ENV: test
run: mix compile
- name: Save dependencies cache
uses: actions/cache/save@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
credo:
needs: compile
name: Credo
Expand All @@ -47,12 +55,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Check code style
Expand All @@ -72,12 +81,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Check for vulnerable Mix dependencies
Expand All @@ -97,19 +107,20 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Restore PLT cache
uses: actions/cache@v3
uses: actions/cache/restore@v4
with:
path: priv/plts
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-plt-${{ github.sha }}
path: priv/plts
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-plt-
- name: Create PLTs
Expand All @@ -120,6 +131,11 @@ jobs:
env:
MIX_ENV: test
run: mix dialyzer
- name: Save PLT cache
uses: actions/cache/save@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-plt-${{ github.sha }}
path: priv/plts
format:
needs: compile
name: Format
Expand All @@ -133,12 +149,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Check Elixir formatting
Expand All @@ -158,12 +175,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Check for retired Hex packages
Expand All @@ -177,7 +195,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Restore npm cache
uses: actions/cache@v3
uses: actions/cache/restore@v4
id: npm-cache
with:
path: node_modules
Expand All @@ -187,6 +205,11 @@ jobs:
run: npm i -D prettier prettier-plugin-toml
- name: Run Prettier
run: npx prettier -c .
- name: Save npm cache
uses: actions/cache/save@v4
with:
path: node_modules
key: ${{ runner.os }}-prettier
sobelow:
needs: compile
name: Security check
Expand All @@ -200,12 +223,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Check for security issues using sobelow
Expand All @@ -225,12 +249,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Run tests
Expand All @@ -250,12 +275,13 @@ jobs:
with:
version-file: .tool-versions
version-type: strict
- uses: actions/cache@v3
- name: Restore dependencies cache
uses: actions/cache/restore@v4
with:
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
path: |
_build
deps
key: ${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-${{ github.sha }}
restore-keys: |
${{ runner.os }}-${{ steps.setup-beam.outputs.elixir-version }}-${{ steps.setup-beam.outputs.otp-version }}-mix-
- name: Check for unused Mix dependencies
Expand Down
Loading

0 comments on commit 0df8f70

Please sign in to comment.