Skip to content

Commit

Permalink
Add name output to identify builder instance name
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
CrazyMax committed Aug 12, 2020
1 parent b72149b commit 0cc5e42
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 20 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/setup-buildx-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,37 @@ jobs:
uses: ./setup-buildx/
with:
buildx-version: ${{ matrix.buildx-version }}
-
name: Builder instance name
run: echo ${{ steps.buildx.outputs.name }}
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
-
name: Dump context
uses: crazy-max/ghaction-dump-context@v1

multi:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v2.3.1
-
name: Set up Docker Buildx 1
id: buildx1
uses: ./setup-buildx/
-
name: Builder 1 instance name
run: echo ${{ steps.buildx1.outputs.name }}
-
name: Set up Docker Buildx 2
id: buildx2
uses: ./setup-buildx/
-
name: Builder 2 instance name
run: echo ${{ steps.buildx2.outputs.name }}

install:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -135,3 +159,6 @@ jobs:
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
-
name: Builder instance name
run: echo ${{ steps.buildx.outputs.name }}
4 changes: 4 additions & 0 deletions setup-buildx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ jobs:
uses: docker/actions/setup-buildx@v1
with:
buildx-version: latest
-
name: Builder instance name
run: echo ${{ steps.buildx.outputs.name }}
-
name: Available platforms
run: echo ${{ steps.buildx.outputs.platforms }}
Expand All @@ -102,6 +105,7 @@ Following outputs are available

| Name | Type | Description |
|---------------|---------|---------------------------------------|
| `name` | String | Builder instance name |
| `platforms` | String | Available platforms (comma separated) |

### environment variables
Expand Down
8 changes: 7 additions & 1 deletion setup-buildx/__tests__/buildx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ const tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'setup-buildx-'));
describe('buildx', () => {
it('is available', async () => {
expect(await buildx.isAvailable()).toBe(true);
}, 100000);
});

it('count builders', async () => {
const countBuilders = await buildx.countBuilders();
console.log(`countBuilders: ${countBuilders}`);
expect(countBuilders).toBeGreaterThan(0);
});

it('acquires v0.2.2 version of buildx', async () => {
const buildxBin = await buildx.install('v0.2.2', tmpDir);
Expand Down
2 changes: 2 additions & 0 deletions setup-buildx/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ inputs:
required: false

outputs:
name:
description: 'Builder instance name'
platforms:
description: 'Available platforms (comma separated)'

Expand Down
27 changes: 17 additions & 10 deletions setup-buildx/dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions setup-buildx/src/buildx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ export async function isAvailable(): Promise<Boolean> {
});
}

export async function countBuilders(): Promise<number> {
return await exec.exec(`docker`, ['buildx', 'ls'], true).then(res => {
if (res.stderr != '' && !res.success) {
throw new Error(`Cannot list builders: ${res.stderr}`);
}
return (res.stdout.trim().split(`\n`).length - 1) / 2;
});
}

export async function install(inputVersion: string, dockerConfigHome: string): Promise<string> {
const release: github.GitHubRelease | null = await github.getRelease(inputVersion);
if (!release) {
Expand Down
15 changes: 6 additions & 9 deletions setup-buildx/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ async function run(): Promise<void> {
core.info('📣 Buildx info');
await exec.exec('docker', ['buildx', 'version'], false);

const builderName: string = `builder-${(await buildx.countBuilders()) + 1}-${process.env.GITHUB_JOB}`;
core.saveState('builderName', builderName);
core.setOutput('name', builderName);

core.info('🔨 Creating a new builder instance...');
let createArgs: Array<string> = [
'buildx',
'create',
'--name',
`builder-${process.env.GITHUB_SHA}`,
'--driver',
driver
];
let createArgs: Array<string> = ['buildx', 'create', '--name', builderName, '--driver', driver];
if (driverOpt) {
createArgs.push('--driver-opt', driverOpt);
}
Expand Down Expand Up @@ -76,7 +73,7 @@ async function run(): Promise<void> {
async function cleanup(): Promise<void> {
try {
core.info('🚿 Removing builder instance...');
await exec.exec('docker', ['buildx', 'rm', `builder-${process.env.GITHUB_SHA}`], false);
await exec.exec('docker', ['buildx', 'rm', `${process.env.STATE_builderName}`], false);
} catch (error) {
core.warning(error.message);
}
Expand Down

0 comments on commit 0cc5e42

Please sign in to comment.