In a previous post we went through how to use Docker Build Cloud to remotely build a Docker container image from a Github repository.
In that example, we kicked off a build and pushed the image to a container registry using the syntax: –
docker buildx build https://github.com/dbafromthecold/sqlserver2022.git ` --builder cloud-dbafromthecold-default ` --tag dbafromthecold/sqlserver2022:latest ` --push
This all seems a bit manual, doesn’t it?
What if we could automate the building of the image and the push to the container registry when we commit to the Github repository?
Thankfully, with Github Actions…we can do just that!
I don’t really have much experience with Github Actions if I’m honest…but all the syntax needed is provided in the Docker Build Cloud: –
N.B. – this option is under the builder that you use to build the image (using the default one here)
This gives the following code: –
name: ci on: push: branches: - "main" jobs: docker: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Log in to Docker Hub uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 with: version: "lab:latest" driver: cloud endpoint: "dbafromthecold/default" - name: Build and push uses: docker/build-push-action@v5 with: context: . tags: "dbafromthecold/sqlserver2022" outputs: ${{ github.event_name == 'pull_request' && 'type=cacheonly' || 'type=registry,push=true' }}
The only update I’ve had to make to the code provided is line 29 where I’ve changed the tag to “dbafromthecold/sqlserver2022”
OK, so let’s go and create this Github Action.
The first thing to do is create secrets in the Github repository to allow the action to connect to Docker Build Cloud.
For this example we’ll use the repo from the previous blog: –
https://github.com/dbafromthecold/sqlserver2022
In the repo, go to /settings/secrets/actions and create two repository secrets: –
These are your Docker Hub username and an access token created under your account (https://hub.docker.com/settings/security): –
Once the secrets are created, we can then create the workflow in Github.
In the repo, go to Actions and then select new workflow: –
Choose simple workflow: –
Remove the template code and replace with the code from Docker Build Cloud (remember to update the tag name on line 29!): –
Commit the changes and we’re good to go!
I have the repo locally so I’ll pull down the changes made in the web gui…
git pull
And now let’s test committing a change to the repo and pushing.
There’s a dockerfile in the repo (to build the image from)…I’ll make a simple change (adding a LABEL for example) and push: –
git add . git commit -m "testing github action" git push
And if we go back to Actions in the repo in Github we should see it working!
But did it build and push the image? Let’s check the Docker Hub: –
Awesome stuff, the image has been built and pushed to the Hub!
So by using Docker Build Cloud and Github Actions, we can push changes to a Github repository, remotely build a container image, and then push to a container registry without having to use any local resources!
Thanks for reading!