Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an output locked #135

Merged
merged 5 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ steps:
mode: check
key: foo
- run: bash deploy.sh foo
if: steps.check.outputs.already_locked != 'true'
if: steps.check.outputs.locked != 'true'
```

You can also use `post_unlock: "true"` to release a lock automatically in a post step.
Expand Down Expand Up @@ -116,6 +116,37 @@ These inputs are also available for `mode: check`.
wait_interval_seconds: "5"
```

[#131](https://github.com/suzuki-shunsuke/lock-action/issues/131) [#135](https://github.com/suzuki-shunsuke/lock-action/pull/135) [>= v0.1.4](https://github.com/suzuki-shunsuke/lock-action/releases/tag/v0.1.4) You can check if this action can acquire the lock in later jobs using the output `locked`.
The output is useful if you want to release a lock in a later job.

```yaml
lock:
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
locked: ${{steps.lock.outputs.locked}}
steps:
- uses: suzuki-shunsuke/lock-action@c752be910ac812e0adc50316855416514d364b57 # v0.1.3
id: lock
with:
mode: lock
key: dev
# ...

unlock:
runs-on: ubuntu-latest
needs: lock
permissions:
contents: write
steps:
- uses: suzuki-shunsuke/lock-action@c752be910ac812e0adc50316855416514d364b57 # v0.1.3
if: needs.lock.outputs.locked == 'true'
with:
mode: unlock
key: dev
```

## Available versions

> [!CAUTION]
Expand Down
8 changes: 7 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ inputs:
default: "false"
required: false
outputs:
locked:
description: Whether the key is locked
already_locked:
description: Whether the key is already locked
description: |
Whether the key is already locked.
The difference between `already_locked` is that if `mode` is `lock` and the lock has already been acquired, `already_locked` is `true` but `locked` is `false`.
And if the lock hasn't been acquired and the action can acquire a lock, `already_locked` is `false` but `locked` is `true`.
If `mode` is `check`, there is no difference between `already_locked` and `locked`.
result:
description: The lock result
runs:
Expand Down
1 change: 1 addition & 0 deletions src/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const check = async (input: lib.Input) => {
core.setOutput("result", s);
core.info(`result: ${s}`);
const alreadyLocked = metadata?.state === "lock";
core.setOutput("locked", alreadyLocked);
core.setOutput("already_locked", alreadyLocked);
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, I want to remove already_locked if mode is check, but it's a breaking change.

core.info(`already_locked: ${alreadyLocked}`);
if (alreadyLocked && input.failIfLocked) {
Expand Down
4 changes: 4 additions & 0 deletions src/lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ export const lock = async (input: lib.Input) => {
switch (result) {
case Result.AlreadyLocked:
core.setOutput("already_locked", true);
core.setOutput("locked", false);
if (input.ignoreAlreadyLockedError) {
return;
}
core.error(`The key ${input.key} has already been locked`);
throw new Error(`The key ${input.key} has already been locked`);
case Result.Locked:
core.info(`The key ${input.key} has been locked`);
core.setOutput("already_locked", false);
core.setOutput("locked", true);
core.saveState(`got_lock`, true);
return;
case Result.FailedToGetLock:
core.setOutput("locked", false);
core.error(
`Failed to acquire lock. Probably the key ${input.key} has already been locked`,
);
Expand Down
Loading