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

Ensure Azure/cli action fail when executed on a non-Linux-based OS #123

Merged
merged 1 commit into from
Nov 24, 2023
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
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ export async function main() {
const CONTAINER_NAME = `MICROSOFT_AZURE_CLI_${getCurrentTime()}_CONTAINER`;
try {
if (process.env.RUNNER_OS != 'Linux') {
core.setFailed('Please use Linux based OS as a runner.');
Copy link
Member

Choose a reason for hiding this comment

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

Why doesn't core.setFailed cause failure?

Copy link
Member Author

@MoChilia MoChilia Nov 22, 2023

Choose a reason for hiding this comment

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

The description of core.setFailed is "Sets the action status to failed. When the action exits it will be with an exit code of 1.". See https://docs.github.com/en/actions/creating-actions/setting-exit-codes-for-actions.
However, in the previous code, it returned after setFailed.

cli/src/main.ts

Lines 18 to 20 in 1828f1c

if (process.env.RUNNER_OS != 'Linux') {
core.setFailed('Please use Linux based OS as a runner.');
return;

This would cause main() didn't catch the error.

cli/src/main.ts

Lines 77 to 80 in 1828f1c

catch (error) {
core.error(error);
throw error;
}

Then, main() exited with 0.

cli/src/entrypoint.ts

Lines 4 to 10 in 1828f1c

main()
.then(() => {
process.exit(0)
})
.catch((err: Error) => {
setFailed(err.message);
process.exit(1);

This is why core.setFailed doesn't cause failure in previous code.

Copy link
Member

Choose a reason for hiding this comment

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

According to Git Blame, this line was added as early as 9932b03. Later, 548c3ec added src/entrypoint.ts which overrides core.setFailed with

process.exit(0)

return;
throw new Error('Please use Linux-based OS as a runner.');
}

let inlineScript: string = core.getInput('inlineScript', { required: true });
Expand Down