├── .env ├── .github └── workflows │ └── ci.yml ├── Dockerfile ├── LICENSE ├── README.md ├── action.yml ├── entrypoint.sh └── src ├── ensure.sh ├── github.sh ├── github_actions.sh ├── main.sh └── misc.sh /.env: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/CodelyTV/bash-github_actions-skeleton/1234d4ff4d1ce62b147fc01ab490bc4ff25987c9/.env -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: ci 2 | 3 | on: [pull_request] 4 | 5 | jobs: 6 | build: 7 | runs-on: ubuntu-latest 8 | name: Ensure this project runs 9 | steps: 10 | - uses: actions/checkout@v1 11 | - uses: ./ 12 | with: 13 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 14 | another_input: "whatever" 15 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:3.11 2 | 3 | RUN apk add --no-cache bash curl jq 4 | 5 | ADD entrypoint.sh /entrypoint.sh 6 | ADD src /src 7 | 8 | ENTRYPOINT ["/entrypoint.sh"] 9 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) 2020 Codely Enseña y Entretiene SL. https://codely.tv 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |

6 | 7 |

8 | 💻 Bash GitHub Actions Skeleton 9 |

10 | 11 |

12 | codely.tv 13 | CodelyTV Courses 14 |

15 | 16 |

17 | Speedup your GitHub Actions creation! 18 |

19 | 20 | ## 🚀 Usage 21 | 22 | Create a file named `whatever_name.yml` inside the `.github/workflows` directory and paste: 23 | 24 | ```yml 25 | name: Bash GH Skeleton 26 | 27 | on: [pull_request] 28 | 29 | jobs: 30 | bash-gh-skeleton: 31 | runs-on: ubuntu-latest 32 | name: Whatever this action does 33 | steps: 34 | - uses: codelytv/buash-github_action-skeleton@v1 35 | with: 36 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 37 | another_input: Some value 38 | ``` 39 | 40 | ## ⚖️ License 41 | 42 | [MIT](LICENSE) 43 | -------------------------------------------------------------------------------- /action.yml: -------------------------------------------------------------------------------- 1 | name: 'The name of your action' 2 | description: 'The description of the action' 3 | branding: 4 | icon: 'alert-circle' 5 | color: 'gray-dark' 6 | inputs: 7 | GITHUB_TOKEN: 8 | description: 'GitHub token' 9 | required: true 10 | another_input: 11 | description: 'A demo argument' 12 | required: true 13 | runs: 14 | using: 'docker' 15 | image: 'Dockerfile' 16 | args: 17 | - ${{ inputs.GITHUB_TOKEN }} 18 | - ${{ inputs.another_input }} 19 | 20 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | set -euo pipefail 3 | 4 | PROJECT_HOME="$(cd "$(dirname "${BASH_SOURCE[0]}")" >/dev/null 2>&1 && pwd)" 5 | 6 | if [ "$PROJECT_HOME" == "/" ]; then 7 | PROJECT_HOME="" 8 | fi 9 | 10 | export PROJECT_HOME 11 | 12 | source "$PROJECT_HOME/src/main.sh" 13 | 14 | main "$@" 15 | 16 | exit $? 17 | -------------------------------------------------------------------------------- /src/ensure.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ensure::env_variable_exist() { 4 | if [[ -z "${!1}" ]]; then 5 | echoerr "The env variable $1 is required." 6 | exit 1 7 | fi 8 | } 9 | 10 | ensure::total_args() { 11 | local -r received_args=$(( $# - 1 )) 12 | local -r expected_args=$1 13 | 14 | if ((received_args != expected_args)); then 15 | echoerr "Illegal number of parameters, $expected_args expected but $received_args found" 16 | exit 1 17 | fi 18 | } 19 | -------------------------------------------------------------------------------- /src/github.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | GITHUB_API_URI="https://api.github.com" 4 | GITHUB_API_HEADER="Accept: application/vnd.github.v3+json" 5 | 6 | github::get_commit_modified_files() { 7 | local -r commit_ref=$1 8 | 9 | curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/commits/$commit_ref" | jq .files | jq -r ".[] | .filename" 10 | # local -r last_commit=$(curl -sSL -H "Authorization: token $GITHUB_TOKEN" -H "$GITHUB_API_HEADER" "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/pulls/$pr_number/commits" | jq .files | jq -r ".[] | .filename") 11 | } 12 | 13 | github::comment_pr() { 14 | local -r comment=$2 15 | local -r pr_number=$1 16 | 17 | curl -sSL \ 18 | -H "Authorization: token $GITHUB_TOKEN" \ 19 | -H "$GITHUB_API_HEADER" \ 20 | -X POST \ 21 | -H "Content-Type: application/json" \ 22 | -d "{\"body\":\"$comment\"}" \ 23 | "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/issues/$pr_number/comments" 24 | } 25 | 26 | github::comment_commit() { 27 | local -r comment=$2 28 | local -r commit_sha=$1 29 | 30 | curl -sSL \ 31 | -H "Authorization: token $GITHUB_TOKEN" \ 32 | -H "$GITHUB_API_HEADER" \ 33 | -X POST \ 34 | -H "Content-Type: application/json" \ 35 | -d "{\"body\":\"$comment\"}" \ 36 | "$GITHUB_API_URI/repos/$GITHUB_REPOSITORY/commits/$commit_sha/comments" 37 | } 38 | -------------------------------------------------------------------------------- /src/github_actions.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | github_actions::get_pr_number() { 4 | jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH" 5 | } 6 | 7 | github_actions::commit_sha() { 8 | jq --raw-output .after "$GITHUB_EVENT_PATH" 9 | } 10 | 11 | github_actions::print_all_data() { 12 | cat "$GITHUB_EVENT_PATH" 13 | } 14 | -------------------------------------------------------------------------------- /src/main.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | source "$PROJECT_HOME/src/ensure.sh" 4 | source "$PROJECT_HOME/src/github.sh" 5 | source "$PROJECT_HOME/src/github_actions.sh" 6 | source "$PROJECT_HOME/src/misc.sh" 7 | 8 | main() { 9 | ensure::env_variable_exist "GITHUB_REPOSITORY" 10 | ensure::env_variable_exist "GITHUB_EVENT_PATH" 11 | ensure::total_args 2 "$@" 12 | 13 | export GITHUB_TOKEN="$1" 14 | 15 | exit $? 16 | } 17 | -------------------------------------------------------------------------------- /src/misc.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | echoerr() { 4 | echo "$@" 1>&2 5 | } 6 | 7 | log::message() { 8 | echo "--------------" 9 | echo "$@" 10 | } 11 | 12 | coll::join_by() { 13 | local IFS="$1" 14 | shift 15 | echo "$*" 16 | } 17 | 18 | coll::map() { 19 | local -r fn="$1" 20 | 21 | for x in $(cat); do 22 | "$fn" "$x" 23 | done 24 | } 25 | 26 | coll::map_2() { 27 | local -r fn="$1" 28 | local -r arg="$2" 29 | 30 | for x in $(cat); do 31 | "$fn" "$arg" "$x" 32 | done 33 | } 34 | 35 | str::contains() { 36 | echo "$1" | grep "$2" || true 37 | } 38 | 39 | html::enlist() { 40 | echo "
  • $1
  • " 41 | } 42 | --------------------------------------------------------------------------------