Skip to content

Commit 0e14212

Browse files
committed
Create openshift.yml
1 parent 89f36e5 commit 0e14212

File tree

1 file changed

+203
-0
lines changed

1 file changed

+203
-0
lines changed

.github/workflows/openshift.yml

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# This workflow uses actions that are not certified by GitHub.
2+
# They are provided by a third-party and are governed by
3+
# separate terms of service, privacy policy, and support
4+
# documentation.
5+
6+
# 💁 The OpenShift Starter workflow will:
7+
# - Checkout your repository
8+
# - Perform a container image build
9+
# - Push the built image to the GitHub Container Registry (GHCR)
10+
# - Log in to your OpenShift cluster
11+
# - Create an OpenShift app from the image and expose it to the internet
12+
13+
# ℹ️ Configure your repository and the workflow with the following steps:
14+
# 1. Have access to an OpenShift cluster. Refer to https://www.openshift.com/try
15+
# 2. Create the OPENSHIFT_SERVER and OPENSHIFT_TOKEN repository secrets. Refer to:
16+
# - https://github.com/redhat-actions/oc-login#readme
17+
# - https://docs.github.com/en/actions/reference/encrypted-secrets
18+
# - https://cli.github.com/manual/gh_secret_set
19+
# 3. (Optional) Edit the top-level 'env' section as marked with '🖊️' if the defaults are not suitable for your project.
20+
# 4. (Optional) Edit the build-image step to build your project.
21+
# The default build type is by using a Dockerfile at the root of the repository,
22+
# but can be replaced with a different file, a source-to-image build, or a step-by-step buildah build.
23+
# 5. Commit and push the workflow file to your default branch to trigger a workflow run.
24+
25+
# 👋 Visit our GitHub organization at https://github.com/redhat-actions/ to see our actions and provide feedback.
26+
27+
name: OpenShift
28+
name: Setup Node.js environment
29+
uses: actions/[email protected]
30+
env:
31+
# 🖊️ EDIT your repository secrets to log into your OpenShift cluster and set up the context.
32+
# See https://github.com/redhat-actions/oc-login#readme for how to retrieve these values.
33+
# To get a permanent token, refer to https://github.com/redhat-actions/oc-login/wiki/Using-a-Service-Account-for-GitHub-Actions
34+
OPENSHIFT_SERVER: ${{ secrets.OPENSHIFT_SERVER }}
35+
OPENSHIFT_TOKEN: ${{ secrets.OPENSHIFT_TOKEN }}
36+
# 🖊️ EDIT to set the kube context's namespace after login. Leave blank to use your user's default namespace.
37+
OPENSHIFT_NAMESPACE: ""
38+
39+
# 🖊️ EDIT to set a name for your OpenShift app, or a default one will be generated below.
40+
APP_NAME: ""
41+
42+
# 🖊️ EDIT with the port your application should be accessible on.
43+
# If the container image exposes *exactly one* port, this can be left blank.
44+
# Refer to the 'port' input of https://github.com/redhat-actions/oc-new-app
45+
APP_PORT: ""
46+
47+
# 🖊️ EDIT to change the image registry settings.
48+
# Registries such as GHCR, Quay.io, and Docker Hub are supported.
49+
IMAGE_REGISTRY: ghcr.io/${{ github.repository_owner }}
50+
IMAGE_REGISTRY_USER: ${{ github.actor }}
51+
IMAGE_REGISTRY_PASSWORD: ${{ github.token }}
52+
53+
# 🖊️ EDIT to specify custom tags for the container image, or default tags will be generated below.
54+
IMAGE_TAGS: ""
55+
56+
on:
57+
# https://docs.github.com/en/actions/reference/events-that-trigger-workflows
58+
workflow_dispatch:
59+
push:
60+
# Edit to the branch(es) you want to build and deploy on each push.
61+
branches: [ "main" ]
62+
63+
jobs:
64+
# 🖊️ EDIT if you want to run vulnerability check on your project before deploying
65+
# the application. Please uncomment the below CRDA scan job and configure to run it in
66+
# your workflow. For details about CRDA action visit https://github.com/redhat-actions/crda/blob/main/README.md
67+
#
68+
# TODO: Make sure to add 'CRDA Scan' starter workflow from the 'Actions' tab.
69+
# For guide on adding new starter workflow visit https://docs.github.com/en/github-ae@latest/actions/using-workflows/using-starter-workflows
70+
71+
#crda-scan:
72+
# uses: ./.github/workflows/crda.yml
73+
# secrets:
74+
# CRDA_KEY: ${{ secrets.CRDA_KEY }}
75+
# # SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }} # Either use SNYK_TOKEN or CRDA_KEY
76+
77+
openshift-ci-cd:
78+
# 🖊️ Uncomment this if you are using CRDA scan step above
79+
# needs: crda-scan
80+
name: Build and deploy to OpenShift
81+
runs-on: ubuntu-latest
82+
environment: production
83+
84+
outputs:
85+
ROUTE: ${{ steps.deploy-and-expose.outputs.route }}
86+
SELECTOR: ${{ steps.deploy-and-expose.outputs.selector }}
87+
88+
steps:
89+
- name: Check for required secrets
90+
uses: actions/github-script@v6
91+
with:
92+
script: |
93+
const secrets = {
94+
OPENSHIFT_SERVER: `${{ secrets.OPENSHIFT_SERVER }}`,
95+
OPENSHIFT_TOKEN: `${{ secrets.OPENSHIFT_TOKEN }}`,
96+
};
97+
98+
const GHCR = "ghcr.io";
99+
if (`${{ env.IMAGE_REGISTRY }}`.startsWith(GHCR)) {
100+
core.info(`Image registry is ${GHCR} - no registry password required`);
101+
}
102+
else {
103+
core.info("A registry password is required");
104+
secrets["IMAGE_REGISTRY_PASSWORD"] = `${{ secrets.IMAGE_REGISTRY_PASSWORD }}`;
105+
}
106+
107+
const missingSecrets = Object.entries(secrets).filter(([ name, value ]) => {
108+
if (value.length === 0) {
109+
core.error(`Secret "${name}" is not set`);
110+
return true;
111+
}
112+
core.info(`✔️ Secret "${name}" is set`);
113+
return false;
114+
});
115+
116+
if (missingSecrets.length > 0) {
117+
core.setFailed(`❌ At least one required secret is not set in the repository. \n` +
118+
"You can add it using:\n" +
119+
"GitHub UI: https://docs.github.com/en/actions/reference/encrypted-secrets#creating-encrypted-secrets-for-a-repository \n" +
120+
"GitHub CLI: https://cli.github.com/manual/gh_secret_set \n" +
121+
"Also, refer to https://github.com/redhat-actions/oc-login#getting-started-with-the-action-or-see-example");
122+
}
123+
else {
124+
core.info(`✅ All the required secrets are set`);
125+
}
126+
127+
- name: Check out repository
128+
uses: actions/checkout@v4
129+
130+
- name: Determine app name
131+
if: env.APP_NAME == ''
132+
run: |
133+
echo "APP_NAME=$(basename $PWD)" | tee -a $GITHUB_ENV
134+
135+
- name: Determine image tags
136+
if: env.IMAGE_TAGS == ''
137+
run: |
138+
echo "IMAGE_TAGS=latest ${GITHUB_SHA::12}" | tee -a $GITHUB_ENV
139+
140+
# https://github.com/redhat-actions/buildah-build#readme
141+
- name: Build from Dockerfile
142+
id: build-image
143+
uses: redhat-actions/buildah-build@v2
144+
with:
145+
image: ${{ env.APP_NAME }}
146+
tags: ${{ env.IMAGE_TAGS }}
147+
148+
# If you don't have a Dockerfile/Containerfile, refer to https://github.com/redhat-actions/buildah-build#scratch-build-inputs
149+
# Or, perform a source-to-image build using https://github.com/redhat-actions/s2i-build
150+
# Otherwise, point this to your Dockerfile/Containerfile relative to the repository root.
151+
dockerfiles: |
152+
./Dockerfile
153+
154+
# https://github.com/redhat-actions/push-to-registry#readme
155+
- name: Push to registry
156+
id: push-image
157+
uses: redhat-actions/push-to-registry@v2
158+
with:
159+
image: ${{ steps.build-image.outputs.image }}
160+
tags: ${{ steps.build-image.outputs.tags }}
161+
registry: ${{ env.IMAGE_REGISTRY }}
162+
username: ${{ env.IMAGE_REGISTRY_USER }}
163+
password: ${{ env.IMAGE_REGISTRY_PASSWORD }}
164+
165+
# The path the image was pushed to is now stored in ${{ steps.push-image.outputs.registry-path }}
166+
167+
- name: Install oc
168+
uses: redhat-actions/openshift-tools-installer@v1
169+
with:
170+
oc: 4
171+
172+
# https://github.com/redhat-actions/oc-login#readme
173+
- name: Log in to OpenShift
174+
uses: redhat-actions/oc-login@v1
175+
with:
176+
openshift_server_url: ${{ env.OPENSHIFT_SERVER }}
177+
openshift_token: ${{ env.OPENSHIFT_TOKEN }}
178+
insecure_skip_tls_verify: true
179+
namespace: ${{ env.OPENSHIFT_NAMESPACE }}
180+
181+
# This step should create a deployment, service, and route to run your app and expose it to the internet.
182+
# https://github.com/redhat-actions/oc-new-app#readme
183+
- name: Create and expose app
184+
id: deploy-and-expose
185+
uses: redhat-actions/oc-new-app@v1
186+
with:
187+
app_name: ${{ env.APP_NAME }}
188+
image: ${{ steps.push-image.outputs.registry-path }}
189+
namespace: ${{ env.OPENSHIFT_NAMESPACE }}
190+
port: ${{ env.APP_PORT }}
191+
192+
- name: Print application URL
193+
env:
194+
ROUTE: ${{ steps.deploy-and-expose.outputs.route }}
195+
SELECTOR: ${{ steps.deploy-and-expose.outputs.selector }}
196+
run: |
197+
[[ -n ${{ env.ROUTE }} ]] || (echo "Determining application route failed in previous step"; exit 1)
198+
echo
199+
echo "======================== Your application is available at: ========================"
200+
echo ${{ env.ROUTE }}
201+
echo "==================================================================================="
202+
echo
203+
echo "Your app can be taken down with: \"oc delete all --selector='${{ env.SELECTOR }}'\""

0 commit comments

Comments
 (0)