action-debian-package/main.js
2020-03-26 18:58:33 +01:00

151 lines
4.7 KiB
JavaScript

const core = require("@actions/core")
const exec = require("@actions/exec")
const firstline = require("firstline")
const hub = require("docker-hub-utils")
const path = require("path")
const fs = require("fs")
function getDistribution(distribution) {
return distribution.replace("UNRELEASED", "unstable")
.replace("-security", "")
.replace("-backports", "")
}
async function getOS(distribution) {
for (const os of ["debian", "ubuntu"]) {
const tags = await hub.queryTags({ user: "library", name: os })
if (tags.find(tag => tag.name == distribution)) {
return os
}
}
}
async function main() {
try {
const sourceRelativeDirectory = core.getInput("source_directory")
const artifactsRelativeDirectory = core.getInput("artifacts_directory")
const workspaceDirectory = process.cwd()
const sourceDirectory = path.join(workspaceDirectory, sourceRelativeDirectory)
const buildDirectory = path.dirname(sourceDirectory)
const artifactsDirectory = path.join(workspaceDirectory, artifactsRelativeDirectory)
const file = path.join(sourceDirectory, "debian/changelog")
const changelog = await firstline(file)
const regex = /^(?<package>.+) \(((?<epoch>[0-9]+):)?(?<version>[^:-]+)(-(?<revision>[^:-]+))?\) (?<distribution>.+);/
const match = changelog.match(regex)
const { package, epoch, version, revision, distribution } = match.groups
const os = await getOS(getDistribution(distribution))
const container = package
const image = os + ":" + getDistribution(distribution)
fs.mkdirSync(artifactsDirectory, { recursive: true })
core.startGroup("Print details")
const details = {
package: package,
epoch: epoch,
version: version,
revision: revision,
distribution: getDistribution(distribution),
os: os,
container: container,
image: image,
workspaceDirectory: workspaceDirectory,
sourceDirectory: sourceDirectory,
buildDirectory: buildDirectory,
artifactsDirectory: artifactsDirectory
}
console.log(details)
core.endGroup()
core.startGroup("Create container")
await exec.exec("docker", [
"create",
"--name", container,
"--volume", workspaceDirectory + ":" + workspaceDirectory,
"--workdir", sourceDirectory,
"--env", "DEBIAN_FRONTEND=noninteractive",
"--env", "DPKG_COLORS=always",
"--env", "FORCE_UNSAFE_CONFIGURE=1",
"--tty",
image,
"sleep", "inf"
])
core.endGroup()
core.startGroup("Start container")
await exec.exec("docker", [
"start",
container
])
core.endGroup()
if (revision) {
core.startGroup("Create tarball")
await exec.exec("docker", [
"exec",
container,
"tar",
"--exclude-vcs",
"--exclude", "./debian",
"--transform", `s/^\./${package}-${version}/`,
"-cvzf", `${buildDirectory}/${package}_${version}.orig.tar.gz`,
"-C", sourceDirectory,
"./"
])
core.endGroup()
}
core.startGroup("Update packages list")
await exec.exec("docker", [
"exec",
container,
"apt-get", "update"
])
core.endGroup()
core.startGroup("Install development packages")
await exec.exec("docker", [
"exec",
container,
"apt-get", "install", "-yq", "dpkg-dev", "debhelper"
])
core.endGroup()
core.startGroup("Install build dependencies")
await exec.exec("docker", [
"exec",
container,
"apt-get", "build-dep", "-yq", sourceDirectory
])
core.endGroup()
core.startGroup("Build package")
await exec.exec("docker", [
"exec",
container,
"dpkg-buildpackage", "-tc"
])
core.endGroup()
core.startGroup("Move artifacts")
await exec.exec("docker", [
"exec",
container,
"find",
buildDirectory,
"-maxdepth", "1",
"-name", `${package}_${version}*.*`,
"-type", "f",
"-print",
"-exec", "mv", "{}", artifactsDirectory, ";"
])
core.endGroup()
} catch (error) {
core.setFailed(error.message)
}
}
main()