Skip to content

Commit 9724334

Browse files
authored
Create openshift.yml
1 parent d3ac5b0 commit 9724334

File tree

1 file changed

+206
-0
lines changed

1 file changed

+206
-0
lines changed

.github/workflows/openshift.yml

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

0 commit comments

Comments
 (0)