Compare commits

..

4 Commits

Author SHA1 Message Date
CrazyMax
2e59ae7030 Merge pull request #53 from crazy-max/docker-io
Some checks failed
publish / publish (push) Has been cancelled
docker: check command using actions/io module
2023-02-21 08:46:20 +01:00
CrazyMax
99487d6986 docker: check command using actions/io module
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-21 08:40:27 +01:00
CrazyMax
3d9ec9f02d Merge pull request #52 from crazy-max/index
run function to handle GitHub Action main and post runs
2023-02-21 08:21:59 +01:00
CrazyMax
f2b1224b00 run function to handle GitHub Action main and post runs
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
2023-02-21 08:18:56 +01:00
7 changed files with 58 additions and 23 deletions

View File

@@ -39,7 +39,7 @@ $ npm install @docker/actions-toolkit
## Usage
```js
const { Toolkit } = require('@docker/actions-toolkit')
const { Toolkit } = require('@docker/actions-toolkit/lib/toolkit')
const toolkit = new Toolkit()
```

View File

@@ -16,6 +16,7 @@
import {afterEach, beforeEach, describe, expect, it, jest} from '@jest/globals';
import path from 'path';
import * as io from '@actions/io';
import osm = require('os');
import {Docker} from '../src/docker';
@@ -49,13 +50,10 @@ describe('configDir', () => {
describe('isAvailable', () => {
it('cli', async () => {
const execSpy = jest.spyOn(Exec, 'getExecOutput');
const ioWhichSpy = jest.spyOn(io, 'which');
await Docker.isAvailable();
// eslint-disable-next-line jest/no-standalone-expect
expect(execSpy).toHaveBeenCalledWith(`docker`, [], {
silent: true,
ignoreReturnCode: true
});
expect(ioWhichSpy).toHaveBeenCalledTimes(1);
expect(ioWhichSpy).toHaveBeenCalledWith('docker', true);
});
});

View File

@@ -41,7 +41,7 @@ module.exports = {
moduleNameMapper: {
'^csv-parse/sync': '<rootDir>/node_modules/csv-parse/dist/cjs/sync.cjs'
},
collectCoverageFrom: ['src/**/{!(toolkit.ts),}.ts'],
collectCoverageFrom: ['src/**/{!(index.ts),}.ts'],
coveragePathIgnorePatterns: ['lib/', 'node_modules/', '__mocks__/', '__tests__/'],
verbose: true
};

View File

@@ -28,8 +28,8 @@
"author": "Docker Inc.",
"license": "Apache-2.0",
"packageManager": "yarn@3.3.1",
"main": "lib/toolkit.js",
"types": "lib/toolkit.d.ts",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib",
"test": "__tests__"
@@ -46,6 +46,7 @@
"@actions/exec": "^1.1.1",
"@actions/github": "^5.1.1",
"@actions/http-client": "^2.0.1",
"@actions/io": "^1.1.2",
"@actions/tool-cache": "^2.0.1",
"csv-parse": "^5.3.5",
"jwt-decode": "^3.1.2",

View File

@@ -17,6 +17,7 @@
import os from 'os';
import path from 'path';
import * as core from '@actions/core';
import * as io from '@actions/io';
import {Exec} from './exec';
export class Docker {
@@ -25,24 +26,16 @@ export class Docker {
}
public static async isAvailable(): Promise<boolean> {
const ok: boolean = await Exec.getExecOutput('docker', [], {
ignoreReturnCode: true,
silent: true
})
return await io
.which('docker', true)
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.debug(`Docker.isAvailable cmd err: ${res.stderr}`);
return false;
}
return res.exitCode == 0;
core.debug(`Docker.isAvailable ok: ${res}`);
return true;
})
.catch(error => {
core.debug(`Docker.isAvailable error: ${error}`);
return false;
});
core.debug(`Docker.isAvailable: ${ok}`);
return ok;
}
public static async printVersion(): Promise<void> {

42
src/index.ts Normal file
View File

@@ -0,0 +1,42 @@
/**
* Copyright 2023 actions-toolkit authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as core from '@actions/core';
const isPost = !!process.env['STATE_isPost'];
if (!isPost) {
core.saveState('isPost', 'true');
}
/**
* Runs a GitHub Action.
* Output will be streamed to the live console.
*
* @param main runs the defined function.
* @param post runs the defined function at the end of the job if set.
* @returns Promise<void>
*/
export async function run(main: () => Promise<void>, post?: () => Promise<void>): Promise<void> {
if (!isPost) {
try {
await main();
} catch (e) {
core.setFailed(e.message);
}
} else if (post) {
await post();
}
}

View File

@@ -52,7 +52,7 @@ __metadata:
languageName: node
linkType: hard
"@actions/io@npm:^1.1.1":
"@actions/io@npm:^1.1.1, @actions/io@npm:^1.1.2":
version: 1.1.2
resolution: "@actions/io@npm:1.1.2"
checksum: 3c6583c4557abf6c95e9cfc9b6377045e65ba2c5dd4863f4feedd6be9daf4f6b60e588ab0151d5626b5f8320a37f05b8d44ab5c329b8c19f65be31b0616e1464
@@ -766,6 +766,7 @@ __metadata:
"@actions/exec": ^1.1.1
"@actions/github": ^5.1.1
"@actions/http-client": ^2.0.1
"@actions/io": ^1.1.2
"@actions/tool-cache": ^2.0.1
"@types/csv-parse": ^1.2.2
"@types/node": ^16.18.11