Skip to content

Commit

Permalink
Refactor internals (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Firehed authored Aug 3, 2021
1 parent b6fccaa commit 2957b45
Show file tree
Hide file tree
Showing 4 changed files with 236 additions and 202 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/self-test-multistage-docker-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ jobs:
stages: env, configured
testenv-stage: testenv
server-stage: server
quiet: false

test:
name: 'test'
Expand Down Expand Up @@ -83,3 +84,4 @@ jobs:
run: |
echo "Server: ${{ steps.build.outputs.server-tag }}"
echo "Testenv: ${{ steps.build.outputs.testenv-tag }}"
echo "Commit: ${{ steps.build.outputs.commit }}"
205 changes: 105 additions & 100 deletions dist/index.js

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

56 changes: 56 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from '@actions/core'
import { exec } from '@actions/exec'
import * as github from '@actions/github'

// Returns a string like "refs_pull_1_merge-bk1"
Expand Down Expand Up @@ -51,3 +52,58 @@ export function getAllStages(): string[] {
}
return stages
}

/**
* Takes the build stage and returns an untagged image name for it
*/
function getUntaggedImageForStage(stage: string): string {
const repo = core.getInput('repository')
return `${repo}/${stage}`
}

export function getTaggedImageForStage(stage: string, tag: string): string {
const image = getUntaggedImageForStage(stage)
return `${image}:${tag}`
}

type DockerCommand = 'pull' | 'push' | 'build' | 'tag'

interface ExecResult {
exitCode: number
stderr: string
stdout: string
}

/**
* Runs a docker command and returns the output. Unlike exec.exec, this does
* not throw on a nonzero exit code.
*/
export async function runDockerCommand(command: DockerCommand, ...args: string[]): Promise<ExecResult> {
let rest: string[] = [command]
if (core.getBooleanInput('quiet')) {
rest.push('--quiet')
}
rest.push(...args)

let stdout = ''
let stderr = ''

const execOptions = {
ignoreReturnCode: true, // Will do manual error handling
listeners: {
stderr: (data: Buffer) => {
stderr += data.toString()
},
stdout: (data: Buffer) => {
stdout += data.toString()
},
}
}
const exitCode = await exec('docker', rest, execOptions)

return {
exitCode,
stderr,
stdout,
}
}
Loading

0 comments on commit 2957b45

Please sign in to comment.