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/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..5f6780f --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,54 @@ +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: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@v4 + 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: 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@v3 + with: + context: . + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + push: ${{ github.event_name != 'pull_request' }} 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"] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e3b228a --- /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)/data:/data \ + --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}' 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