Skip to content

Commit abbd69c

Browse files
authored
Merge pull request #1 from Luke-Roy-IBM/init
Initial commit with action github action code
2 parents 2d6f589 + d7edfbf commit abbd69c

File tree

2 files changed

+322
-2
lines changed

2 files changed

+322
-2
lines changed

README.md

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,154 @@
1-
# code-engine-github-action
2-
Repository to demonstrate how Github Actions can be used to deploy your source code to IBM Cloud Code Engine
1+
# Code Engine GitHub Action
2+
3+
This GitHub Action allows you to interact with IBM Cloud Code Engine. Deploy Apps, Jobs, and Functions. It offers flexibility for different deployment types and provides various configuration options.
4+
5+
## Inputs
6+
7+
| Name | Required | Default Value |Description |
8+
|-----------------|----------|---------------|----------------------------------------------------------------|
9+
| `api-key` || - | IAM API Key used to log into the IBM Cloud. Please store your IBM Cloud API key securely in your GitHub repository Secrets.|
10+
| `resource-group`|| Your Default Resource Group | An IBM Cloud Resource Group, a logical container for organizing and managing related cloud resources.|
11+
| `region` || - | The geographical area where your Code Engine project is located, like `eu-de` [codeengine-regions](https://cloud.ibm.com/docs/codeengine?topic=codeengine-regions)|
12+
| `project` || - | The unique identifier (GUID) or the name that identifies your IBM Cloud Code Engine project. |
13+
| `component` || - | The type of component to deploy. allowed values `application`, `app`, `function`, `func`, `fn`, `job` |
14+
| `name` || - | The name of the App, Function, or Job.|
15+
| `runtime` || - | The runtime used for the Function. Currently supported `nodejs-18` and `python-3.11` see [IBM Code Engine Function Runtimes](https://cloud.ibm.com/docs/codeengine?topic=codeengine-fun-runtime) for more information.|
16+
| `build-source` || . | Path to the directory containing the source code.|
17+
| `cpu` || - | CPU value set for your component [Config for Functions](https://cloud.ibm.com/docs/codeengine?topic=codeengine-fun-runtime), [Codeengine Memory CPU combo](https://cloud.ibm.com/docs/codeengine?topic=codeengine-mem-cpu-combo)|
18+
| `memory` || - | Memory value set for your component [Config for Functions](https://cloud.ibm.com/docs/codeengine?topic=codeengine-fun-runtime), [Codeengine Memory CPU combo](https://cloud.ibm.com/docs/codeengine?topic=codeengine-mem-cpu-combo)|
19+
20+
21+
22+
## Usage and Example
23+
24+
To use this action, add it to your GitHub Actions workflow YAML file also make sure to add your IBM Cloud API Key as GitHub Action Repository Secret. There is a example for deploying an App, Job and Python/Nodejs Function.
25+
26+
*Deploy an App: `deploy-app.yml`*: Deploy your app to `Default` resource-group in `eu-de` to the project `MY-PROJECT` with its source code in the root of the repository the name of the app is`my-app`.
27+
```yaml
28+
name: Deploy App to Code Engine
29+
30+
on:
31+
push:
32+
branches:
33+
- main
34+
workflow_dispatch:
35+
36+
jobs:
37+
38+
deploy-app:
39+
runs-on: ubuntu-latest
40+
steps:
41+
- name: Check out code
42+
uses: actions/checkout@v3
43+
44+
- name: Deploy Application to Code Engine
45+
uses: IBM/code-engine-github-action@v1
46+
with:
47+
api-key: ${{ secrets.IBM_IAM_API_KEY }}
48+
resource-group: 'Default'
49+
region: 'eu-de'
50+
project: 'MY-PROJECT'
51+
component: 'app'
52+
name: 'my-app'
53+
build-source: './'
54+
cpu: 1
55+
memory: 4G
56+
```
57+
58+
*Deploy a Job: `deploy-job.yml`*: Deploy your Job to `Default` resource-group in `eu-de` to the project `MY-PROJECT` with its source code in the root of the repository the name of the job is`my-job`.
59+
```yaml
60+
name: Deploy App to Code Engine
61+
62+
on:
63+
push:
64+
branches:
65+
- main
66+
workflow_dispatch:
67+
68+
jobs:
69+
70+
deploy-job:
71+
runs-on: ubuntu-latest
72+
steps:
73+
- name: Check out code
74+
uses: actions/checkout@v3
75+
76+
- name: Deploy Job to Code Engine
77+
uses: IBM/code-engine-github-action@v1
78+
with:
79+
api-key: ${{ secrets.IBM_IAM_API_KEY }}
80+
resource-group: 'Default'
81+
region: 'eu-de'
82+
project: 'MY-PROJECT'
83+
component: 'job'
84+
name: 'my-job'
85+
build-source: './'
86+
cpu: 1
87+
memory: 4G
88+
```
89+
90+
*Deploy a NodeJS Function: `deploy-nodejs-func.yml`*: Deploy your NodeJS Function to `Default` resource-group in `eu-de` to the project `MY-PROJECT` with its source code in the root of the repository the name of the function is`my-js-fn`.
91+
```yaml
92+
name: Deploy a NodeJS Function to Code Engine
93+
94+
on:
95+
push:
96+
branches:
97+
- main
98+
workflow_dispatch:
99+
100+
jobs:
101+
102+
deploy-nodejs-junc:
103+
runs-on: ubuntu-latest
104+
steps:
105+
- name: Check out code
106+
uses: actions/checkout@v3
107+
108+
- name: Deploy JavaScript Function to Code Engine
109+
uses: IBM/code-engine-github-action@v1
110+
with:
111+
api-key: ${{ secrets.IBM_IAM_API_KEY }}
112+
resource-group: 'Default'
113+
region: 'eu-de'
114+
project: 'MY-PROJECT'
115+
component: 'fn'
116+
runtime: nodejs-18
117+
name: 'my-js-fn'
118+
build-source: './'
119+
cpu: 1
120+
memory: 4G
121+
```
122+
123+
*Deploy a Python Function: `deploy-python-func.yml`*: Deploy your Python Function to `Default` resource-group in `eu-de` to the project `MY-PROJECT` with its source code in the root of the repository the name of the function is`my-py-fn`.
124+
```yaml
125+
name: Deploy a Python Function to Code Engine
126+
127+
on:
128+
push:
129+
branches:
130+
- main
131+
workflow_dispatch:
132+
133+
jobs:
134+
135+
fn-py:
136+
runs-on: ubuntu-latest
137+
steps:
138+
- name: Check out code
139+
uses: actions/checkout@v3
140+
141+
- name: Deploy Python Function to Code Engine
142+
uses: IBM/code-engine-github-action@v1
143+
with:
144+
api-key: ${{ secrets.IBM_IAM_API_KEY }}
145+
resource-group: 'Default'
146+
region: 'eu-de'
147+
project: 'MY-PROJECT'
148+
component: 'fn'
149+
runtime: python-3.11
150+
name: 'my-py-fn'
151+
build-source: './'
152+
cpu: 1
153+
memory: 4G
154+
```

action.yml

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
name: Code Engine GitHub Action
2+
description: Github action to interact with IBM Cloud Code Engine deploy Apps, Jobs and Functions
3+
author: IBM
4+
5+
branding:
6+
icon: cloud
7+
color: blue
8+
9+
inputs:
10+
api-key:
11+
description: IAM API Key used to log into the IBM Cloud
12+
required: true
13+
14+
resource-group:
15+
description: An IBM Cloud Resource Group is a logical container used to organize and manage related cloud resources
16+
required: false
17+
18+
region:
19+
description: The Region refers to a geographical area where the project is are located
20+
required: true
21+
22+
project:
23+
description: A Code Engine Project Is the grouping of your Apps, Functions and Jobs
24+
required: true
25+
26+
# App, Function or Job Specific inputs
27+
component:
28+
description: The type of component that should be deployed [App, Function, Job]
29+
required: true
30+
31+
name:
32+
description: Name of the App, Function or Job
33+
required: true
34+
35+
runtime:
36+
description: Runtime used for the Function only required for function
37+
required: false
38+
39+
build-source:
40+
description: path to the directory containing the source code
41+
required: false
42+
default: .
43+
44+
cpu:
45+
description: CPU configuration set for the component. If not set default Code Engine values are used.
46+
required: false
47+
48+
memory:
49+
description: Memory configuration set for the component. If not set default Code Engine values are used.
50+
required: false
51+
52+
# actual action code
53+
runs:
54+
using: composite
55+
56+
steps:
57+
# Default steps required
58+
- name: Install IBM Cloud CLI
59+
shell: bash
60+
run: curl -fsSL https://clis.cloud.ibm.com/install/linux | sh
61+
62+
- name: Login to IBM Cloud And target Resouce Groupe and Region
63+
shell: bash
64+
run: |
65+
ibmcloud login --apikey ${{ inputs.api-key }} -r ${{ inputs.region }}
66+
67+
- name: Set Resource Group
68+
shell: bash
69+
run: |
70+
if [[ "${{ inputs.resource-group }}" != "" ]] ; then
71+
ibmcloud target -g ${{ inputs.resource-group }}
72+
else
73+
ibmcloud target -g $(ibmcloud resource groups --default --output JSON | jq -r '.[0].id')
74+
fi
75+
76+
- name: Install Code Engine Plugin
77+
shell: bash
78+
run: ibmcloud plugin install code-engine
79+
80+
# Select the project using Name or ID
81+
# If the project doesn`t exist the project is created with the provided Name
82+
- name: Select Code Engine Project
83+
shell: bash
84+
run: |
85+
if ibmcloud ce project select --name "${{ inputs.project }}" || ibmcloud ce project select --id ${{ inputs.project }} ; then
86+
echo "Project Selected"
87+
else
88+
ibmcloud ce project create --name "${{ inputs.project }}"
89+
fi
90+
91+
# set resources for target
92+
- name: Set resources
93+
id: set-resources
94+
shell: bash
95+
run: |
96+
# set the CPU value
97+
if [[ "${{ inputs.cpu }}" != "" ]] ; then
98+
echo "cpu=--cpu ${{ inputs.cpu }}" >> "$GITHUB_OUTPUT"
99+
else
100+
echo "cpu=" >> "$GITHUB_OUTPUT"
101+
fi
102+
103+
# set the memory value
104+
if [[ "${{ inputs.memory }}" != "" ]] ; then
105+
echo "memory=--memory ${{ inputs.memory }}" >> "$GITHUB_OUTPUT"
106+
else
107+
echo "memory=" >> "$GITHUB_OUTPUT"
108+
fi
109+
110+
# Functions Steps
111+
- name: Create or Update Functions
112+
shell: bash
113+
id: fn-create
114+
if: ( inputs.component == 'function' || inputs.component == 'func' || inputs.component == 'fn' )
115+
run: |
116+
117+
if ibmcloud ce fn get --name ${{ inputs.name }} ; then
118+
ibmcloud ce fn update --name ${{ inputs.name }} --runtime ${{ inputs.runtime }} --build-source ${{ inputs.build-source }} ${{ steps.set-resources.outputs.cpu }} ${{ steps.set-resources.outputs.memory }}
119+
else
120+
ibmcloud ce fn create --name ${{ inputs.name }} --runtime ${{ inputs.runtime }} --build-source ${{ inputs.build-source }} ${{ steps.set-resources.outputs.cpu }} ${{ steps.set-resources.outputs.memory }}
121+
fi
122+
123+
# Application Steps
124+
- name: Create or Update Application
125+
shell: bash
126+
id: create-app
127+
if: ( inputs.component == 'application' || inputs.component == 'app' )
128+
run: |
129+
130+
if ibmcloud ce application get --name ${{ inputs.name }} ; then
131+
ibmcloud ce application update --name ${{ inputs.name }} --build-source ${{ inputs.build-source }} ${{ steps.set-resources.outputs.cpu }} ${{ steps.set-resources.outputs.memory }}
132+
else
133+
ibmcloud ce application create --name ${{ inputs.name }} --build-source ${{ inputs.build-source }} ${{ steps.set-resources.outputs.cpu }} ${{ steps.set-resources.outputs.memory }}
134+
fi
135+
136+
# Job Steps
137+
- name: Create or Update Job
138+
shell: bash
139+
id: create-job
140+
if: inputs.component == 'job'
141+
run: |
142+
143+
if ibmcloud ce job get --name ${{ inputs.name }} ; then
144+
ibmcloud ce job update --name ${{ inputs.name }} --build-source ${{ inputs.build-source }} --wait ${{ steps.set-resources.outputs.cpu }} ${{ steps.set-resources.outputs.memory }}
145+
else
146+
ibmcloud ce job create --name ${{ inputs.name }} --build-source ${{ inputs.build-source }} --wait ${{ steps.set-resources.outputs.cpu }} ${{ steps.set-resources.outputs.memory }}
147+
fi
148+
149+
- name: Get component
150+
shell: bash
151+
if: steps.fn-create.outcome == 'success' || steps.app-create.outcome == 'success' || steps.job-create.outcome == 'success'
152+
run: |
153+
case ${{ inputs.component }} in
154+
function|func|fn)
155+
ibmcloud ce fn get --name ${{ inputs.name }}
156+
;;
157+
application|app)
158+
ibmcloud ce app get --name ${{ inputs.name }}
159+
;;
160+
job)
161+
ibmcloud ce job get --name ${{ inputs.name }}
162+
;;
163+
*)
164+
echo "Wrong Code Engine component used!"
165+
echo "Use[ function | func | fn | application | app | job ]"
166+
exit 1
167+
;;
168+
esac

0 commit comments

Comments
 (0)