diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index dad62d9..0000000 Binary files a/.DS_Store and /dev/null differ diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index 065e5a8..0000000 --- a/.github/main.workflow +++ /dev/null @@ -1,13 +0,0 @@ -workflow "pipeline runner" { - on = "push" - resolves = "run pipeline" -} - -action "run pipeline" { - uses = "./" - env = { - TRIGGER_NAME = "codefresh-trigger" - PIPELINE_NAME = "codefresh-pipeline" - } - secrets = ["CF_API_KEY"] -} diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..1f441b6 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,16 @@ +name: run codefresh pipeline +on: push +jobs: + build: + runs-on: ubuntu-22.04 + steps: + - name: Checkout + uses: actions/checkout@master + + - name: 'run pipeline' + uses: ./ + env: + PIPELINE_NAME: 'codefresh-pipeline' + TRIGGER_NAME: 'codefresh-trigger' + CF_API_KEY: ${{ secrets.CF_API_KEY }} + id: run-pipeline diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..e948fcb --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,42 @@ +# Contributing to codefresh-pipeline-runner +We love your input! We want to make contributing to this project as easy and transparent as possible, whether it's: + +- Reporting a bug +- Discussing the current state of the code +- Submitting a fix +- Proposing new features +- Becoming a maintainer + +## We Develop with Github +We use github to host code, to track issues and feature requests, as well as accept pull requests. + +## We Use [Github Flow](https://guides.github.com/introduction/flow/index.html), So All Code Changes Happen Through Pull Requests +Pull requests are the best way to propose changes to the codebase (we use [Github Flow](https://guides.github.com/introduction/flow/index.html)). We actively welcome your pull requests: + +1. Fork the repo and create your branch from `master`. +2. If you've added code that should be tested, add tests. +3. If you've changed APIs, update the documentation. +4. Ensure the test suite passes. +5. Make sure your code lints. +6. Issue that pull request! + +## Any contributions you make will be under the MIT Software License +In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project. Feel free to contact the maintainers if that's a concern. + +## Report bugs using Github's [issues](https://github.com/briandk/transcriptase-atom/issues) +We use GitHub issues to track public bugs. Report a bug by [opening a new issue](); it's that easy! + +## Write bug reports with detail, background, and sample code +[This is an example](http://stackoverflow.com/q/12488905/180626) of a bug report I wrote, and I think it's not a bad model. Here's [another example from Craig Hockenberry](http://www.openradar.me/11905408), an app developer whom I greatly respect. + +**Great Bug Reports** tend to have: + +- A quick summary and/or background +- Steps to reproduce + - Be specific! +- What you expected would happen +- What actually happens +- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work) + +## License +By contributing, you agree that your contributions will be licensed under its MIT License. \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 1e5d7db..886403e 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,14 +1,5 @@ FROM codefresh/cli -LABEL "com.github.actions.name"="codefresh-pipeline-runner" -LABEL "com.github.actions.description"="allows to run custom codefresh pipelines" -LABEL "com.github.actions.icon"="play-circle" -LABEL "com.github.actions.color"="green" - -LABEL "repository"="https://github.com/codefresh-io/codefresh-pipeline-runner" -LABEL "homepage"="https://github.com/codefresh-io/codefresh-pipeline-runner" -LABEL "maintainer"="" - ADD runner-entrypoint.sh /runner-entrypoint.sh -ENTRYPOINT ["/runner-entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/runner-entrypoint.sh"] diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..806e0f2 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2019 Codefresh + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index 5acc5a6..03c55f6 100644 --- a/README.md +++ b/README.md @@ -1,56 +1,69 @@ -# Integrating Codefresh pipelines with Github actions +# Codefresh pipeline runner GitHub Action -Github actions are a flexible way to respond to Github events and perform one or more tasks -when a specific Github event happens. Github actions can also use Codefresh pipeline as a back-end -resulting in a very powerful combination where the first action starts from Github, but Codefresh takes care -of the actual compilation or deployment in a pipeline. +Codefresh is a CI/CD platform that engineers actually love to use. The [Codefresh pipeline](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/pipelines/) runner GitHub action will trigger an existing Codefresh pipeline from a GitHub action. + +It is based on the [Codefresh CLI](https://codefresh-io.github.io/cli/) which can execute Codefresh pipelines remotely (using an API key for authentication). The Codefresh CLI is already available as a [public Docker image](https://hub.docker.com/r/codefresh/cli/), so creating a GitHub action with it is a trivial process. +## Integrating Codefresh pipelines with GitHub actions -## Codefresh Github action +GitHub actions are a flexible way to respond to GitHub events and perform one or more tasks +when a specific GitHub event happens. GitHub actions can also use Codefresh pipelines as a back-end +resulting in a very powerful combination where the first action starts from GitHub, but Codefresh takes care +of the actual compilation or deployment in a pipeline. -This repository holds a Github action that can be used to call existing [Codefresh pipelines](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/pipelines/). It is based on the [Codefresh CLI](https://codefresh-io.github.io/cli/) which can execute Codefresh pipelines remotely (using an API key for authentication). The Codefresh CLI is already available as a [public Docker image](https://hub.docker.com/r/codefresh/cli/), so creating a Github action with it is a trivial process. + ## Prerequisites Make sure that you have -* a Github account with Actions enabled +* a GitHub account with Actions enabled * a [Codefresh account](https://codefresh.io/docs/docs/getting-started/create-a-codefresh-account/) with one or more existing pipelines ready -* a [Codefresh API token](https://codefresh.io/docs/docs/integrations/codefresh-api/#authentication-instructions) that will be used as a secret in the Github action +* a [Codefresh API token](https://codefresh.io/docs/docs/integrations/codefresh-api/#authentication-instructions) that will be used as a secret in the GitHub action ## How the Codefresh action works -The Github workflow is placed on the [push event](https://developer.github.com/v3/activity/events/types/#pushevent) and therefore starts whenever a Git commit happens. The Workflow has a single action that starts the Codefresh pipeline runner. +The GitHub workflow is placed on the [push event](https://developer.github.com/v3/activity/events/types/#pushevent) and therefore starts whenever a Git commit happens. The Workflow has a single action that starts the Codefresh pipeline runner. The pipeline runner is a Docker image with the Codefresh CLI. It uses the Codefresh API token to authenticate to Codefresh and then calls a an existing pipeline via its trigger. The result is that all the details from the Git push (i.e. the GIT hash) are transferred to the Codefresh pipeline that gets triggered remotely -## How to use the Codefresh Github action - -In order to use the Github action, fork this repository and then navigate to the "Actions" tab in Github. Click on the "View main.workflow" button on the right hand side. Make sure that an actual branch is selected on the top left of the window. Then click the "Edit this file" button in the main workarea (exactly as you would edit a normal git file via Github) +## How to use the Codefresh GitHub action -Select the pipeline runner action and click the "Edit" button. On the right side panel enter the following +An example of workflow -* A secret with name `CF_API_KEY` and value your Codefresh API token +``` +name: run codefresh pipeline +on: push +jobs: + build: + runs-on: ubuntu-18.04 + steps: + - name: Checkout + uses: actions/checkout@master + + - name: 'run pipeline' + uses: codefresh-io/codefresh-pipeline-runner@v8 + with: + args: '-v key1=value1 -v key2=value2' + env: + PIPELINE_NAME: 'codefresh-pipeline' + TRIGGER_NAME: 'codefresh-trigger' + CF_API_KEY: ${{ secrets.CF_API_KEY }} + id: run-pipeline +``` +### Env variables +* A secret with name `CF_API_KEY` and value your Codefresh API token ( https://codefresh.io/docs/docs/integrations/codefresh-api/#authentication-instructions ) * An environment variable called `PIPELINE_NAME` with a value of `/` -* An environment variable called `TRIGGER_NAME` with trigger name attached to this pipeline. See the [triggers section](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/) for more information +* An optional environment variable called `TRIGGER_NAME` with trigger name attached to this pipeline. See the [triggers section](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/triggers/) for more information +* An optional environment variable called `CF_BRANCH` with branch name . Click the Done button to save your changes and commit. -Now next time you commit anything in your Github repository the Codefresh pipeline will also execute. +Now next time you commit anything in your GitHub repository the Codefresh pipeline will also execute. + +### Outputs +The action will report if the pipeline execution was successful. For example, if your pipeline has unit tests that fail, by default, it will report the action failed. The logs from the pipeline will be streamed into the Github action console. -## Usage -An example to run codefresh pipeline that can compile, test , docker build and deploy to kubernetes -``` -name: 'Codefresh pipeline runner' -description: 'Github action that run codefresh pipeline' -author: 'codefresh' -branding: - icon: 'arrow-right-circle' - color: 'green' -runs: - using: 'docker' - image: 'Dockerfile' -``` diff --git a/action.yml b/action.yml index 7e0c0d9..ba52678 100644 --- a/action.yml +++ b/action.yml @@ -1,9 +1,15 @@ name: 'Codefresh pipeline runner' -description: 'Github action that run codefresh pipeline' -author: 'codefresh' +description: 'Github action that runs codefresh pipeline' +author: 'codefresh-inc' +inputs: + args: + description: 'codefresh cli build variables, see https://codefresh-io.github.io/cli/pipelines/run-pipeline/#setting-variables-through-the-command for more information.' + required: false branding: icon: 'arrow-right-circle' color: 'green' runs: using: 'docker' image: 'Dockerfile' + args: + - ${{ inputs.args }} diff --git a/art/github-action-demo.gif b/art/github-action-demo.gif new file mode 100644 index 0000000..a98bdce Binary files /dev/null and b/art/github-action-demo.gif differ diff --git a/runner-entrypoint.sh b/runner-entrypoint.sh index 3c35b9e..59aeaf7 100755 --- a/runner-entrypoint.sh +++ b/runner-entrypoint.sh @@ -1,16 +1,31 @@ -#!/bin/sh +#!/bin/bash # parse branch -# in case of push event -BRANCH=$(cat $GITHUB_EVENT_PATH | jq -r .ref | awk -F '/' '{print $3}') +if [ -f $GITHUB_EVENT_PATH ]; then + # in case of push event + BRANCH=$(cat $GITHUB_EVENT_PATH | jq -r .ref | awk -F '/' '{print $3}') -if [-z "$BRANCH"] + if [ -z "$BRANCH" ] + then + # in case of pullresuest event + BRANCH=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.head.ref) + fi +else + echo "Required file on path 'GITHUB_EVENT_PATH' not exists" +fi +codefresh auth create-context context --api-key $CF_API_KEY +codefresh auth use-context context + +if [ -n "$CF_BRANCH" ] then - # in case of pullresuest event - BRANCH=$(cat $GITHUB_EVENT_PATH | jq -r head.ref) + BRANCH=$CF_BRANCH fi -codefresh auth create-context context --api-key $CF_API_KEY -codefresh auth use-contex context +echo "Execute pipeline with branch $BRANCH" -codefresh run $PIPELINE_NAME --trigger=$TRIGGER_NAME --branch=$BRANCH +if [ -n "$TRIGGER_NAME" ] +then + codefresh run $PIPELINE_NAME --trigger=$TRIGGER_NAME --branch=$BRANCH ${@:1} +else + codefresh run $PIPELINE_NAME --branch=$BRANCH ${@:1} +fi