From 71bef4a446f8a4b3d360884df2096d930b52b206 Mon Sep 17 00:00:00 2001 From: David Regla Date: Tue, 19 Apr 2022 18:08:23 -0500 Subject: [PATCH 1/6] Add basic Dockerfile --- .dockerignore | 4 ++++ Dockerfile | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..aa0af42 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +docs +tests +build \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3e407d4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,28 @@ +# ------------------------------------ +# Nuild stage +# ------------------------------------ +FROM composer/composer:2 as build +WORKDIR /app + +# Install dependencies +COPY composer.json ./ +RUN composer install --no-dev --no-autoloader --no-scripts --ignore-platform-reqs +COPY . . +RUN composer install --no-dev --optimize-autoloader --ignore-platform-reqs + + +# ------------------------------------ +# Runtime stage +# ------------------------------------ +FROM php:7-fpm-alpine as runtime + +# System dependencies +RUN apk add --update libxslt-dev +RUN docker-php-ext-install xsl soap + +# Copy source code +WORKDIR /app +COPY --from=build /app . + +ENTRYPOINT ["bin/cfditopdf"] +CMD ["--help"] From fabb49d7a7643960661df465739ed8d6a5dbbd08 Mon Sep 17 00:00:00 2001 From: David Regla Date: Thu, 5 May 2022 18:20:55 -0500 Subject: [PATCH 2/6] Add a helper Makefle --- Makefile | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ac72731 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +# Container image +IMAGE_REPO ?= ghcr.io/phpcfdi/cfditopdf +IMAGE_TAG ?= latest + +.DEFAULT_GOAL := help +.PHONY: build run help + +build: ## Build the container image + docker build -t $(IMAGE_REPO):$(IMAGE_TAG) . + +run: ## Run container passing `cmd` make argument as the containr command + @docker run --rm \ + --volume $(shell pwd)/files:/app/files \ + --user $(shell id -u ${USER}):$(shell id -g ${USER}) \ + $(IMAGE_REPO):$(IMAGE_TAG) \ + $(cmd) + +help: ## Print this help. + @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' From 813162ee783d8fb1eccbd22092a560dd4f130edf Mon Sep 17 00:00:00 2001 From: David Regla Date: Thu, 5 May 2022 18:29:12 -0500 Subject: [PATCH 3/6] Update README about running as a container --- README.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/README.md b/README.md index 308a6c7..6d3b700 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,32 @@ Use [composer](https://getcomposer.org/), so please run composer require phpcfdi/cfditopdf ``` +## Run with Docker + +Running as a Docker container lets you use this CLI anywhere where Docker is installed without worrying about about any dependencies, not even PHP itself. + +```shell +# This will mount the current working directory +# into a the /data directory inside the container +docker run --rm \ + --volume $PWD:/data \ + --user $(id -u):$(id -g) \ + ghcr.io/phpcfdi/cfditopdf:latest \ + /data/my-cfdi.xml \ + /data/my-cfdi-output.pdf +``` + +You can optionally set an alias in your shell to simplify running the container (to make this alias permanent add the alias to your .bashrc fille). + +```shell +alias cfditopdf='docker run --rm --volume $PWD:/data --user $(id -u):$(id -g) ghcr.io/phpcfdi/cfditopdf:latest' + +# Then, execute just as `cfditopdf` (see usage in the next section) + +cfditopdf --help +cfditopdf /data/my-cfdi.xml /data/my-cfdi-output.pdf +``` + ## Basic usage from CLI ```text From 5cf5c5ebe4fbc2fdf20909ec910c9ceb7aed2ec9 Mon Sep 17 00:00:00 2001 From: David Regla Date: Thu, 5 May 2022 18:30:27 -0500 Subject: [PATCH 4/6] Add a GitHub workflow to automatically build and push to a public Docker registry --- .github/workflows/ci.yml | 53 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 .github/workflows/ci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..3f6e88a --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,53 @@ +name: CI + +on: + push: + branches: [main] + tags: [v*] + pull_request: + branches: [main] + release: + types: [published] + +env: + REGISTRY: ghcr.io + REPOSITORY: ${{ github.repository }} + +jobs: + docker-image: + name: Build and push Docker image + runs-on: ubuntu-latest + + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Log in to the Container registry + uses: docker/login-action@v1 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v3 + with: + images: ${{ env.REGISTRY }}/${{ env.REPOSITORY }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + + - name: Build and push ${{ env.REGISTRY }}/${{ env.REPOSITORY }} image + uses: docker/build-push-action@v2 + with: + context: . + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + push: true From 7ccb90ec20816d3f81f690bcedfcc1af0bfb2f13 Mon Sep 17 00:00:00 2001 From: David Regla Date: Thu, 5 May 2022 18:59:21 -0500 Subject: [PATCH 5/6] Update CI workflow --- .github/workflows/ci.yml | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3f6e88a..5f6780f 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,16 +26,9 @@ jobs: - name: Checkout repository uses: actions/checkout@v3 - - name: Log in to the Container registry - uses: docker/login-action@v1 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - name: Extract metadata (tags, labels) for Docker id: meta - uses: docker/metadata-action@v3 + uses: docker/metadata-action@v4 with: images: ${{ env.REGISTRY }}/${{ env.REPOSITORY }} tags: | @@ -44,10 +37,18 @@ jobs: type=semver,pattern={{version}} type=semver,pattern={{major}}.{{minor}} + - name: Log in to the Container registry + if: github.event_name != 'pull_request' + uses: docker/login-action@v2 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + - name: Build and push ${{ env.REGISTRY }}/${{ env.REPOSITORY }} image - uses: docker/build-push-action@v2 + uses: docker/build-push-action@v3 with: context: . tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - push: true + push: ${{ github.event_name != 'pull_request' }} From 38c0b0df9102c55c1cafb8db8b9fd674b090d145 Mon Sep 17 00:00:00 2001 From: David Regla Date: Thu, 5 May 2022 19:17:30 -0500 Subject: [PATCH 6/6] Update Maakefile 'run' target to match the volme binding convention suggested in the README (/data) --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index ac72731..e3b228a 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ build: ## Build the container image run: ## Run container passing `cmd` make argument as the containr command @docker run --rm \ - --volume $(shell pwd)/files:/app/files \ + --volume $(shell pwd)/data:/data \ --user $(shell id -u ${USER}):$(shell id -g ${USER}) \ $(IMAGE_REPO):$(IMAGE_TAG) \ $(cmd)