Add setup-buildx action (#71)

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-08-07 11:01:05 +02:00
parent 836357fa9e
commit 6df1822dc3
No known key found for this signature in database
GPG key ID: 3248E46B6BB8C7F7
20 changed files with 11823 additions and 0 deletions

40
.github/workflows/setup-buildx-ci.yml vendored Normal file
View file

@ -0,0 +1,40 @@
name: setup-buildx-ci
on:
push:
paths:
- .github/workflows/setup-buildx-ci.yml
- setup-buildx/**
pull_request:
paths:
- .github/workflows/setup-buildx-ci.yml
- setup-buildx/**
jobs:
build:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
buildx-version:
- latest
- v0.2.2
steps:
-
name: Runner info
run: |
sudo apt-get install -y hwinfo
sudo hwinfo --short
sudo mount
-
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Set up Docker Buildx
id: buildx
uses: ./setup-buildx/
with:
buildx-version: ${{ matrix.buildx-version }}
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}

View file

@ -0,0 +1,32 @@
name: setup-buildx-precheckin
on:
push:
paths:
- .github/workflows/setup-buildx-precheckin.yml
- setup-buildx/**
pull_request:
paths:
- .github/workflows/setup-buildx-precheckin.yml
- setup-buildx/**
jobs:
pre-checkin:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Install
run: yarn --cwd ./setup-buildx/ install
-
name: Pre-checkin
run: yarn --cwd ./setup-buildx/ run pre-checkin
-
name: Check for uncommitted changes
run: |
if [[ `git status --porcelain` ]]; then
git status --porcelain
echo "::warning::Found changes. Please run 'yarn --cwd ./setup-buildx/ run pre-checkin' and push"
fi

32
.github/workflows/setup-buildx-test.yml vendored Normal file
View file

@ -0,0 +1,32 @@
name: setup-buildx-test
on:
push:
paths:
- .github/workflows/setup-buildx-test.yml
- setup-buildx/**
pull_request:
paths:
- .github/workflows/setup-buildx-test.yml
- setup-buildx/**
jobs:
test:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Install
run: yarn install
-
name: Test
run: yarn run test
# -
# name: Upload coverage
# uses: codecov/codecov-action@v1.0.7
# if: success()
# with:
# token: ${{ secrets.CODECOV_TOKEN }}
# file: ./coverage/clover.xml

View file

@ -0,0 +1,15 @@
# This file is for unifying the coding style for different editors and IDEs.
# More information at http://editorconfig.org
root = true
[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false

2
setup-buildx/.gitattributes vendored Normal file
View file

@ -0,0 +1,2 @@
/dist/** linguist-generated=true
/lib/** linguist-generated=true

95
setup-buildx/.gitignore vendored Normal file
View file

@ -0,0 +1,95 @@
node_modules
lib
# Jetbrains
/.idea
/*.iml
# Rest of the file pulled from https://github.com/github/gitignore/blob/master/Node.gitignore
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
jspm_packages/
# TypeScript v1 declaration files
typings/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
.env.test
# parcel-bundler cache (https://parceljs.org/)
.cache
# next.js build output
.next
# nuxt.js build output
.nuxt
# vuepress build output
.vuepress/dist
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/

View file

@ -0,0 +1,11 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"trailingComma": "none",
"bracketSpacing": false,
"arrowParens": "avoid",
"parser": "typescript"
}

75
setup-buildx/README.md Normal file
View file

@ -0,0 +1,75 @@
## About
GitHub Action to set up Docker [Buildx](https://github.com/docker/buildx).
___
* [Usage](#usage)
* [Quick start](#quick-start)
* [Customizing](#customizing)
* [inputs](#inputs)
* [outputs](#outputs)
* [environment variables](#environment-variables)
* [Limitation](#limitation)
## Usage
### Quick start
```yaml
name: ci
on:
pull_request:
branches: master
push:
branches: master
tags:
jobs:
buildx:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2
-
name: Set up Docker Buildx
id: buildx
uses: docker/action/setup-buildx@v2
with:
buildx-version: latest
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
```
## Customizing
### inputs
Following inputs can be used as `step.with` keys
| Name | Type | Default | Description |
|------------------|---------|-----------|------------------------------------|
| `buildx-version` | String | `latest` | [Buildx](https://github.com/docker/buildx) version. Example: `v0.3.0` |
### outputs
Following outputs are available
| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `platforms` | String | Available platforms (comma separated) |
### environment variables
The following [official docker environment variables](https://docs.docker.com/engine/reference/commandline/cli/#environment-variables) are supported:
| Name | Type | Default | Description |
|-----------------|---------|-------------|-------------------------------------------------|
| `DOCKER_CONFIG` | String | `~/.docker` | The location of your client configuration files |
## Limitation
This action is only available for Linux [virtual environments](https://help.github.com/en/articles/virtual-environments-for-github-actions#supported-virtual-environments-and-hardware-resources).

View file

@ -0,0 +1,17 @@
import * as github from '../src/github';
describe('github', () => {
it('returns latest buildx GitHub release', async () => {
const release = await github.getRelease('latest');
console.log(release);
expect(release).not.toBeNull();
expect(release?.tag_name).not.toEqual('');
});
it('returns v0.2.2 buildx GitHub release', async () => {
const release = await github.getRelease('v0.2.2');
console.log(release);
expect(release).not.toBeNull();
expect(release?.tag_name).toEqual('v0.2.2');
});
});

View file

@ -0,0 +1,20 @@
import fs = require('fs');
import * as installer from '../src/installer';
import * as path from 'path';
import * as os from 'os';
const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'ghaction-docker-buildx-'));
describe('installer', () => {
it('acquires v0.2.2 version of buildx', async () => {
const buildx = await installer.buildx('v0.2.2', tmpDir);
console.log(buildx);
expect(fs.existsSync(buildx)).toBe(true);
}, 100000);
it('acquires latest version of buildx', async () => {
const buildx = await installer.buildx('latest', tmpDir);
console.log(buildx);
expect(fs.existsSync(buildx)).toBe(true);
}, 100000);
});

22
setup-buildx/action.yml Normal file
View file

@ -0,0 +1,22 @@
# https://help.github.com/en/articles/metadata-syntax-for-github-actions
name: 'Docker - Setup Buildx'
description: 'GitHub Action to set up Docker Buildx'
author: 'crazy-max'
branding:
color: 'blue'
icon: 'truck'
inputs:
buildx-version:
description: 'Buildx version. Example: v0.3.0'
default: 'latest'
required: false
outputs:
platforms:
description: 'Available platforms (comma separated)'
runs:
using: 'node12'
main: 'dist/index.js'
post: 'dist/index.js'

7446
setup-buildx/dist/index.js generated vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,12 @@
module.exports = {
clearMocks: true,
moduleFileExtensions: ['js', 'ts'],
setupFiles: ["dotenv/config"],
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
testRunner: 'jest-circus/runner',
transform: {
'^.+\\.ts$': 'ts-jest'
},
verbose: false
}

44
setup-buildx/package.json Normal file
View file

@ -0,0 +1,44 @@
{
"name": "setup-buildx",
"description": "GitHub Action to set up Docker Buildx",
"main": "lib/main.js",
"scripts": {
"build": "tsc && ncc build",
"format": "prettier --write **/*.ts",
"format-check": "prettier --check **/*.ts",
"test": "jest --coverage",
"pre-checkin": "yarn run format && yarn run build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/docker/action.git",
"directory": "setup-buildx"
},
"keywords": [
"actions",
"docker",
"buildx"
],
"author": "CrazyMax",
"license": "Apache-2.0",
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4",
"@actions/http-client": "^1.0.8",
"@actions/tool-cache": "^1.5.5",
"semver": "^7.3.2"
},
"devDependencies": {
"@types/jest": "^26.0.3",
"@types/node": "^14.0.14",
"@zeit/ncc": "^0.22.3",
"dotenv": "^8.2.0",
"jest": "^26.1.0",
"jest-circus": "^26.1.0",
"jest-runtime": "^26.1.0",
"prettier": "^2.0.5",
"ts-jest": "^26.1.1",
"typescript": "^3.9.5",
"typescript-formatter": "^7.2.2"
}
}

View file

@ -0,0 +1,12 @@
import * as httpm from '@actions/http-client';
export interface GitHubRelease {
id: number;
tag_name: string;
}
export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
const url: string = `https://github.com/docker/buildx/releases/${version}`;
const http: httpm.HttpClient = new httpm.HttpClient('ghaction-docker-buildx');
return (await http.getJson<GitHubRelease>(url)).result;
};

View file

@ -0,0 +1,66 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as semver from 'semver';
import * as util from 'util';
import * as github from './github';
import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';
const osPlat: string = os.platform();
export async function buildx(inputVersion: string, dockerConfigHome: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(inputVersion);
if (!release) {
throw new Error(`Cannot find buildx ${inputVersion} release`);
}
core.debug(`Release found: ${release.tag_name}`);
const version = release.tag_name.replace(/^v+|v+$/g, '');
let toolPath: string;
toolPath = tc.find('buildx', version);
if (!toolPath) {
const c = semver.clean(version) || '';
if (!semver.valid(c)) {
throw new Error(`Invalid Buildx version "${version}".`);
}
toolPath = await installBuildx(version);
}
const pluginsDir: string = path.join(dockerConfigHome, 'cli-plugins');
core.debug(`Plugins dir is ${pluginsDir}`);
if (!fs.existsSync(pluginsDir)) {
fs.mkdirSync(pluginsDir, {recursive: true});
}
const filename: string = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const pluginPath: string = path.join(pluginsDir, filename);
core.debug(`Plugin path is ${pluginPath}`);
fs.copyFileSync(path.join(toolPath, filename), pluginPath);
core.info('🔨 Fixing perms...');
fs.chmodSync(pluginPath, '0755');
return pluginPath;
}
async function installBuildx(version: string): Promise<string> {
version = semver.clean(version) || '';
const platform: string = osPlat == 'win32' ? 'windows' : osPlat;
const ext: string = osPlat == 'win32' ? '.exe' : '';
const filename: string = util.format('buildx-v%s.%s-amd64%s', version, platform, ext);
const targetFile: string = osPlat == 'win32' ? 'docker-buildx.exe' : 'docker-buildx';
const downloadUrl = util.format('https://github.com/docker/buildx/releases/download/v%s/%s', version, filename);
let downloadPath: string;
try {
core.info(`⬇️ Downloading ${downloadUrl}...`);
downloadPath = await tc.downloadTool(downloadUrl);
core.debug(`Downloaded to ${downloadPath}`);
} catch (error) {
throw error;
}
return await tc.cacheFile(downloadPath, targetFile, 'buildx', version);
}

71
setup-buildx/src/main.ts Normal file
View file

@ -0,0 +1,71 @@
import * as child_process from 'child_process';
import * as os from 'os';
import * as path from 'path';
import * as installer from './installer';
import * as stateHelper from './state-helper';
import * as core from '@actions/core';
import * as exec from '@actions/exec';
async function run(): Promise<void> {
try {
if (os.platform() !== 'linux') {
core.setFailed('Only supported on linux platform');
return;
}
const buildxVer: string = core.getInput('buildx-version') || 'latest';
const dockerConfigHome: string = process.env.DOCKER_CONFIG || path.join(os.homedir(), '.docker');
await installer.buildx(buildxVer, dockerConfigHome);
core.info('📣 Buildx info');
await exec.exec('docker', ['buildx', 'version']);
core.info('🔨 Creating a new builder instance...');
await exec.exec('docker', [
'buildx',
'create',
'--name',
`builder-${process.env.GITHUB_SHA}`,
'--driver',
'docker-container',
'--use'
]);
core.info('🏃 Booting builder...');
await exec.exec('docker', ['buildx', 'inspect', '--bootstrap']);
core.info('🐳 Docker info');
await exec.exec('docker', ['info']);
core.info('🛒 Extracting available platforms...');
const inspect = child_process.execSync('docker buildx inspect', {
encoding: 'utf8'
});
for (const line of inspect.split(os.EOL)) {
if (line.startsWith('Platforms')) {
core.setOutput('platforms', line.replace('Platforms: ', '').replace(/\s/g, '').trim());
break;
}
}
} catch (error) {
core.setFailed(error.message);
}
}
async function cleanup(): Promise<void> {
try {
core.info('🚿 Removing builder instance...');
await exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`]);
} catch (error) {
core.warning(error.message);
}
}
// Main
if (!stateHelper.IsPost) {
run();
}
// Post
else {
cleanup();
}

View file

@ -0,0 +1,14 @@
// From https://github.com/actions/checkout/blob/master/src/state-helper.ts
import * as coreCommand from '@actions/core/lib/command';
/**
* Indicates whether the POST action is running
*/
export const IsPost = !!process.env['STATE_isPost'];
// Publish a variable so that when the POST action runs, it can determine it should run the cleanup logic.
// This is necessary since we don't have a separate entry point.
if (!IsPost) {
coreCommand.issueCommand('save-state', {name: 'isPost'}, 'true');
}

View file

@ -0,0 +1,18 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"lib": [
"es6",
"dom"
],
"newLine": "lf",
"outDir": "./lib",
"rootDir": "./src",
"strict": true,
"noImplicitAny": false,
"esModuleInterop": true,
"sourceMap": true
},
"exclude": ["node_modules", "**/*.test.ts"]
}

3779
setup-buildx/yarn.lock Normal file

File diff suppressed because it is too large Load diff