I have some code to generate a binary file (pdf). Generating locally is fine, but that might be on another computer, which requires effort to retrieve.
So I will use some Github CI to build for me!
The code is not important to this article, but basically it’s a php
script with some composer
dependencies, mostly tcpdf
which builds a file output.pdf
into the repository root.
Local Builds
Can build locally, using docker
of course, something like this.
Makefile
:
build:
docker run --rm --tty --user=$$(id -u) \
--volume="$$(pwd):/app" \
composer:latest \
composer install
docker run --rm --tty --user=$$(id -u) \
--volume="$$(pwd):/app" \
--workdir=/app \
--env-file=".env" \
php:7.4-cli \
php src/index.php
open:
xdg-open "output.pdf"
Have a .env
file which is passed into docker run
- values should not be quoted though, e.g.:
NAME=Alistair Collins
ADDRESS=1 Random Street, Town, AA1 1AA
PHONE_OR_EMAIL=+44 07123 456 789 | email@website.com
WEBSITE=https://ac93.uk
Github Builds
Main thing here is the absence of .env
file. Instead, this is handled using Github secrets. Go to Settings -> Secrets
, see .env.example
for which ones you need to add.
The file described here is .github/workflows/build.yml
.
|
|
Fairly self-explanatory, the jobs below are run when a tag starting with v
is pushed. Read more on on
.
jobs
- important lines are highlighed and some more information given below.
|
|
Entire file here.
shivammathur/setup-php@v2
is fairly configurable and comes with composer
.
We are setting env
s in this step too, this is so that our code has access to our repository secrets (our code basically takes NAME
, etc. from $_ENV
and prints into the pdf).
Line 33 php src/index.php
- running this will create the output.pdf
which should be attached to the release (see line 53).
Line 54 gives the name of the output.pdf
on the release.
To push changes and trigger builds:
# make changes
git add ...
git commit -m '...'
git push origin master
git tag v0.0.2 -m 'updated current position'
git push origin v0.0.2
The last step will trigger the build and release.