Skip to content

Commit 8685041

Browse files
Merge pull request #33 from DakshCodess/DakshCodess-patch-21
Create openshift.yml
2 parents 1a52a31 + 401a0dd commit 8685041

File tree

1 file changed

+187
-0
lines changed

1 file changed

+187
-0
lines changed

.github/workflows/openshift.yml

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

0 commit comments

Comments
 (0)