1.12.0 • Published 8 months ago

codefresh-sdk v1.12.0

Weekly downloads
1,690
License
-
Repository
-
Last release
8 months ago

Codefresh-Sdk

Codefresh SDK built on openapi spec.

Install

yarn add codefresh-sdk

npm install codefresh-sdk

Initialize

const { Codefresh, Config } = require('codefresh-sdk');

// configure at creation phase
let sdk = new Codefresh(await Config.load());

// or postpone
sdk = new Codefresh();
const config = await Config.load();
sdk.configure(config);

Configure

Sdk requires apiKey to authenticate its requests.

Configuring from .cfconfig

// load from default path, using 'current-context'
// default path: $HOME/.cfconfig
sdk.configure(await Config.fromCodefreshConfig());


// load from path specified at $CFCONFIG, using 'current-context'
process.env.CFCONFIG = 'some/path/.cfconfig';
sdk.configure(await Config.fromCodefreshConfig());


// load from specific path, using 'current-context'
sdk.configure(await Config.fromCodefreshConfig({ configPath: 'some/path/.cfconfig' }));


// load from default path and specific context
// throws if context does not exist
sdk.configure(await Config.fromCodefreshConfig({ context: 'local' }));


// load from specific path and specific context
sdk.configure(await Config.fromCodefreshConfig({ configPath: 'some/path/.cfconfig', context: 'local' }));

Configuring using Config.load()

Loading flow:

1) if options.apiKey is provided (options.url is also available) - create config from it 2) else if $CF_API_KEY variable has apiKey - create config from it 3) else if options.configPath or $CFCONFIG is specified - load config from this path (same as Config.fromCodefreshConfig()) 4) otherwise load config from default path (same as Config.fromCodefreshConfig())

Examples:
// 1) from credentials
sdk.configure(await Config.load({
    url: 'http://local.codefresh.io',
    apiKey: 'API_KEY',
}));


// 2) from env
process.env.CF_API_KEY = 'API_KEY';
sdk.configure(await Config.load());


// 3) load from specific path, using 'current-context'
// same as Config.fromCodefreshConfig()
sdk.configure(await Config.load({ configPath: 'some/path/.cfconfig' }));


// 3) load from specific path and specific context
// same as Config.fromCodefreshConfig()
sdk.configure(await Config.load({ configPath: 'some/path/.cfconfig', context: 'local' }));


// 3) load from path specified at $CFCONFIG, using 'current-context'
// same as Config.fromCodefreshConfig()
process.env.CFCONFIG = 'some/path/.cfconfig';
sdk.configure(await Config.load());


// 4) load from default path and specific context
// same as Config.fromCodefreshConfig()
// default path: $HOME/.cfconfig
sdk.configure(await Config.load({ context: 'local' }));

Config options

sdk.configure(await Config.load({
    url: 'http://g.codefresh.io', // when loading not from config file
    apiKey: 'API_KEY', // when loading not from config file
    spec: {
        json: '{ "openapi": "v3" .... }' || { openapi: 'v3' }, // openapi spec here
        url: 'http://not.codefresh.io/api/openapi.json', // or url to load (default: https://g.codefresh.io/api/openapi.json)
    },
    request: {
        timeout: 2000, // request timeout
        headers: { 'x-useful-header': 'some useful data' }, // some custom headers
    },
}));

Manipulate .cfconfig file

// use config manager for manipulating .cfconfig
const manager = Config.manager();


if (!manager.isConfigLoaded()) {
    // load from default path: $HOME/.cfconfig
    await manager.loadConfig();

    // load from specific path: $HOME/.cfconfig
    await manager.loadConfig({ configFilePath: 'some/path/.cfconfig' });

    // second time config is loaded from cache if path didn't change
    await manager.loadConfig({ configFilePath: 'some/path/.cfconfig' });

    // or you can force load config
    await manager.loadConfig({ configFilePath: 'some/path/.cfconfig', forceLoad: true });
}


// creating context
const context = await manager.createContext({ apiKey: 'API_KEY', url: 'http://local.codefresh.io', name: 'test' });
manager.addContext(context);
await manager.persistConfig();


// setting current context
manager.setCurrentContext(context);
await manager.persistConfig();


// use context
manager.useContext('test');
await manager.persistConfig();


// remove context
manager.removeContext('test');
await manager.persistConfig();


// clear config
manager.clearConfig();
await manager.persistConfig();

Naming Conventions

Every sdk operation retrieves it's interface from openapi.json spec using x-sdk-interface field (see the docs)

Interface templates:

// template
sdk.<resource>.<operation>()

// example
sdk.images.list()
// template
sdk.<resource>.[<sub-resource>...].<operation>()

// example
sdk.helm.boards.list()
sdk.helm.repos.create(body)
sdk.triggers.events.get({ event })
// template
sdk.<resource>.<operation>({ <params> })

// examples
sdk.workflows.get({ id })
sdk.workflows.list({ limit, page, status, trigger, pipeline })
// template
sdk.<resource>.<operation>(body)

// examples
sdk.compositions.create(body)
// template
sdk.<resource>.<operation>({ <params> }, body)

// examples
sdk.repos.create({ context }, body)
sdk.triggers.create({ event, pipeline }, body)

Standard for CRUD operations:

1) create - example: sdk.pipelines.create(body) 2) get - example: sdk.pipelines.get({ name }) 3) list - example: sdk.pipelines.list() 4) update - example: sdk.pipelines.update({ name }, body) 5) patch - example: sdk.helm.sections.patch({ id }, body) 6) delete - example: sdk.pipelines.create(body)

Examples

Getting pipelines:
// get list by filter
const pipelines = sdk.pipelines.list({ label: 'some-label' });

// get one by name
const pip = sdk.pipelines.get({ name: 'some-pip' }); 
Creating pipelines:
const data = {
    version: "1.0",
    kind: "pipeline",
    metadata: {
        name: "test"
    },
    spec: {
        steps: {
            eslint: {
                title: "Do something...",
                image: "node:alpine",
                commands: ["node -v"]
            }
        }
    }
};

sdk.pipelines.create(data);
Updating pipelines:
const data = { /* same data */ };
sdk.pipelines.update({ name: 'some-pip' }, data);
Running pipelines:
const data = {
    branch: 'master',
    sha: '192993440506679',
    variables: {
        SOME_VAR: 'some-var'
    },
    options: {
        noCache: true,
        resetVolume: true
    }
};

sdk.pipelines.run({ name: 'some-pip' }, data);
1.12.0

8 months ago

1.11.3

10 months ago

1.11.2

1 year ago

1.11.1

1 year ago

1.11.0

2 years ago

1.9.24

2 years ago

1.10.0

2 years ago

1.9.23

3 years ago

1.9.22

3 years ago

1.9.21

3 years ago

1.9.20

3 years ago

1.9.19

3 years ago

1.9.18

3 years ago

1.9.17

3 years ago

1.9.16

3 years ago

1.9.15

3 years ago

1.9.14

4 years ago

1.9.13

4 years ago

1.9.12

4 years ago

1.9.11

4 years ago

1.9.10

4 years ago

1.9.9

4 years ago

1.9.8

4 years ago

1.9.7

4 years ago

1.9.6

4 years ago

1.9.5

4 years ago

1.9.4

4 years ago

1.9.3

4 years ago

1.9.2

4 years ago

1.9.1

4 years ago

1.9.0

4 years ago

1.8.6

4 years ago

1.8.5

4 years ago

1.8.4

4 years ago

1.8.2

4 years ago

1.8.3

4 years ago

1.8.1

4 years ago

1.8.0

4 years ago

1.7.1

4 years ago

1.7.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.9

4 years ago

1.4.8

4 years ago

1.4.7

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.2

4 years ago

1.1.0

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.1.0

5 years ago

0.0.4

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago