-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Filipe Forattini
committed
Jul 9, 2022
1 parent
68f427e
commit 1a5c00c
Showing
13 changed files
with
1,233 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(', '), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 () {} | ||
} |