From 7d86f1b9adc7afe4a64e13fb425872a547d6afa3 Mon Sep 17 00:00:00 2001 From: Samuel Ryan Date: Sun, 6 Sep 2020 18:47:06 +0100 Subject: [PATCH] :bug: Extract contents of directory (#4) --- .github/tests/Dockerfile | 11 ++++++++ .github/workflows/directory.yml | 48 +++++++++++++++++++++++++++++++ .github/workflows/file.yml | 29 +++++++++++++++++++ .github/workflows/test.yml | 50 --------------------------------- README.md | 23 +++++++++------ src/extract.js | 1 + 6 files changed, 103 insertions(+), 59 deletions(-) create mode 100644 .github/tests/Dockerfile create mode 100644 .github/workflows/directory.yml create mode 100644 .github/workflows/file.yml delete mode 100644 .github/workflows/test.yml diff --git a/.github/tests/Dockerfile b/.github/tests/Dockerfile new file mode 100644 index 0000000..a382ede --- /dev/null +++ b/.github/tests/Dockerfile @@ -0,0 +1,11 @@ +FROM alpine + +RUN mkdir -p /files/x +RUN mkdir -p /files/y + +RUN echo "Hello, World 1! \$(date)" > /files/001.txt +RUN echo "Hello, World 2! \$(date)" > /files/002.txt +RUN echo "Hello, World 3! \$(date)" > /files/003.txt +RUN echo "Hello, World 4! \$(date)" > /files/x/004.txt +RUN echo "Hello, World 5! \$(date)" > /files/x/005.txt +RUN echo "Hello, World 6! \$(date)" > /files/y/006.txt diff --git a/.github/workflows/directory.yml b/.github/workflows/directory.yml new file mode 100644 index 0000000..3d66535 --- /dev/null +++ b/.github/workflows/directory.yml @@ -0,0 +1,48 @@ +name: Test Directory Extraction + +on: [push] + +jobs: + root-directory: + runs-on: ubuntu-latest + name: Extract Contents of Directory from Example Image + steps: + - uses: actions/checkout@v2 + - run: docker build -t example:${{ github.sha }} ./.github/tests + - uses: ./ + id: extract + with: + image: example:${{ github.sha }} + path: /files/. + - run: test -e ${{ steps.extract.outputs.destination }}/001.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/002.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/003.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/x/004.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/x/005.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/y/006.txt || exit 1 + nested-directory: + runs-on: ubuntu-latest + name: Extract Nested Directory from Example Image + steps: + - uses: actions/checkout@v2 + - run: docker build -t example:${{ github.sha }} ./.github/tests + - uses: ./ + id: extract + with: + image: example:${{ github.sha }} + path: /files/x + - run: test -e ${{ steps.extract.outputs.destination }}/x/004.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/x/005.txt || exit 1 + nested-directory-contents: + runs-on: ubuntu-latest + name: Extract Nested Contents of Directory from Example Image + steps: + - uses: actions/checkout@v2 + - run: docker build -t example:${{ github.sha }} ./.github/tests + - uses: ./ + id: extract + with: + image: example:${{ github.sha }} + path: /files/x/. + - run: test -e ${{ steps.extract.outputs.destination }}/004.txt || exit 1 + - run: test -e ${{ steps.extract.outputs.destination }}/005.txt || exit 1 diff --git a/.github/workflows/file.yml b/.github/workflows/file.yml new file mode 100644 index 0000000..29bf7b4 --- /dev/null +++ b/.github/workflows/file.yml @@ -0,0 +1,29 @@ +name: Test File Extraction + +on: [push] + +jobs: + root-file: + runs-on: ubuntu-latest + name: Extract Example File From Root of Built Image + steps: + - uses: actions/checkout@v2 + - run: docker build -t example:${{ github.sha }} ./.github/tests + - uses: ./ + id: extract + with: + image: example:${{ github.sha }} + path: /files/001.txt + - run: test -e ${{ steps.extract.outputs.destination }}/001.txt || exit 1 + nested-file: + runs-on: ubuntu-latest + name: Extract Nexted Example File From Built Image + steps: + - uses: actions/checkout@v2 + - run: docker build -t example:${{ github.sha }} ./.github/tests + - uses: ./ + id: extract + with: + image: example:${{ github.sha }} + path: /files/y/006.txt + - run: test -e ${{ steps.extract.outputs.destination }}/006.txt || exit 1 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml deleted file mode 100644 index f38f68c..0000000 --- a/.github/workflows/test.yml +++ /dev/null @@ -1,50 +0,0 @@ -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 }} - name: dist diff --git a/README.md b/README.md index 087083b..050b0a9 100644 --- a/README.md +++ b/README.md @@ -5,24 +5,28 @@ 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' + image: 'ghost:alpine' + path: '/var/lib/ghost/current/core/built/assets/.' ``` ## 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` | +| ID | Description | Examples | +| --- | ----------- | -------- | +| `image` | Docker Image to extract files from | `alpine` `docker.pkg.github.com/github/semantic/semantic` | +| `path` | Path (from root) to a file or directory within Image | `files/example.txt` `files` `files/.` | + +> :paperclip: To copy the **contents** of a directory the `path` must end with +`/.` otherwise the directory itself will be copied. More information about the +specific rules can be found via the [docker cp][docker-cp] documentation. ## Outputs | ID | Description | Example | | --- | ----------- | ------- | -| `destination` | Destination path containing the extracted file(s) | `.extracted-1598717412` | +| `destination` | Destination path containing the extracted file(s) | `.extracted-1598717412/` | ## Examples @@ -46,7 +50,7 @@ jobs: - uses: shrink/actions-docker-extract@v1 with: image: my-example-image - path: /app + path: /app/. - name: Upload Dist uses: actions/upload-artifact@v2 with: @@ -74,7 +78,7 @@ jobs: - uses: shrink/actions-docker-extract@v1 with: image: ${{ github.repository }}/example-image:latest - path: /app + path: /app/. - name: Upload Dist uses: actions/upload-artifact@v2 with: @@ -84,3 +88,4 @@ jobs: [build-push-action]: https://github.com/docker/build-push-action [login-action]: https://github.com/docker/login-action +[docker-cp]: https://docs.docker.com/engine/reference/commandline/cp/#extended-description diff --git a/src/extract.js b/src/extract.js index e3028a9..28ab202 100644 --- a/src/extract.js +++ b/src/extract.js @@ -8,6 +8,7 @@ async function run() { 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);