Skip to content

Commit 924ced2

Browse files
committed
updated the complete application
1 parent 5a57d1e commit 924ced2

File tree

5 files changed

+109
-5
lines changed

5 files changed

+109
-5
lines changed

README.md

+79-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,81 @@
1-
# automate-github-actions-using-rest-api
2-
This is a sample node.js project for which github actions cicd workflow file is automated using the github rest apis
1+
# Automate Github Actions Using GitHub REST API
2+
This project is a demonstration of automating a GitHub Actions CI/CD pipeline using the GitHub REST API, integrated into a simple Node.js web application. The application includes a button to trigger the CI/CD pipeline directly from the frontend, providing an interactive way to manage the workflow.
33

4+
## Features
5+
- **GitHub Actions Integration :** Uses GitHub REST API to trigger a workflow directly from the application.
6+
- **Interactive UI :** A button on the web interface initiates the CI/CD pipeline.
7+
- **REST API Usage :** Demonstrates how to call the GitHub REST API from within a Node.js server.
8+
- **CORS Enabled :** Ensures the frontend can communicate with the backend on the same server.
49

5-
- `server.js`: This file will serve an HTML page and include an endpoint to trigger the GitHub Actions workflow.
10+
## Project Structure
11+
```bash
12+
demo-ci-cd-app/
13+
├── .env # Environment variables (GitHub token)
14+
├── .github/
15+
│ └── workflows/
16+
│ └── main.yml # GitHub Actions workflow file
17+
├── public/
18+
│ └── index.html # Frontend HTML file
19+
├── server.js # Node.js server file
20+
├── package.json # Node.js project configuration
21+
└── README.md # Project documentation
22+
└── node_modules # Project dependencies
23+
```
24+
25+
## Installation
26+
### Step 1: Clone the Repository
27+
28+
```bash
29+
git clone https://github.com/YOUR_USERNAME/demo-ci-cd-app.git
30+
cd automate-github-actions-using-rest-api
31+
```
32+
33+
### Step 2: Install Dependencies
34+
35+
```bash
36+
npm install
37+
```
38+
39+
### Step 3: Set Up Environment Variable
40+
Create a .env file in the root directory:
41+
42+
```bash
43+
GITHUB_TOKEN=your_personal_access_token
44+
# Replace your_personal_access_token with your GitHub Personal Access Token (with repo and workflow permissions).
45+
```
46+
47+
### Step 4: Configure GitHub Actions Workflow:
48+
49+
Go to the `.github/workflows/main.yml` file and ensure it has the following setup:
50+
51+
```yaml
52+
name: CI/CD Pipeline
53+
54+
on:
55+
workflow_dispatch:
56+
push:
57+
branches:
58+
- main
59+
60+
jobs:
61+
build:
62+
runs-on: ubuntu-latest
63+
64+
steps:
65+
- name: Checkout Code
66+
uses: actions/checkout@v2
67+
68+
- name: Display Message
69+
run: echo "CI/CD Pipeline is triggered successfully!"
70+
```
71+
72+
### Step 5: Start the Server:
73+
74+
```bash
75+
node server.js
76+
```
77+
78+
### Access the Application
79+
80+
- Open your browser and go to http://localhost:3000.
81+
- Click on the Trigger CI/CD Pipeline button. This will make a POST request to the GitHub API to start the workflow.

package-lock.json

+23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"license": "ISC",
1212
"dependencies": {
1313
"axios": "^1.7.7",
14+
"cors": "^2.8.5",
1415
"dotenv": "^16.4.5",
1516
"express": "^4.21.1"
1617
}

public/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<title>Demo CI/CD App</title>
88
</head>
99
<body>
10-
<h1>Demo CI/CD Application</h1>
10+
<h1>Demo CI/CD Applications</h1>
1111
<button onclick="triggerWorkflow()">Trigger CI/CD Pipeline</button>
1212
<p id="status"></p>
1313

server.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,23 @@
22
const express = require('express');
33
const axios = require('axios');
44
const path = require('path');
5+
const cors = require('cors');
56
require('dotenv').config();
7+
console.log('GitHub Token:', process.env.GITHUB_TOKEN);
68

79
const app = express();
810
const PORT = 3000;
911

12+
app.use(cors());
13+
1014
// Serve static HTML files
1115
app.use(express.static(path.join(__dirname, 'public')));
1216

1317
// Endpoint to trigger GitHub Actions workflow
1418
app.post('/trigger-workflow', async (req, res) => {
1519
try {
1620
const response = await axios.post(
17-
`https://api.github.com/repos/aman1905/automate-github-actions-using-rest-api/actions/workflows/main.yml/dispatches`,
21+
`https://api.github.com/repos/YOUR_USERNAME/automate-github-actions-using-rest-api/actions/workflows/main.yml/dispatches`,
1822
{ ref: "main" },
1923
{
2024
headers: {

0 commit comments

Comments
 (0)