GitHub: Workflows and Actions
Introduction
To create a new github worklfow you need to place a your-workflow.yml
file inside
the .github/workflows
folder. The .github folder should be palced at the root of your project.
An workflow file file is made up of the following components:
name:
the name of the workflowon:
determines which event may trigger this workflow, e.g. "push, pull_request"jobs:
the list of jobs to be executed.runs-on:
is the name of the image of the virtual machine to be used.steps:
each job contains a sequence of tasks called steps.
Build your github workflow for litstack projects
Specify the trigger
Start by giving your Github workflow a name and specifying which events can trigger this workflow. For example, you might want to run this workflow for every single push to the repostory. But you can also specify a list of branches to narrow down where the workflow should be triggered on a push or pull request event.
Furthermore it's possible to tell Github to only run this workflow when a new version of this repository is tagged and released.
name: buildon: push: branches: - dev - main pull_request: branches: - main release: types: [published]
Jobs
Once the trigger events are specified, the workflow should have something to do. Therefore, within the jobs, we configure a basic setup with a standard Ubuntu server and set up the first steps
- checkout our currently used repository
- setting up our required php version (and composer)
- install the composer and npm dependencies.
jobs: build: runs-on: ubuntu-latest steps: - name: checkout uses: actions/checkout@v2 - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: 8.0 tools: composer:v2 coverage: none - name: Install dependencies run: | composer config http-basic.store.litstack.io ${{ secrets.LITSTACK_USERNAME }} ${{ secrets.LITSTACK_PASSWORD }} composer install --ignore-platform-reqs && npm install --non-interactive
Listack Sponsorware packages
If your project contains any of litstacks sponserware packages (bladesmith, pages, etc.) it is required to provide credentials to store.litstack.io. Those are usally stored within your auth.json file and thus not commited to the git.
So to get composer to install these pacakges we need to define the credentials within the repository secrets. Go to the settings of your project, and set up two new repository secrets and fill them with the corresponding values:
LITSTACK_USERNAME
LITSTACK_PASSWORD
This allows us to use these values like this ${{ secrets.LITSTACK_USERNAME }}
within the workflow, but without actually exposing them.
Build
Now that the required packages and npm dependencies are installed, we are able to run a the npm production build!
- name: Make production build
run: npm run production
Now, this does not have any real effect yet, because it happens on the virtual machine on which the workflow is running. So we have to make a new commit from the virtual machine and push it back to the repository. We authenticate the commit by providing an automatically generated GITHUB_TOKEN. To make it clear that this commit was made by the CI, we specify the aw-ci-bot as its co-author.
- name: Commit production build
uses: EndBug/add-and-commit@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
add: "."
author_name: ${{ github.event.pusher.name }}
author_email: ${{ github.event.pusher.email }}
message: "Apply production build from CI
Co-authored-by: aw-ci-bot <bot@aw-studio.de>"
Final example Script
name: build
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 8.0
tools: composer:v2
coverage: none
- name: Install dependencies
run: |
composer config http-basic.store.litstack.io ${{ secrets.LITSTACK_USERNAME }} ${{ secrets.LITSTACK_PASSWORD }}
composer install --ignore-platform-reqs && npm install --non-interactive
- name: Make production build
run: npm run production
- name: Commit production build
uses: EndBug/add-and-commit@v4
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
add: "."
author_name: ${{ github.event.pusher.name }}
author_email: ${{ github.event.pusher.email }}
message: "Apply production build from CI
Co-authored-by: aw-ci-bot <bot@aw-studio.de>"