|
| 1 | +--- |
| 2 | +icon: material/bash |
| 3 | +title: Bash Scripts |
| 4 | +tags: |
| 5 | + - Bash |
| 6 | +--- |
| 7 | + |
| 8 | +<!-- markdownlint-disable single-h1 --> |
| 9 | +# :material-bash: Bash Scripts |
| 10 | +<!-- markdownlint-enable single-h1 --> |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +<!-- markdownlint-disable max-one-sentence-per-line --> |
| 15 | +!!! Tip |
| 16 | + If you're just looking for a complete example, |
| 17 | + [click here](https://github.com/input-output-hk/catalyst-ci/blob/master/Earthfile). |
| 18 | + This guide will provide detailed instructions for how the example was built. |
| 19 | +<!-- markdownlint-enable max-one-sentence-per-line --> |
| 20 | + |
| 21 | +This guide will get you started with using the Catalyst CI to build projects that include Bash scripts. |
| 22 | +By the end of the guide, we'll have an `Earthfile` that utilizes the Catalyst CI that can check your Bash scripts. |
| 23 | + |
| 24 | +Bash is not considered a stand alone target, although bash scripts are used extensively across multiple targets. |
| 25 | +The Bash support consists solely of a single repo wide `check` target which validates: |
| 26 | + |
| 27 | +* Are any of the `bash` shell scripts redundant. |
| 28 | + * This prevent maintenance issues where common scripts are copy/pasted rather than being properly organized. |
| 29 | +* Do the bash scripts pass `shellcheck` lints. |
| 30 | + * This forces us to follow a consistent style guide, and also checks for problematic Bash syntax. |
| 31 | + |
| 32 | +To begin, clone the Catalyst CI repository: |
| 33 | + |
| 34 | +## Adding Bash checks to your Repo that is already using Catalyst-CI |
| 35 | + |
| 36 | +Bash script checking is to be added to a repo that is already using Catalyst CI. |
| 37 | + |
| 38 | +All that needs to happen is the following be added to the `Earthfile` in the root of the repo. |
| 39 | + |
| 40 | +```Earthfile |
| 41 | +# Internal: shell-check - test all bash files against our shell check rules. |
| 42 | +shell-check: |
| 43 | + FROM alpine:3.18 |
| 44 | +
|
| 45 | + DO github.com/input-output-hk/catalyst-ci/earthly/bash:vx.y.z+SHELLCHECK --src=. |
| 46 | +
|
| 47 | +# check all repo wide checks are run from here |
| 48 | +check: |
| 49 | + FROM alpine:3.18 |
| 50 | +
|
| 51 | + # Lint all bash files. |
| 52 | + BUILD +shell-check |
| 53 | +
|
| 54 | +``` |
| 55 | + |
| 56 | +<!-- markdownlint-disable max-one-sentence-per-line --> |
| 57 | +!!! Note |
| 58 | + It is expected that there may be multiple repo level `checks`. |
| 59 | + This pattern shown above allows for this by separating the individual checks into their own targets. |
| 60 | + The `check` target then just executed `BUILD` once for each check. |
| 61 | +<!-- markdownlint-enable max-one-sentence-per-line --> |
| 62 | + |
| 63 | +### Common Scripts |
| 64 | + |
| 65 | +It is not a good practice to copy bash scripts with common functionality. |
| 66 | +Accordingly, the *Utility* target `./utilities/scripts+bash-scripts` exists to provide a central location for common scripts. |
| 67 | +These are used locally to this repo and may be used by other repos using catalyst-ci. |
| 68 | + |
| 69 | +These scripts are intended to be used inside Earthly builds, and not locally. |
| 70 | + |
| 71 | +A common pattern to include these common scripts is the following: |
| 72 | + |
| 73 | +```Earthfile |
| 74 | + # Copy our target specific scripts |
| 75 | + COPY --dir scripts /scripts |
| 76 | +
|
| 77 | + # Copy our common scripts so we can use them inside the container. |
| 78 | + DO ../../utilities/scripts+ADD_BASH_SCRIPTS |
| 79 | +``` |
| 80 | + |
| 81 | +<!-- markdownlint-disable max-one-sentence-per-line --> |
| 82 | +!!! Note |
| 83 | + Always source scripts using `source "/scripts/include/something.sh"`. |
| 84 | + This will ensure the scripts are properly located. |
| 85 | + bash has no concept of the directory a script is located and so relative |
| 86 | + source commands are unreliable. |
| 87 | +<!-- markdownlint-enable max-one-sentence-per-line --> |
| 88 | + |
| 89 | +<!-- markdownlint-disable max-one-sentence-per-line --> |
| 90 | +!!! Note |
| 91 | + This is just an example, and you would adapt it to your specific requirements. |
| 92 | +<!-- markdownlint-enable max-one-sentence-per-line --> |
| 93 | + |
| 94 | +### Running checks |
| 95 | + |
| 96 | +From the root of the repo, you can check all bash scripts within the repo by running: |
| 97 | + |
| 98 | +```sh |
| 99 | +earthly +check |
| 100 | +``` |
| 101 | + |
| 102 | +This will also run all other repo-wide checks that are in use. |
| 103 | + |
| 104 | +### Build and test |
| 105 | + |
| 106 | +Bash scripts should not have a `build` target. |
| 107 | +They can form part of the Build of other targets. |
| 108 | + |
| 109 | +### Releasing |
| 110 | + |
| 111 | +Bash scripts should not have a discreet `release` target. |
| 112 | +They can form part of the `release` of other targets. |
| 113 | + |
| 114 | +### Publishing |
| 115 | + |
| 116 | +Bash scripts should not have a discreet `publish` target. |
| 117 | +They can form part of the `publish` of other targets. |
| 118 | + |
| 119 | +## Conclusion |
| 120 | + |
| 121 | +You can see the final `Earthfile` [here](https://github.com/input-output-hk/catalyst-ci/blob/master/Earthfile). |
| 122 | + |
| 123 | +This `Earthfile` will check the quality of the Bash files within the Catalyst-CI repo. |
0 commit comments