Extract path from Docker Image (#1)

This commit is contained in:
Samuel Ryan 2020-08-29 16:20:01 +01:00 committed by GitHub
parent f6106497d3
commit bf80417647
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 3800 additions and 0 deletions

9
.editorconfig Normal file
View file

@ -0,0 +1,9 @@
root = true
[*.{json,md,yml,js}]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

3
.eslintrc.json Normal file
View file

@ -0,0 +1,3 @@
{
"extends": "airbnb"
}

50
.github/workflows/test.yml vendored Normal file
View file

@ -0,0 +1,50 @@
name: Test Extraction Action
on: [push]
jobs:
extract-motd:
runs-on: ubuntu-latest
name: Extract MOTD From Alpine
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Extract
id: extract
uses: ./
with:
image: 'alpine'
path: '/etc/motd'
- name: Upload Extracted MOTD
uses: actions/upload-artifact@v2
with:
path: ${{ steps.extract.outputs.destination }}/motd
name: alpine-motd
example:
runs-on: ubuntu-latest
name: Extract Example From Built Image
steps:
- uses: actions/checkout@v2
- name: Create Example Dockerfile
run: |
cat <<-DOCKERFILE > Dockerfile
FROM alpine
RUN mkdir -p /app
RUN echo "Hello, World! \$(date)" > /app/example
DOCKERFILE
- name: Build Docker Image
uses: docker/build-push-action@v1
with:
repository: example
tags: ${{ github.sha }}
push: false
- uses: ./
id: extract
with:
image: example:${{ github.sha }}
path: /app
- name: Upload Dist
uses: actions/upload-artifact@v2
with:
path: ${{ steps.extract.outputs.destination }}/app
name: dist

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
node_modules/

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2020 Samuel Ryan <sam@samryan.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

80
README.md Normal file
View file

@ -0,0 +1,80 @@
# Docker Extract
A GitHub Action for extracting files from a Docker Image.
```yaml
- uses: shrink/actions-docker-extract@v1
with:
image: 'docker.pkg.github.com/github/semantic/semantic'
path: '/etc/motd'
```
## Inputs
All inputs are required.
| ID | Description | Example |
| --- | ----------- | ------- |
| `image` | Docker Image to extract files from | `alpine` |
| `path` | Path (from root) to a file or directory within Image | `/etc/motd` |
## Examples
### Build, Extract
Using [docker/build-push-action][build-push-action] to build a Docker
Image and then extract the contents of the `/app` directory within the newly
built image to upload as a `dist` artifact.
```yaml
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build Docker Image
uses: docker/build-push-action@v1
with:
repository: my-example-image
tags: latest
- uses: shrink/actions-docker-extract@v1
with:
image: my-example-image
path: /app
- name: Upload Dist
uses: actions/upload-artifact@v2
with:
path: ${{ steps.extract.outputs.destination }}/app
name: dist
```
### Login, Pull, Extract
Using [docker/login-action][login-action] to authenticate with the GitHub
Package Registry to extract from a published Docker Image.
```yaml
jobs:
extract:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Login to GitHub Package Registry
uses: docker/login-action@v1
with:
registry: docker.pkg.github.com
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: shrink/actions-docker-extract@v1
with:
image: ${{ github.repository }}/example-image:latest
path: /app
- name: Upload Dist
uses: actions/upload-artifact@v2
with:
path: ${{ steps.extract.outputs.destination }}/app
name: dist
```
[build-push-action]: https://github.com/docker/build-push-action
[login-action]: https://github.com/docker/login-action

19
action.yml Normal file
View file

@ -0,0 +1,19 @@
name: 'Docker Extract'
description: 'Extract file(s) from a Docker Image'
author: 'Samuel Ryan <sam@samryan.co.uk>'
inputs:
image:
description: 'Docker Image to extract files from'
required: true
path:
description: 'Path (from root) to a file or directory within Image'
required: true
outputs:
destination:
description: 'Destination of extracted file(s)'
runs:
using: 'node12'
main: 'dist/index.js'
branding:
icon: 'scissors'
color: 'blue'

1613
dist/index.js vendored Normal file

File diff suppressed because it is too large Load diff

1950
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

34
package.json Normal file
View file

@ -0,0 +1,34 @@
{
"name": "@shrink/actions-docker-extract",
"version": "0.1.0",
"description": "Extract from a Docker Image",
"author": "Samuel Ryan <sam@samryan.co.uk>",
"homepage": "https://github.com/shrink/actions-docker-extract#readme",
"main": "dist/index.js",
"scripts": {
"build": "ncc build src/extract.js",
"lint": "eslint 'src/**.js' --fix",
"commit": "npm run-script lint && npm run-script build"
},
"repository": {
"type": "git",
"url": "git+https://github.com/shrink/actions-document-sign.git"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/shrink/actions-docker-extract/issues"
},
"dependencies": {
"@actions/core": "^1.2.4",
"@actions/exec": "^1.0.4"
},
"devDependencies": {
"@zeit/ncc": "^0.22.3",
"eslint": "^7.2.0",
"eslint-config-airbnb": "^18.2.0",
"eslint-plugin-import": "^2.22.0",
"eslint-plugin-jsx-a11y": "^6.3.1",
"eslint-plugin-react": "^7.20.6",
"eslint-plugin-react-hooks": "^4.0.0"
}
}

20
src/extract.js Normal file
View file

@ -0,0 +1,20 @@
const core = require('@actions/core');
const exec = require('@actions/exec');
async function run() {
try {
const image = core.getInput('image');
const path = core.getInput('path');
const destination = `.extracted-${Date.now()}`;
const create = `docker cp $(docker create ${image}):/${path} ${destination}`;
await exec.exec(`mkdir -p ${destination}`);
await exec.exec(`/bin/bash -c "${create}"`, []);
core.setOutput('destination', destination);
} catch (error) {
core.setFailed(error.message);
}
}
run();