Update OpenShift workflow to use GHCR by default (#6)

- Simplifies required configuration since a registry account is now
  optional
- Update a variety of comments
- Use tools-installer to install oc
- Other small changes towards a better UX

Signed-off-by: Tim Etchells <[email protected]>
This commit is contained in:
Tim Etchells
2021-09-14 11:12:35 -07:00
parent 028df69d88
commit 48e2865d35
+77 -70
View File
@@ -3,61 +3,57 @@
# separate terms of service, privacy policy, and support # separate terms of service, privacy policy, and support
# documentation. # documentation.
### The OpenShift Starter workflow will: # 💁 The OpenShift Starter workflow will:
# - Checkout your repository # - Checkout your repository
# - Perform a Docker build # - Perform a container image build
# - Push the built image to an image registry # - Push the built image to the GitHub Container Registry (GHCR)
# - Log in to your OpenShift cluster # - Log in to your OpenShift cluster
# - Create an OpenShift app from the image and expose it to the internet. # - Create an OpenShift app from the image and expose it to the internet
### Before you begin: # ️ Configure your repository and the workflow with the following steps:
# - Have write access to a container image registry such as quay.io or Dockerhub. # 1. Have access to an OpenShift cluster. Refer to https://www.openshift.com/try
# - Have access to an OpenShift cluster. # 2. Create the OPENSHIFT_SERVER and OPENSHIFT_TOKEN repository secrets. Refer to:
# - For instructions to get started with OpenShift see https://www.openshift.com/try # - https://github.com/redhat-actions/oc-login#readme
# - The project you wish to add this workflow to should have a Dockerfile. # - https://docs.github.com/en/actions/reference/encrypted-secrets
# - If you don't have a Dockerfile at the repository root, see the buildah-build step. # - https://cli.github.com/manual/gh_secret_set
# - Builds from scratch are also available, but require more configuration. # 3. (Optional) Edit the top-level 'env' section as marked with '🖊️' if the defaults are not suitable for your project.
# 4. (Optional) Edit the build-image step to build your project.
# The default build type is by using a Dockerfile at the root of the repository,
# but can be replaced with a different file, a source-to-image build, or a step-by-step buildah build.
# 5. Commit and push the workflow file to your default branch to trigger a workflow run.
### To get the workflow running: # 👋 Visit our GitHub organization at https://github.com/redhat-actions/ to see our actions and provide feedback.
# 1. Add this workflow to your repository.
# 2. Edit the top-level 'env' section, which contains a list of environment variables that must be configured.
# 3. Create the secrets referenced in the 'env' section under your repository Settings.
# 4. Edit the 'branches' in the 'on' section to trigger the workflow on a push to your branch.
# 5. Commit and push your changes.
# For a more sophisticated example, see https://github.com/redhat-actions/spring-petclinic/blob/main/.github/workflows/petclinic-sample.yaml
# Also see our GitHub organization, https://github.com/redhat-actions/
# ▶️ See a video of how to set up this workflow at https://www.youtube.com/watch?v=6hgBO-1pKho
name: OpenShift name: OpenShift
# ⬇️ Modify the fields marked with ⬇️ to fit your project, and create any secrets that are referenced.
# https://docs.github.com/en/free-pro-team@latest/actions/reference/encrypted-secrets
env: env:
# EDIT with your registry and registry path. # 🖊 EDIT your repository secrets to log into your OpenShift cluster and set up the context.
REGISTRY: quay.io/<username>
# ⬇️ EDIT with your registry username.
REGISTRY_USER: <username>
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
# ⬇️ EDIT to log into your OpenShift cluster and set up the context.
# See https://github.com/redhat-actions/oc-login#readme for how to retrieve these values. # See https://github.com/redhat-actions/oc-login#readme for how to retrieve these values.
# To get a permanent token, refer to https://github.com/redhat-actions/oc-login/wiki/Using-a-Service-Account-for-GitHub-Actions
OPENSHIFT_SERVER: ${{ secrets.OPENSHIFT_SERVER }} OPENSHIFT_SERVER: ${{ secrets.OPENSHIFT_SERVER }}
OPENSHIFT_TOKEN: ${{ secrets.OPENSHIFT_TOKEN }} OPENSHIFT_TOKEN: ${{ secrets.OPENSHIFT_TOKEN }}
# 🖊️ EDIT to set the kube context's namespace after login. Leave blank to use your user's default namespace.
# ⬇️ EDIT with any additional port your application should expose.
# By default, oc new-app action creates a service to the image's lowest numeric exposed port.
APP_PORT: ""
# ⬇️ EDIT if you wish to set the kube context's namespace after login. Leave blank to use the default namespace.
OPENSHIFT_NAMESPACE: "" OPENSHIFT_NAMESPACE: ""
# If you wish to manually provide the APP_NAME and TAG, set them here, otherwise they will be auto-detected. # 🖊️ EDIT to set a name for your OpenShift app, or a default one will be generated below.
APP_NAME: "" APP_NAME: ""
TAG: ""
# 🖊️ EDIT with the port your application should be accessible on.
# If the container image exposes *exactly one* port, this can be left blank.
# Refer to the 'port' input of https://github.com/redhat-actions/oc-new-app
APP_PORT: ""
# 🖊️ EDIT to change the image registry settings.
# Registries such as GHCR, Quay.io, and Docker Hub are supported.
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }}
IMAGE_REGISTRY_USER: ${{ github.actor }}
IMAGE_REGISTRY_PASSWORD: ${{ github.token }}
# 🖊️ EDIT to specify custom tags for the container image, or default tags will be generated below.
IMAGE_TAGS: ""
on: on:
# https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows # https://docs.github.com/en/actions/reference/events-that-trigger-workflows
push: push:
# Edit to the branch(es) you want to build and deploy on each push. # Edit to the branch(es) you want to build and deploy on each push.
branches: [ $default-branch ] branches: [ $default-branch ]
@@ -65,32 +61,40 @@ on:
jobs: jobs:
openshift-ci-cd: openshift-ci-cd:
name: Build and deploy to OpenShift name: Build and deploy to OpenShift
# ubuntu-20.04 can also be used.
runs-on: ubuntu-18.04 runs-on: ubuntu-18.04
environment: production environment: production
outputs: outputs:
ROUTE: ${{ steps.deploy-and-expose.outputs.route }} ROUTE: ${{ steps.deploy-and-expose.outputs.route }}
SELECTOR: ${{ steps.deploy-and-expose.outputs.selector }} SELECTOR: ${{ steps.deploy-and-expose.outputs.selector }}
steps: steps:
- name: Check if secrets exists - name: Check for required secrets
uses: actions/github-script@v3 uses: actions/github-script@v3
with: with:
script: | script: |
const secrets = { const secrets = {
REGISTRY_PASSWORD: `${{ secrets.REGISTRY_PASSWORD }}`,
OPENSHIFT_SERVER: `${{ secrets.OPENSHIFT_SERVER }}`, OPENSHIFT_SERVER: `${{ secrets.OPENSHIFT_SERVER }}`,
OPENSHIFT_TOKEN: `${{ secrets.OPENSHIFT_TOKEN }}`, OPENSHIFT_TOKEN: `${{ secrets.OPENSHIFT_TOKEN }}`,
}; };
const GHCR = "ghcr.io";
if (`${{ env.IMAGE_REGISTRY }}`.startsWith(GHCR)) {
core.info(`Image registry is ${GHCR} - no registry password required`);
}
else {
core.info("A registry password is required");
secrets["IMAGE_REGISTRY_PASSWORD"] = `${{ secrets.IMAGE_REGISTRY_PASSWORD }}`;
}
const missingSecrets = Object.entries(secrets).filter(([ name, value ]) => { const missingSecrets = Object.entries(secrets).filter(([ name, value ]) => {
if (value.length === 0) { if (value.length === 0) {
core.warning(`Secret "${name}" is not set`); core.error(`Secret "${name}" is not set`);
return true; return true;
} }
core.info(`✔️ Secret "${name}" is set`); core.info(`✔️ Secret "${name}" is set`);
return false; return false;
}); });
if (missingSecrets.length > 0) { if (missingSecrets.length > 0) {
@@ -104,48 +108,50 @@ jobs:
core.info(`✅ All the required secrets are set`); core.info(`✅ All the required secrets are set`);
} }
- uses: actions/checkout@v2 - name: Check out repository
uses: actions/checkout@v2
- name: Determine app name - name: Determine app name
if: env.APP_NAME == '' if: env.APP_NAME == ''
run: | run: |
echo "APP_NAME=$(basename $PWD)" | tee -a $GITHUB_ENV echo "APP_NAME=$(basename $PWD)" | tee -a $GITHUB_ENV
- name: Determine tag - name: Determine image tags
if: env.TAG == '' if: env.IMAGE_TAGS == ''
run: | run: |
echo "TAG=${GITHUB_SHA::7}" | tee -a $GITHUB_ENV echo "IMAGE_TAGS=latest ${GITHUB_SHA::12}" | tee -a $GITHUB_ENV
# https://github.com/redhat-actions/buildah-build#readme # https://github.com/redhat-actions/buildah-build#readme
- name: Build from Dockerfile - name: Build from Dockerfile
id: image-build id: build-image
uses: redhat-actions/buildah-build@v2 uses: redhat-actions/buildah-build@v2
with: with:
image: ${{ env.APP_NAME }} image: ${{ env.APP_NAME }}
tags: ${{ env.TAG }} tags: ${{ env.IMAGE_TAGS }}
# If you don't have a dockerfile, see:
# https://github.com/redhat-actions/buildah-build#scratch-build-inputs # If you don't have a Dockerfile/Containerfile, refer to https://github.com/redhat-actions/buildah-build#scratch-build-inputs
# Otherwise, point this to your Dockerfile relative to the repository root. # Or, perform a source-to-image build using https://github.com/redhat-actions/s2i-build
# Otherwise, point this to your Dockerfile/Containerfile relative to the repository root.
dockerfiles: | dockerfiles: |
./Dockerfile ./Dockerfile
# https://github.com/redhat-actions/push-to-registry#readme # https://github.com/redhat-actions/push-to-registry#readme
- name: Push to registry - name: Push to registry
id: push-to-registry id: push-image
uses: redhat-actions/push-to-registry@v2 uses: redhat-actions/push-to-registry@v2
with: with:
image: ${{ steps.image-build.outputs.image }} image: ${{ steps.build-image.outputs.image }}
tags: ${{ steps.image-build.outputs.tags }} tags: ${{ steps.build-image.outputs.tags }}
registry: ${{ env.REGISTRY }} registry: ${{ env.IMAGE_REGISTRY }}
username: ${{ env.REGISTRY_USER }} username: ${{ env.IMAGE_REGISTRY_USER }}
password: ${{ env.REGISTRY_PASSWORD }} password: ${{ env.IMAGE_REGISTRY_PASSWORD }}
# The path the image was pushed to is now stored in ${{ steps.push-to-registry.outputs.registry-path }} # The path the image was pushed to is now stored in ${{ steps.push-image.outputs.registry-path }}
# oc-login works on all platforms, but oc must be installed first. - name: Install oc
# The GitHub Ubuntu runner already includes oc. uses: redhat-actions/openshift-tools-installer@v1
# Otherwise, https://github.com/redhat-actions/openshift-tools-installer can be used to install oc, with:
# as well as many other tools. oc: 4
# https://github.com/redhat-actions/oc-login#readme # https://github.com/redhat-actions/oc-login#readme
- name: Log in to OpenShift - name: Log in to OpenShift
@@ -163,18 +169,19 @@ jobs:
uses: redhat-actions/oc-new-app@v1 uses: redhat-actions/oc-new-app@v1
with: with:
app_name: ${{ env.APP_NAME }} app_name: ${{ env.APP_NAME }}
image: ${{ steps.push-to-registry.outputs.registry-path }} image: ${{ steps.push-image.outputs.registry-path }}
namespace: ${{ env.OPENSHIFT_NAMESPACE }} namespace: ${{ env.OPENSHIFT_NAMESPACE }}
port: ${{ env.APP_PORT }} port: ${{ env.APP_PORT }}
- name: View application route - name: Print application URL
env:
ROUTE: ${{ steps.deploy-and-expose.outputs.route }}
SELECTOR: ${{ steps.deploy-and-expose.outputs.selector }}
run: | run: |
[[ -n ${{ env.ROUTE }} ]] || (echo "Determining application route failed in previous step"; exit 1) [[ -n ${{ env.ROUTE }} ]] || (echo "Determining application route failed in previous step"; exit 1)
echo
echo "======================== Your application is available at: ========================" echo "======================== Your application is available at: ========================"
echo ${{ env.ROUTE }} echo ${{ env.ROUTE }}
echo "===================================================================================" echo "==================================================================================="
echo echo
echo "Your app can be taken down with: \"oc delete all --selector='${{ env.SELECTOR }}'\"" echo "Your app can be taken down with: \"oc delete all --selector='${{ env.SELECTOR }}'\""
env:
ROUTE: ${{ steps.deploy-and-expose.outputs.route }}
SELECTOR: ${{ steps.deploy-and-expose.outputs.selector }}