Github Actions: Build code-generated assets, create a release and attach those assets to the release

Tag
Tag
Tag
Published:
Author: Ally

Table of Contents

  1. Local Builds
  2. Github Builds

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

build

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.

secrets


The file described here is .github/workflows/build.yml.

1
2
3
4
5
6
name: 'Build & Release'

on:
  push:
    tags:
      - 'v*'

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.

 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v2

      - name: Get Composer Cache Directory
        id: composer-cache
        run: |
                    echo "::set-output name=dir::$(composer config cache-files-dir)"

      - name: 'Setup PHP'
        uses: shivammathur/setup-php@v2
        env:
          NAME: ${{ secrets.NAME }}
          ADDRESS: ${{ secrets.ADDRESS }}
          PHONE_OR_EMAIL: ${{ secrets.PHONE_OR_EMAIL }}
          WEBSITE: ${{ secrets.WEBSITE }}

      - name: 'Composer'
        run: |
                    composer install

      - name: 'Build'
        run: |
                    php src/index.php

      - name: 'Create Release'
        id: create_release
        uses: actions/create-release@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          tag_name: ${{ github.ref }}
          release_name: ${{ github.ref }}
          draft: false
          prerelease: false

      - name: 'Upload Release Asset'
        id: upload-release-asset
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ steps.create_release.outputs.upload_url }} # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
          asset_path: ./output.pdf
          asset_name: cv.pdf
          asset_content_type: application/pdf

Entire file here.


shivammathur/setup-php@v2 is fairly configurable and comes with composer.

We are setting envs 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.

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.

Querying an IP restricted API with Postman
Deploying a static site generators build to an S3 bucket using CI with Bitbucket pipelines
To bottom
To top
< SM
max-width: 640px