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

Refactor namespaced variable names, add log and loader modules #32

Merged
Merged
42 changes: 30 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,24 @@ file [here](examples/unit_test_example.sh).
| smoke_test.sh | **DEPRECATED**, use [cji_smoke_test.sh](cji_smoke_test.sh) |
| iqe_pod | **DEPRECATED**, use [cji_smoke_test.sh](cji_smoke_test.sh) |

## Bash script helper scripts usage
## Bash script library usage

The collection of helper libraries are expected to be loaded using the
provided [src/bootstrap.sh](bootstrap) script.

Currently, there are 2 supported modules. To read more about what each of the modules provide,
please read through the documents linked through the library IDs in the following table:
The [src/bootstrap.sh](/src/bootstrap.sh) script pulls a local copy of this repo and initializes
the loader module, which serves as an entrypoint into the library. The loader module provides
the ` cicd::loader::load_module` functions that enable loading different modules.

| Library ID | Description |
|-------------------------------------------------------|----------------------------------------------------------------------------|
| [container](docs/cicd_tools/container_lib.md) | Provides wrapper functions for invoking container engine agnostic commands |
| [image_builder](docs/cicd_tools/image_builder_lib.md) | Provides helper functions to simplify the image building process |
See the table below for information on the modules:

| Library ID | Description |
|---------------------------------------------------|----------------------------------------------------|
| [container](docs/cicd_tools/container.md) | container engine agnostic commands |
| [image_builder](docs/cicd_tools/image_builder.md) | Simplify image building process |
| [common](docs/cicd_tools/common.md) | Generic helper functions shared across all modules |
| [log](docs/cicd_tools/log.md) | logging tools |
| [loader](docs/cicd_tools/loader.md) | Module loading functions |

### How to use the helper libraries

Expand All @@ -46,12 +52,23 @@ centralized way. This should be helpful to reduce the amount of code needed to w
operations in a pipeline for routine tasks, such as operating with containers or building container
images.

The [src/main.sh](main.sh) script is the main entrypoint and should be used to load the modules
The [src/load_module.sh](load_module.sh) script is the main entrypoint and should be used to load
the modules
included in this library. This script requires all the other scripts available in a local directory
following the same structure in this repository.

To use any of the provided libraries, you must source the [src/main.sh](main.sh) script
and pass the unique library ID to be loaded as a parameter.
To use any of the provided libraries, you must source the [src/load_module.sh](load_module.sh)
script and pass the unique library ID to be loaded as a parameter. For example:

```
module_id='container'
source src/load_module.sh "$module_id"


$ cicd::container::cmd --version

podman version 4.7.0
```

There's two different approaches for loading these scripts, depending on if you're a contributor or
an end user.
Expand All @@ -61,13 +78,14 @@ an end user.
This is the intended way when developing new modules for this library. The recommended approach for
contributing is to create a new fork and then open a pull request against the `main` branch.

When working with a local copy of the repository, you should source the [src/main.sh](main.sh)
When working with a local copy of the repository, you should source
the [src/load_module.sh](load_module.sh)
script directly.

#### Using the library from other scripts

There is an existing helper script named [src/bootstrap.sh](bootstrap) to help with sourcing the
[src/main.sh](main.sh) script if you're not contributing to this repo.
[src/load_module.sh](load_module.sh) script if you're not contributing to this repo.

**This is the intended way of using this library from external projects**.

Expand Down
49 changes: 49 additions & 0 deletions docs/cicd_tools/common.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Common module

Helper functions that are shared and used by other modules

## Definition

The module ID is `common`

All functions exposed by this module will use the namespaced prefix:

```
cicd::common::
```

## Usage

Use the `common` id to load the module

```
CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh"
# shellcheck source=/dev/null
source <(curl -sSL "$CICD_TOOLS_URL") common
```

## Dependencies

This module requires one of the supported container engines to be present in the session's `PATH`.
The currently supported container engines are:

- `docker`
- `podman`

### Public functions

#### cicd::common::command_is_present

returns 0 (true) if the command passed as a parameter is found in the session's PATH

#### cicd::common::get_7_chars_commit_hash

Returns the first 7 characters from the latest commit of current directory's git HEAD

#### cicd::common::is_ci_context

Returns 0 (true) if the current context is considered a CI context (i.e - running in a job on a CI
system)



36 changes: 19 additions & 17 deletions docs/cicd_tools/container_lib.md → docs/cicd_tools/container.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,10 @@ All functions exposed by this module will use the namespaced prefix:
cicd::container::
```

## Dependencies

None

### Public functions

#### cicd::container::cmd

Should be used instead of a container engine command to support container-engine agnostic commands (among the supported container engines)

## How to use
## Usage

Use the `container` id to load the module
Use the `container` id to load the module via the `loader` module, using either the `load_module.sh`
or the `bootstrap.sh` script

```
CICD_TOOLS_URL="https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh"
Expand All @@ -42,10 +33,6 @@ cicd::container::cmd
which serves as a wrapper to the container engine of choice. You should be able to safely replace
your invocations to `docker` or `podman` commands with this function

### Supported container engines

We currently support `docker` and `podman`

### Container engine detection and order choice

The library favors `podman` in case both container engines are available in the user's PATH.
Expand All @@ -57,7 +44,7 @@ the `CICD_TOOLS_CONTAINER_PREFER_ENGINE` to one of the supported container engin
*before** loading the library.

The container engine is selected when the library is loaded and is not possible to update it
afterwards.
afterward.

```
export CICD_TOOLS_CONTAINER_PREFER_ENGINE=docker
Expand All @@ -66,3 +53,18 @@ export CICD_TOOLS_CONTAINER_PREFER_ENGINE=docker
**Please note:** If you set your preference to be `docker` and the library detects that `docker` is
actually mocked as a wrapper to `podman` the library will ignore the preference and will try to
set `podman` as the selected container engine.

## Dependencies

This module requires one of the supported container engines to be present in the session's `PATH`.
The currently supported container engines are:

- `docker`
- `podman`

### Public functions

#### cicd::container::cmd

Should be used instead of a container engine command to support container-engine agnostic commands (
among the supported container engines)
Loading