Skip to content

Commit

Permalink
feat: using actions in root dir
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipe Forattini committed Jul 9, 2022
1 parent 68f427e commit 1a5c00c
Show file tree
Hide file tree
Showing 13 changed files with 1,233 additions and 0 deletions.
9 changes: 9 additions & 0 deletions actions/pipeline-config-scrapper/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: 'Hello World'
description: 'Greet someone and record the time'
inputs: {}
outputs:
time: # id of output
description: 'The time we greeted you'
runs:
using: 'node16'
main: 'index.js'
14 changes: 14 additions & 0 deletions actions/pipeline-config-scrapper/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const _ = require('lodash')
const core = require('@actions/core');
const github = require('@actions/github');
const linguist = require('linguist-js');

try {
const { files, languages, unknown } = linguist(folder, options);

core.setOutput("language", languages);
const payload = JSON.stringify(github.context.payload, undefined, 2)
console.log(`The event payload: ${payload}`);
} catch (error) {
core.setFailed(error.message);
}
15 changes: 15 additions & 0 deletions actions/pipeline-config-scrapper/index.js

Large diffs are not rendered by default.

870 changes: 870 additions & 0 deletions actions/pipeline-config-scrapper/licenses.txt

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions actions/pipeline-config-scrapper/src/code.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const fs = require('fs')
const path = require('path')
const Scrapper = require('./scrapper.class')

module.exports = class Code extends Scrapper {
setup () {
const isNode = fs.existsSync(path.join(process.cwd(), 'package.json'))
const isPython = fs.existsSync(path.join(process.cwd(), 'requirements.txt'))

const language = isNode ? 'node' : isPython ? 'python' : 'unknown'

this
.add('code', {
isNode,
isPython,
language,
})
}
}
64 changes: 64 additions & 0 deletions actions/pipeline-config-scrapper/src/deploy.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const fs = require('fs')
const path = require('path')
const Scrapper = require('./scrapper.class')

module.exports = class Deploy extends Scrapper {
setup () {
const repository = this.context.payload.repository.name
const containerRegistry = this.inputs.containerRegistry

const podName = repository.includes('-svc-')
? 'svc'
: repository.includes('-app-')
? 'app'
: repository.includes('-iac-')
? 'iac'
: repository.includes('-mob-')
? 'mob'
: 'unknown'

const ecosystem = this.inputs.ecosystem || repository.split('-')[0]

const deployAsK8s = fs.existsSync(path.join(process.cwd(), 'manifests', 'k8s-values.yml'))
const deployAsChart = fs.existsSync(path.join(process.cwd(), 'manifests', 'charts-values.yml'))

let envs = [ 'dev', 'stg', 'prd', 'sbx', 'dry' ]

const { namespaces, secrets, configs, dependencies, buildArgs } = envs.reduce((acc, env) => {
acc.configs[env] = fs.existsSync(path.join(process.cwd(), 'manifests', 'configs', env + '.env'))
acc.dependencies[env] = fs.existsSync(path.join(process.cwd(), 'manifests', 'dependencies', env + '.yml'))
acc.namespaces[env] = `${repository}-${env}`
acc.secrets[env] = fs.existsSync(path.join(process.cwd(), 'manifests', 'secrets', env + '.gpg'))

acc.buildArgs[env] = acc.configs[env]
? fs.readFileSync(path.join(process.cwd(), 'manifests', 'configs', env + '.env')).toString().trim().split('\n').join(', ')
: ''

return acc
}, {
configs: {},
dependencies: {},
namespaces: {},
secrets: {},
buildArgs: {},
})

this
.add('dockerfile', {
buildArgs,
containerRegistry,
})
.add('deploy', {
configs,
containerRegistry,
dependencies,
deployAsChart,
deployAsK8s,
ecosystem,
namespace: repository,
namespaces,
podName,
secrets,
})
}
}
40 changes: 40 additions & 0 deletions actions/pipeline-config-scrapper/src/docker.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs')
const path = require('path')
const Scrapper = require('./scrapper.class')

module.exports = class Docker extends Scrapper {
setup () {
const hasDockerfile = fs.existsSync(path.join(process.cwd(), 'Dockerfile'))
const hasDockerignore = fs.existsSync(path.join(process.cwd(), '.dockerignore'))

const containerRegistry = this.inputs.containerRegistry
const containerName = this.context.payload.repository.full_name

const imageFullname = `${containerRegistry}/${containerName}`
const mainImage = `${imageFullname}:c-${this.output.git.commit}`

let tags = [
`${imageFullname}:latest`,
`${imageFullname}:d-${this.output.run.date}`,
`${imageFullname}:r-${this.output.run.count}`,
`${imageFullname}:t-${this.output.run.startTimestamp}`,
`${imageFullname}:b-${this.output.git.branch.replace('/', '-')}`,
`${imageFullname}:c-${this.output.git.commit}`,
// `${imageFullName}:node-${matrix.node-version}`,
// `${imageFullName}:node-${matrix.node-version}-latest`,
// `${imageFullName}:node-${matrix.node-version}-d-${needs.Setup.outputs.Date}`,
// `${imageFullName}:node-${matrix.node-version}-b-${needs.Setup.outputs.Branch}`,
// `${imageFullName}:node-${matrix.node-version}-c-${needs.Setup.outputs.ShaHash}`,
]

this
.add('dockerfile', {
hasDockerfile,
hasDockerignore,
imageFullname,
mainImage,
tags,
tagsAsString: tags.join(', '),
})
}
}
69 changes: 69 additions & 0 deletions actions/pipeline-config-scrapper/src/git.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const fs = require('fs')
const path = require('path')
const thread = require('child_process')
const Scrapper = require('./scrapper.class')

module.exports = class Git extends Scrapper {
setup () {
const commit = this.context.sha.substring(0, 7)
const repository = this.context.payload.repository.name
const organization = this.context.payload.repository.full_name.split('/')[0]
const branch = this.context.ref.replace('refs/heads/', '')

let firstName, surnames = []
if (this.context.payload.head_commit) {
[ firstName, ...surnames ] = this.context.payload.head_commit.committer.name.split(' ')
}

const hasReleaserc = fs.existsSync(path.join(process.cwd(), '.releaserc.json'))
? true
: false

this
.add('run', {
repository
})
.add('deploy', {
repository,
organization,
commitTag: `c-${commit}`,
})
.add('git', {
branch,
commit,
repository,
hasReleaserc,
organization,
repositoryFull: this.context.payload.repository.full_name,

fromContext: {
commit,
branch,
},

fromRepository: {
commit: this.getCommitShaFromRepository(),
branch: this.getBranchFromRepository(),
},

actor: {
...this.context.payload.head_commit.committer,
messages: this.context.payload.commits.map(c => c.message),
firstName,
lastName: surnames.pop(),
}
})
}

getCommitShaFromRepository (short = true) {
return thread.execSync(`git rev-parse ${short ? '--short' : ''} HEAD`)
.toString()
.trim()
}

getBranchFromRepository () {
return thread.execSync('git branch --show-current')
.toString()
.trim()
}
}
11 changes: 11 additions & 0 deletions actions/pipeline-config-scrapper/src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
Git: require('./git.class'),
Run: require('./run.class'),
Deploy: require('./deploy.class'),

// code related
Code: require('./code.class'),
Nodejs: require('./nodejs.class'),
Python: require('./python.class'),
Docker: require('./docker.class'),
}
47 changes: 47 additions & 0 deletions actions/pipeline-config-scrapper/src/nodejs.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const fs = require('fs')
const path = require('path')
const Scrapper = require('./scrapper.class')

module.exports = class Nodejs extends Scrapper {
setup () {
const hasYarnLock = fs.existsSync(path.join(process.cwd(), 'yarn.lock'))
const hasPackageLock = fs.existsSync(path.join(process.cwd(), 'package-lock.json'))

const entrypoint = [ "npm" ]
const command = [ "start" ]
const dockerignore = ['node_modules']

const dependencyCommand = hasYarnLock
? 'yarn install'
: hasPackageLock
? 'npm ci'
: 'npm install'

const cacheKey = hasYarnLock
? 'yarn.lock'
: hasPackageLock
? 'package-lock.json'
: 'package.json'

const dockerDependency = [ dependencyCommand ]
if (hasYarnLock) dockerDependency.push('yarn cache clean')

this
.add('code', {
cacheKey,
dependencyCommand,
})
.add('nodejs', {
cacheKey,
hasYarnLock,
hasPackageLock,
dependencyCommand,
})
.add('dockerfile', {
dockerignore,
entrypoint: JSON.stringify(entrypoint),
command: JSON.stringify(command),
dependencyCommand: JSON.stringify(dockerDependency),
})
}
}
25 changes: 25 additions & 0 deletions actions/pipeline-config-scrapper/src/python.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const Scrapper = require('./scrapper.class')

module.exports = class Python extends Scrapper {
setup () {
const entrypoint = [ "python" ]
const command = [ "app.py" ]
const dockerignore = ['']
const dependencyCommand = ['pip install --no-cache-dir -r requirements.txt']
const dockerDependency = [ dependencyCommand ]

this
.add('code', {
dependencyCommand,
})
.add('python', {
dependencyCommand,
})
.add('dockerfile', {
dockerignore,
entrypoint: JSON.stringify(entrypoint),
command: JSON.stringify(command),
dependencyCommand: JSON.stringify(dockerDependency),
})
}
}
16 changes: 16 additions & 0 deletions actions/pipeline-config-scrapper/src/run.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const Scrapper = require('./scrapper.class')

module.exports = class Run extends Scrapper {
setup () {
this
.add('run', {
id: this.context.runId,
jobTimestamp: Date.now(),
count: this.context.runNumber,
event: this.context.eventName,
date: this.context.payload.head_commit.timestamp.substring(0, 10),
startedAt: new Date(this.context.payload.head_commit.timestamp).toISOString(),
startTimestamp: new Date(this.context.payload.head_commit.timestamp).getTime(),
})
}
}
34 changes: 34 additions & 0 deletions actions/pipeline-config-scrapper/src/scrapper.class.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const mergeDeep = require('../../../src/merge-deep')

module.exports = class Scrapper {
constructor ({ github, context, inputs, core, glob, io, exec }, output) {
this.io = io
this.core = core
this.exec = exec
this.glob = glob
this.inputs = inputs
this.github = github
this.output = output
this.context = context

this.data = {}
}

static load (...args) {
const instance = new this(...args)
instance.setup()
return instance
}

add (key, data) {
this.data[key] = mergeDeep(
this.data[key] || {},
data
)

return this
}

// overwrite
setup () {}
}

0 comments on commit 1a5c00c

Please sign in to comment.