Skip to content

Commit

Permalink
Merge pull request #95 from stuartleeks/sl/image-tag
Browse files Browse the repository at this point in the history
Add imageTag option
  • Loading branch information
stuartleeks authored Jul 20, 2021
2 parents 9b2e8cc + 586a62b commit bc711cd
Show file tree
Hide file tree
Showing 26 changed files with 352 additions and 123 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ jobs:
- test-gh-dockerfile-context
- test-gh-docker-from-docker-non-root
- test-gh-docker-from-docker-root
- test-gh-image-tag
if: github.ref == 'refs/heads/main' # only create release on `main`
env:
VERSION: ${{ needs.build.outputs.version }}
Expand Down Expand Up @@ -360,6 +361,42 @@ jobs:
runCmd: make docker-build


test-gh-image-tag:
name: Run GitHub image-tag test
runs-on: ubuntu-latest
needs: build
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1

- name: Login to GitHub Container Registry
uses: docker/login-action@v1
if: github.ref == 'refs/heads/main' # only need this for push (on `main`)
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Run test
uses: ./
with:
subFolder: github-tests/image-tag
imageName: ghcr.io/stuartleeks/devcontainer-build-run/tests/image-tag
imageTag: custom-tag
runCmd: |
echo "Get dev container..."
container_id=$(docker ps --latest --format "{{.ID}}" --no-trunc)
echo "Container ID: $container_id"
container_image=$(docker inspect $container_id --format "{{.Config.Image}}")
echo "Container Image: $container_image"
container_tag=$(echo $container_image | sed s/[^:].*://g)
echo "Container tag: $container_tag"
[[ $container_tag == "custom-tag" ]]
# - name: fail
# run: |
# echo "::error file=app.js,line=10,col=15::Something went wrong"
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ inputs:
imageName:
required: true
description: Image name (including registry)
imageTag:
required: false
description: Image tag (defaults to latest)
runCmd:
required: true
description: Specify the command to run after building the dev container image
Expand Down
6 changes: 3 additions & 3 deletions azdo-task/DevContainerBuildRun/dist/docker.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ExecFunction } from './exec';
export declare function isDockerBuildXInstalled(exec: ExecFunction): Promise<boolean>;
export declare function buildImage(exec: ExecFunction, imageName: string, checkoutPath: string, subFolder: string): Promise<string>;
export declare function runContainer(exec: ExecFunction, imageName: string, checkoutPath: string, subFolder: string, command: string, envs?: string[], mounts?: string[]): Promise<void>;
export declare function pushImage(exec: ExecFunction, imageName: string): Promise<void>;
export declare function buildImage(exec: ExecFunction, imageName: string, imageTag: string | undefined, checkoutPath: string, subFolder: string): Promise<string>;
export declare function runContainer(exec: ExecFunction, imageName: string, imageTag: string | undefined, checkoutPath: string, subFolder: string, command: string, envs?: string[], mounts?: string[]): Promise<void>;
export declare function pushImage(exec: ExecFunction, imageName: string, imageTag: string | undefined): Promise<void>;
export interface DockerMount {
type: string;
source: string;
Expand Down
53 changes: 28 additions & 25 deletions azdo-task/DevContainerBuildRun/dist/index.js

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

2 changes: 1 addition & 1 deletion azdo-task/DevContainerBuildRun/dist/index.js.map

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions azdo-task/DevContainerBuildRun/lib/docker.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ function isDockerBuildXInstalled() {
});
}
exports.isDockerBuildXInstalled = isDockerBuildXInstalled;
function buildImage(imageName, checkoutPath, subFolder) {
function buildImage(imageName, imageTag, checkoutPath, subFolder) {
return __awaiter(this, void 0, void 0, function* () {
console.log('🏗 Building dev container...');
try {
return yield docker.buildImage(exec_1.exec, imageName, checkoutPath, subFolder);
return yield docker.buildImage(exec_1.exec, imageName, imageTag, checkoutPath, subFolder);
}
catch (error) {
task.setResult(task.TaskResult.Failed, error);
Expand All @@ -51,11 +51,11 @@ function buildImage(imageName, checkoutPath, subFolder) {
});
}
exports.buildImage = buildImage;
function runContainer(imageName, checkoutPath, subFolder, command, envs) {
function runContainer(imageName, imageTag, checkoutPath, subFolder, command, envs) {
return __awaiter(this, void 0, void 0, function* () {
console.log('🏃‍♀️ Running dev container...');
try {
yield docker.runContainer(exec_1.exec, imageName, checkoutPath, subFolder, command, envs);
yield docker.runContainer(exec_1.exec, imageName, imageTag, checkoutPath, subFolder, command, envs);
return true;
}
catch (error) {
Expand All @@ -65,11 +65,11 @@ function runContainer(imageName, checkoutPath, subFolder, command, envs) {
});
}
exports.runContainer = runContainer;
function pushImage(imageName) {
function pushImage(imageName, imageTag) {
return __awaiter(this, void 0, void 0, function* () {
console.log('📌 Pushing image...');
try {
yield docker.pushImage(exec_1.exec, imageName);
yield docker.pushImage(exec_1.exec, imageName, imageTag);
return true;
}
catch (error) {
Expand Down
10 changes: 6 additions & 4 deletions azdo-task/DevContainerBuildRun/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,19 @@ function runMain() {
task.setResult(task.TaskResult.Failed, 'imageName input is required');
return;
}
const imageTag = task.getInput('imageTag');
const subFolder = (_b = task.getInput('subFolder')) !== null && _b !== void 0 ? _b : '.';
const runCommand = task.getInput('runCmd', true);
if (!runCommand) {
task.setResult(task.TaskResult.Failed, 'runCmd input is required');
return;
}
const envs = (_d = (_c = task.getInput('env')) === null || _c === void 0 ? void 0 : _c.split('\n')) !== null && _d !== void 0 ? _d : [];
const buildImageName = yield docker_1.buildImage(imageName, checkoutPath, subFolder);
const buildImageName = yield docker_1.buildImage(imageName, imageTag, checkoutPath, subFolder);
if (buildImageName === '') {
return;
}
if (!(yield docker_1.runContainer(buildImageName, checkoutPath, subFolder, runCommand, envs))) {
if (!(yield docker_1.runContainer(buildImageName, imageTag, checkoutPath, subFolder, runCommand, envs))) {
return;
}
}
Expand Down Expand Up @@ -125,8 +126,9 @@ function runPost() {
task.setResult(task.TaskResult.Failed, 'imageName input is required');
return;
}
console.log(`Pushing image ''${imageName}...`);
yield docker_1.pushImage(imageName);
const imageTag = task.getInput('imageTag');
console.log(`Pushing image ''${imageName}:${imageTag !== null && imageTag !== void 0 ? imageTag : 'latest'}...`);
yield docker_1.pushImage(imageName, imageTag);
});
}
run();
18 changes: 15 additions & 3 deletions azdo-task/DevContainerBuildRun/src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ export async function isDockerBuildXInstalled(): Promise<boolean> {
}
export async function buildImage(
imageName: string,
imageTag: string | undefined,
checkoutPath: string,
subFolder: string
): Promise<string> {
console.log('🏗 Building dev container...')
try {
return await docker.buildImage(exec, imageName, checkoutPath, subFolder)
return await docker.buildImage(
exec,
imageName,
imageTag,
checkoutPath,
subFolder
)
} catch (error) {
task.setResult(task.TaskResult.Failed, error)
return ''
Expand All @@ -21,6 +28,7 @@ export async function buildImage(

export async function runContainer(
imageName: string,
imageTag: string | undefined,
checkoutPath: string,
subFolder: string,
command: string,
Expand All @@ -31,6 +39,7 @@ export async function runContainer(
await docker.runContainer(
exec,
imageName,
imageTag,
checkoutPath,
subFolder,
command,
Expand All @@ -43,10 +52,13 @@ export async function runContainer(
}
}

export async function pushImage(imageName: string): Promise<boolean> {
export async function pushImage(
imageName: string,
imageTag: string | undefined
): Promise<boolean> {
console.log('📌 Pushing image...')
try {
await docker.pushImage(exec, imageName)
await docker.pushImage(exec, imageName, imageTag)
return true
} catch (error) {
task.setResult(task.TaskResult.Failed, error)
Expand Down
Loading

0 comments on commit bc711cd

Please sign in to comment.