Skip to content

Commit

Permalink
Merge pull request #15 from upmaru/feature/skip-push-when-deployment-…
Browse files Browse the repository at this point in the history
…exists

When deployment already exists copy the archive path
  • Loading branch information
zacksiri authored Aug 13, 2024
2 parents 03b505f + b98499d commit 2f2d53f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 8 deletions.
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ runs:
uses: actions/cache@v4
with:
path: ~/.mix
key: ${{ runner.arch }}-alpine-${{ inputs.alpine }}-pakman-8.2.12
key: ${{ runner.arch }}-alpine-${{ inputs.alpine }}-pakman-develop

- name: Install Pakman
if: steps.cache-pakman.outputs.cache-hit != 'true'
run: |
mix local.rebar --force
mix local.hex --force
mix escript.install hex pakman 8.2.12 --force
mix escript.install github upmaru/pakman branch develop --force
shell: alpine.sh {0}
env:
MIX_ENV: prod
25 changes: 22 additions & 3 deletions lib/pakman/push.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,35 @@ defmodule Pakman.Push do
{:error, error} ->
raise Error, message: "[Pakman.Push] #{inspect(error)}"

{:ok, %{"attributes" => _}} ->
Logger.info("[Pakman.Push] Deployment already exists...")
{:ok, %{"attributes" => attributes}} ->
Logger.info(
"[Pakman.Push] Deployment already exists copying existing..."
)

{:ok, :already_exists}
copy(attributes, config)

_ ->
raise Error, message: "[Pakman.Push] Deployment creation failed..."
end
end

defp copy(%{"archive_path" => archive_path} = existing_deployment, config) do
with {:ok, token} <- Instellar.authenticate(),
{:ok, deployment_message, response} <-
Instellar.create_deployment(token, archive_path, config),
{:ok, configuration_message, _response} <-
Instellar.create_configuration(
token,
response["attributes"]["id"],
config
) do
print_deployment_message(deployment_message)
print_configuration_message(configuration_message)

{:ok, :copied}
end
end

def push_files(storage, archive, options) do
home = System.get_env("HOME")
sha = System.get_env("WORKFLOW_SHA") || System.get_env("GITHUB_SHA")
Expand Down
42 changes: 39 additions & 3 deletions test/pakman/push_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,47 @@ defmodule Pakman.PushTest do
Bypass.expect(bypass, "GET", "/publish/deployments/somesha", fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(200, Jason.encode!(%{data: %{attributes: %{id: 1}}}))
|> Plug.Conn.resp(
200,
Jason.encode!(%{
data: %{
attributes: %{
"id" => 1,
"archive_path" => "archives/some-uuid/packages.zip"
}
}
})
)
end)

Bypass.expect(bypass, "POST", "/publish/deployments", fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(
201,
Jason.encode!(%{
data: %{
attributes: %{
"id" => 1,
"archive_path" => "archives/some-uuid/packages.zip"
}
}
})
)
end)

assert {:ok, :already_exists} =
Push.perform(config: "test/fixtures/rails.yml")
Bypass.expect_once(
bypass,
"POST",
"/publish/deployments/1/configurations",
fn conn ->
conn
|> Plug.Conn.put_resp_header("content-type", "application/json")
|> Plug.Conn.resp(201, Jason.encode!(%{data: %{id: 1}}))
end
)

assert {:ok, :copied} = Push.perform(config: "test/fixtures/rails.yml")
end
end
end

0 comments on commit 2f2d53f

Please sign in to comment.