I’m migrating from Jenkins to GitHub Actions.
Jenkins code:
pipeline {
agent any
stages {
stage('Start') {
steps {
sh 'ls'
}
}
stage('Clone Repo') {
steps {
git credentialsId: 'githubapp', url: 'https://github..com/configurations.git', branch: 'dev'
}
}
stage('Invoke_configure_cache_pipeline') {
steps {
script {
sh 'cat cache.csv'
def csvFile = readFile(file: 'cache.csv')
csvFile.split('n').each {String line->
final String names = sh(script:"echo $line", returnStdout: true).trim()
def repos = line.tokenize(",").toList()
build job: 'Configurations/configure_cache', parameters: [
string(name: 'cacheName', value: repos[0]),
string(name: 'expirationType', value: repos[1]),
string(name: 'expirationTime', value: repos[2])
]
}
}
}
}
stage('End') {
steps {
sh 'ls'
}
}
}
}
GitHub Actions Code:
name: Bulk Test
on:
workflow_dispatch:
jobs:
build:
runs-on: [Name]
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: List files (Start)
run: |
ls
echo "*** List Files ***"
- name: Clone configurations repository
run: |
echo "*** Clone configurations repository ***"
git clone https://github.com/$userName/configurations.git
git checkout dev
git pull origin dev
git branch
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '14'
- name: Install dependencies
run: npm install node-fetch
- name: Read CSV and trigger workflows
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
const fs = require('fs');
const fetch = require('node-fetch');
async function triggerConfigureCacheWorkflow() {
const owner = 'Rohit';
const repo = 'config-pipeline-github-actions';
const workflow_id = 'Config-Cache';
const ref = 'dev';
const token = process.env.GITHUB_TOKEN;
const csvFile = fs.readFileSync('cache.csv', 'utf-8');
const lines = csvFile.split('n');
for (const line of lines) {
const repos = line.split(',').map(item => item.trim());
const cacheName = repos[0];
const expirationType = repos[1];
const expirationTime = repos[2];
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/actions/workflows/${workflow_id}/dispatches`, {
method: 'POST',
headers: {
'Authorization': `token ${token}`,
'Accept': 'application/vnd.github.v3+json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
ref,
inputs: {
cacheName,
expirationType,
expirationTime
}
})
});
if (!response.ok) {
throw new Error(`Error triggering workflow: ${response.statusText}`);
}
console.log(`Workflow triggered successfully for cacheName: ${cacheName}`);
}
}
triggerConfigureCacheWorkflow().catch(error => console.error(error));
- name: List files (End)
run: ls
Error: fatal: could not read Username for ‘https://github.vodafone.com’: No such device or address
My code is in GitHub and the repository that I’m trying to clone is also in GitHub, do I really need to provide an user? I doubt my code has lot of flaws.
Goal: From Source workflow, I want to trigger destination workflow multiple times to configure cache.