build and install dagger from source

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2022-04-25 21:19:33 +02:00
parent 7d447c263c
commit a4e1b392be
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
13 changed files with 152 additions and 12 deletions

View file

@ -82,3 +82,23 @@ jobs:
name: Check name: Check
run: | run: |
dagger version dagger version
build-ref:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ref:
- refs/tags/v0.2.5
- refs/pull/2288/head
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Dagger
uses: ./
with:
version: https://github.com/dagger/dagger.git#${{ matrix.ref }}
cmds: do test
workdir: ./test/ci

View file

@ -87,13 +87,13 @@ Following inputs can be used as `step.with` keys
> do test > do test
> ``` > ```
| Name | Type | Default | Description | | Name | Type | Default | Description |
|------------------|--------|--------------|------------------------------------------------| |------------------|--------|--------------|----------------------------------------------------------------------------------------|
| `version` | String | `latest` | Dagger version | | `version` | String | `latest` | Dagger version (e.g., `v0.2.7`, `latest`, `https://github.com/dagger/dagger.git#main`) |
| `cmds` | List | | List of Dagger commands | | `cmds` | List | | List of Dagger commands |
| `workdir` | String | `.` | Working directory (below repository root) | | `workdir` | String | `.` | Working directory (below repository root) |
| `install-only` | Bool | `false` | Just install Dagger | | `install-only` | Bool | `false` | Just install Dagger |
| `cleanup` | Bool | `true` | Cleanup Dagger home folder at the end of a job | | `cleanup` | Bool | `true` | Cleanup Dagger home folder at the end of a job |
## Development ## Development

View file

@ -1,6 +1,16 @@
import {describe, expect, it} from '@jest/globals'; import {describe, expect, jest, it} from '@jest/globals';
import * as fs from 'fs';
import * as path from 'path';
import * as context from '../src/context'; import * as context from '../src/context';
jest.spyOn(context, 'tmpDir').mockImplementation((): string => {
const tmpDir = path.join('/tmp/.dagger-jest').split(path.sep).join(path.posix.sep);
if (!fs.existsSync(tmpDir)) {
fs.mkdirSync(tmpDir, {recursive: true});
}
return tmpDir;
});
describe('getInputList', () => { describe('getInputList', () => {
it('handles single line correctly', async () => { it('handles single line correctly', async () => {
await setInput('foo', 'bar'); await setInput('foo', 'bar');

View file

@ -2,6 +2,13 @@ import {describe, expect, it} from '@jest/globals';
import * as fs from 'fs'; import * as fs from 'fs';
import * as dagger from '../src/dagger'; import * as dagger from '../src/dagger';
describe('build', () => {
it.skip('valid', async () => {
const daggerBin = await dagger.build('https://github.com/dagger/dagger.git#refs/pull/2161/head');
expect(fs.existsSync(daggerBin)).toBe(true);
}, 100000);
});
describe('install', () => { describe('install', () => {
it('acquires latest version of Dagger', async () => { it('acquires latest version of Dagger', async () => {
const daggerBin = await dagger.install('latest'); const daggerBin = await dagger.install('latest');

9
__tests__/git.test.ts Normal file
View file

@ -0,0 +1,9 @@
import {describe, expect, it} from '@jest/globals';
import * as git from '../src/git';
describe('git', () => {
it('returns git remote ref', async () => {
const ref: string = await git.getRemoteSha('https://github.com/dagger/dagger.git', 'refs/pull/2161/head');
expect(ref).toEqual('aeb8ea3973a7815fff7cbd16f986811baa08ae2f');
});
});

12
__tests__/util.test.ts Normal file
View file

@ -0,0 +1,12 @@
import {describe, expect, test} from '@jest/globals';
import * as util from '../src/util';
describe('isValidUrl', () => {
test.each([
['https://github.com/dagger/dagger.git', true],
['https://github.com/dagger/dagger.git#refs/pull/2161/head', true],
['v0.2.7', false]
])('given %p', async (url, expected) => {
expect(util.isValidUrl(url)).toEqual(expected);
});
});

2
dist/index.js generated vendored

File diff suppressed because one or more lines are too long

2
dist/index.js.map generated vendored

File diff suppressed because one or more lines are too long

View file

@ -1,5 +1,17 @@
import fs from 'fs';
import * as os from 'os';
import path from 'path';
import * as core from '@actions/core'; import * as core from '@actions/core';
let _tmpDir: string;
export function tmpDir(): string {
if (!_tmpDir) {
_tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'dagger-')).split(path.sep).join(path.posix.sep);
}
return _tmpDir;
}
export interface Inputs { export interface Inputs {
version: string; version: string;
workdir: string; workdir: string;

View file

@ -1,7 +1,10 @@
import * as os from 'os'; import * as os from 'os';
import * as path from 'path'; import * as path from 'path';
import * as util from 'util'; import * as util from 'util';
import * as context from './context';
import * as git from './git';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec';
import * as http from '@actions/http-client'; import * as http from '@actions/http-client';
import * as tc from '@actions/tool-cache'; import * as tc from '@actions/tool-cache';
@ -9,6 +12,35 @@ const s3URL = 'https://dl.dagger.io/dagger';
const osPlat: string = os.platform(); const osPlat: string = os.platform();
const osArch: string = os.arch(); const osArch: string = os.arch();
export async function build(inputBuildRef: string): Promise<string> {
// eslint-disable-next-line prefer-const
let [repo, ref] = inputBuildRef.split('#');
if (ref.length == 0) {
ref = 'main';
}
const sha = await git.getRemoteSha(repo, ref);
core.debug(`Remote ref ${sha} found`);
let toolPath: string;
toolPath = tc.find('dagger', sha);
if (!toolPath) {
const outFolder = path.join(context.tmpDir(), 'out').split(path.sep).join(path.posix.sep);
toolPath = await exec
.getExecOutput('docker', ['buildx', 'build', '--build-arg', 'BUILDKIT_CONTEXT_KEEP_GIT_DIR=1', '--output', `type=local,dest=${outFolder}`, inputBuildRef], {
ignoreReturnCode: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
core.warning(res.stderr.trim());
}
return tc.cacheFile(`${outFolder}/bin/dagger`, osPlat == 'win32' ? 'dagger.exe' : 'dagger', 'dagger', sha);
});
}
return path.join(toolPath, osPlat == 'win32' ? 'dagger.exe' : 'dagger');
}
export async function install(version: string): Promise<string> { export async function install(version: string): Promise<string> {
version = await getVersionMapping(version); version = await getVersionMapping(version);
version = version.replace(/^v/, ''); version = version.replace(/^v/, '');

19
src/git.ts Normal file
View file

@ -0,0 +1,19 @@
import * as exec from '@actions/exec';
export async function getRemoteSha(repo: string, ref: string): Promise<string> {
return await exec
.getExecOutput(`git`, ['ls-remote', repo, ref], {
ignoreReturnCode: true,
silent: true
})
.then(res => {
if (res.stderr.length > 0 && res.exitCode != 0) {
throw new Error(res.stderr);
}
const [rsha] = res.stdout.trim().split(/[\s\t]/);
if (rsha.length == 0) {
throw new Error(`Cannot find remote ref for ${repo}#${ref}`);
}
return rsha;
});
}

View file

@ -4,13 +4,24 @@ import os from 'os';
import * as context from './context'; import * as context from './context';
import * as dagger from './dagger'; import * as dagger from './dagger';
import * as stateHelper from './state-helper'; import * as stateHelper from './state-helper';
import * as util from './util';
import * as core from '@actions/core'; import * as core from '@actions/core';
import * as exec from '@actions/exec'; import * as exec from '@actions/exec';
async function run(): Promise<void> { async function run(): Promise<void> {
try { try {
const inputs: context.Inputs = await context.getInputs(); const inputs: context.Inputs = await context.getInputs();
const daggerBin = await dagger.install(inputs.version);
let daggerBin;
if (util.isValidUrl(inputs.version)) {
core.startGroup(`Build and install Dagger`);
daggerBin = await dagger.build(inputs.version);
core.endGroup();
} else {
core.startGroup(`Download and install Dagger`);
daggerBin = await dagger.install(inputs.version);
core.endGroup();
}
if (inputs.installOnly) { if (inputs.installOnly) {
const daggerDir = path.dirname(daggerBin); const daggerDir = path.dirname(daggerBin);
@ -22,7 +33,7 @@ async function run(): Promise<void> {
} }
if (inputs.workdir && inputs.workdir !== '.') { if (inputs.workdir && inputs.workdir !== '.') {
core.info(`Using ${inputs.workdir} as working directory`); core.debug(`Using ${inputs.workdir} as working directory`);
process.chdir(inputs.workdir); process.chdir(inputs.workdir);
} }

8
src/util.ts Normal file
View file

@ -0,0 +1,8 @@
export function isValidUrl(url: string): boolean {
try {
new URL(url);
} catch (e) {
return false;
}
return true;
}