A recipe for automating GitHub Pull Request release CI pipelines with Kubernetes, Helm and Codefresh.
Although Codefresh can connect to other K8S cloud providers and container registries, for simplicity, this tutorial assumes a functioning GKE K8S cluster and GCR registry.
- In cloud console, note the GCP project name associated with your GKE cluster for use farther below.
- Create a new namespace if you don't have one you want to use for this demo. I recommend
kubectl create ns codefresh
, but theNAMESPACE
environment variable required by this demo app is configurable. - Helm must be installed, and you must be able to connect to Tiller to your desired namespace. The simplest way to secure your Tiller installation is the first approach (restart tiller with
--listen=localhost:44134
flag) outlined in this excellent article by @anguslees. If you take a more complicated approach, this tutorial assumes you know what you're doing.
In Codefresh UI:
In GitHub UI:
- Fork this repo
- Create and save a new GitHub Personal access token, setting the
repo
scope
In Codefresh UI:
-
Create a Codefresh account with a GitHub user, if you haven't alrady done so. Note this demo is for GitHub - if you already have a Codefresh account connected to another git provider:
Currently, it is possible to have only one git provider per account. You have to create a separate Codefresh account for each of your git providers.
Be sure to accept the permission request for Codefresh to access your git provider account.
-
Add your fork of this repo as a (GitHub) repository
-
Create and save a new Codefresh API key
You will create two pipelines. This first pipeline will filter PR actions to only those you wish to trigger a Pull Request release: if the actions are one of "opened", "reopened", "synchronize", or "closed", this will trigger the second pipeline responsible for building the PR release. Note that a future feature of Codefresh - allowing selection of Pull Request actions to trigger a build - will make this first step unnecessary.
- Name your first pipeline "PR action filter"
- Under
Configuration
>General Settings
>Automated build
:Trigger flow on
selectAll Branches and Tags
Add webhook
toggleOn
Trigger by
select onlyPull request opened
- Under
Configuration
>Environment Variables
fill out theNew variable
key and value fields, and clickAdd variable
for each of the below vars:PORT
=3000
REGISTRY_DOMAIN
=gcr.io
REGISTRY_ACCOUNT
= [your GCP project ID]NAMESPACE
= [your desired K8S namespace]GITHUB_TOKEN
= [your GitHub personal access token]API_KEY
= [your Codefresh API key]PIPELINE_ID
= [the ID of the next pipeline you will create. See next section]
- Under
WORKFLOW
toggle fromBasic
toYAML
, and selectInline YAML
- Paste the contents of
codefresh-actions.yaml
from this repo into the textarea - Note that we recommend
Inline YAML
for security reasons. TheUse YAML from Repository
option draws from the PR branch, and this can be a security risk (for example, an unprivileged user submitting a PR to a public repo could run arbitrarykubectl
commands on the connected cluster)
- Paste the contents of
- Click
Save
to save these configurations for this pipeline
Triggered only by the first "PR action filter" pipeline, this pipeline is responsible for building the Pull Request release, and updating the PR accordingly.
- Click
Add Pipeline
- Name this second pipeline "PR release"
- Copy the pipeline ID, and add to the
PIPELINE_ID
environment variable in the previous pipeline (you can get the ID from the Codefresh CLI or by temporarily enabling the webhook option which contains the ID) - Do not enable
Configuration
>General Settings
>Automated build
>Add webhook
- Do not bother configuring
Configuration
>Environment Variables
, as they will be ignored since this pipeline is only built from the previous one, which in YAML is configured to pass along it's own environment variables. - Under
WORKFLOW
toggle fromBasic
toYAML
, and selectInline YAML
- Paste the contents of
codefresh.yaml
from this repo into the textarea - Note that we recommend
Inline YAML
for security reasons. See explanation above
- Paste the contents of
- Click
Save
to save these configurations for this pipeline
In GitHub UI:
- Browse to your new test repo
- Click Create new file
- Name your file
test
(file can be empty), selectCreate a new branch for this commit and start a pull request
, and clickPropose new file
- On the next page, click
Create pull request
In GitHub UI (or API):
- During build, the Pull Request Status should contain two pending checks:
Codefresh - Build is pending or running
, linking to the Codefresh buildPR Release — Waiting for successful build
- On success, the Pull Request Status should contain two successful checks:
Codefresh - Build passed
, linking to the Codefresh buildPR Release — Deployed to codefresh namespace
, linking to the built PR release (printing only "Hello")