In a previous tutorial we looked at how we can use Github actions to run our PHPUnit tests on laravel applications on a merge into master.
The next step once tests are successful is to build a docker image and push this to the repository.
This will require us to create a new workflow file that will run after the tests are successful to build and push your docker image to a repository.
First create a new file in your workflow folder .github/workflows/docker.yml
and paste the following in that file.
name: Docker
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: 'Checkout GitHub Action'
uses: actions/checkout@v2
- name: Build and push docker
uses: docker/build-push-action@v1
with:
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
repository: DOCKER_REPOSITORY
tags: latest
This works by first checking out the current repository on pushed into master, by using the actions/checkout@v2
action. The next step will use the docker build and push action docker/build-push-action@v1
. This requires 4 parameters the docker hub username, password, repository and tags.
In the code above you can see we're using the secrets parameter for username and password.
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
To add secrets to your repository in Github is done by selecting Settings
-> Secrets
then create a new secret for the username and password.
Ensure your repository has a Dockerfile at the root of your project to build the image.
That's it, now commit this file to your repository and on pushes into master, Github will build a push your docker image to the docker hub.