I want to build my image and push it to dockerhub using github actions.This is my repo
And this is how I want to do my job:
name: ci
on:
push:
branches:
- 'master'
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout#v2
-
name: Set up QEMU
uses: docker/setup-qemu-action#v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
-
name: Login to DockerHub
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
-
name: Build and push
uses: docker/build-push-action#v2
with:
context: .
load: true
tags: shirzadi/ehsan:latest
-
name: Push it!
run: docker push ${{ env.IMAGE }}:${{ env.GIT_SHA }}
And this is the result:
You are using environment variables that don't exist. According to docker/build-push-action#v2 documentation it can build, tag and push your image in a single step:
jobs:
docker:
steps:
# ...
-
name: Build and push
uses: docker/build-push-action#v2
with:
context: .
push: true
tags: shirzadi/ehsan:latest,shirzadi/ehsan:${{ env.GITHUB_SHA }}
The tags key lists 2 tags and use the GITHUB_SHA environment variable. These tags will be pushed as the push key is set to true.
See:
Github Actions - Default environment variables
docker/build-push-action#v2 - customizing inputs
You are not having env.IMAGE data. Below piece of code should do the job.
- name: Build and push
run: docker build -t ${{ env.IMAGE }}:${{ github.sha }} .
env:
IMAGE: shirzadi/ehsan
- name: Push it!
run: docker push ${{ env.IMAGE }}:${{ github.sha }}
env:
IMAGE: shirzadi/ehsan
Related
Hi I am trying to create an image of a simple project holded on github and push it to docker hub, but I do not achieve it.
this is my .yml file:
name: workflow 3
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Set up QEMU
uses: docker/setup-qemu-action#v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
- name: login to docker hub
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKER_NAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build and push
uses: docker/build-push-action#v2
with:
context: .
push: true
tags: user/app:latest
but I receive this error: Error: buildx failed with: error: failed to solve: failed to read dockerfile: open /tmp/buildkit-mount2002185417/Dockerfile: no such file or directory
I'm getting this error:
error: failed to solve: error writing layer blob: server message: insufficient_scope: authorization failed
after this GitHub Action finishes building the Docker image:
# https://docs.docker.com/ci-cd/github-actions/
name: Publish Docker Image
on:
workflow_dispatch:
push:
branches:
- master
paths:
- "Dockerfile"
schedule:
- cron: "0 6 * * */3"
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout#v3
- name: Set up QEMU
uses: docker/setup-qemu-action#v1
- name: Set up Docker Buildx
id: buildx
uses: docker/setup-buildx-action#v1
- name: Login to Docker Hub
uses: docker/login-action#v1
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Build and push
uses: docker/build-push-action#v2
with:
context: ./
file: ./Dockerfile
builder: ${{ steps.buildx.outputs.name }}
push: true
tags: ghcr.io/t145/black-mirror:latest
cache-from: type=registry,ref=t145/black-mirror:buildcache
cache-to: type=registry,ref=t145/black-mirror:buildcache,mode=max
It's configured following the guide linked at the top of the document. Why would this error occur here?
Try updating the cache-from and cache-to cache references to ghcr.io/t145/black-mirror. When you omit the registry (ghcr.io), I believe it defaults to DockerHub.
I have a simple pipeline in GitHub Actions. I'm trying to build and publish a Docker image to Docker Hub within the same. I would also like to build the image for different platforms (read: operating systems), hence I'm using strategy.matrix. This is how it looks like so far:
name: Build and Publish Docker image(s)
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout#v2
- name: Log in to Docker Hub
uses: docker/login-action#v1
with:
password: ${{ secrets.DOCKERHUB_TOKEN }}
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
- name: Build and push
uses: docker/build-push-action#v2
env:
platform: ${{ matrix.platforms }}
with:
context: ./${platform}/
push: true
tags: oscarotero/lume:1.3.0-${platform} # TODO: Parameterize version once this is finally working
strategy:
matrix:
platforms:
- alpine
- debian
on:
push:
branches:
- main
This is currently failing because it doesn't recognize platform when running the Build and push step. I have a similar one and it works with this approach.
Anyone more knowledgeable in GitHub Actions can shred some light here? All I can think of is that docker/build-push-action#v2 doesn't support that, but in that case, how can I approach this problem?
Removing the environment variable and using the matrix values directly solved the problem:
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout#v2
- name: Log in to Docker Hub
uses: docker/login-action#v1
with:
password: ${{ secrets.DOCKERHUB_TOKEN }}
registry: docker.io
username: ${{ secrets.DOCKERHUB_USERNAME }}
- name: Build and push
uses: docker/build-push-action#v2
with:
context: ./${{ matrix.platform }}/
push: true
tags: oscarotero/lume:1.3.0-${{ matrix.platform }}
strategy:
matrix:
platform:
- alpine
- debian
Since the FROM syntax in dockerfile doesnt support a github repo link.
There are few Dockerfiles in github repo.
How could I use github action to build them and push to docker-registry once per day?
you can use this example:
name: ci
on:
push:
branches:
- 'master'
jobs:
docker:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout#v2
-
name: Set up QEMU
uses: docker/setup-qemu-action#v1
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
-
name: Login to DockerHub
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Build and push
uses: docker/build-push-action#v2
with:
context: .
push: true
tags: user/app:latest
I'm trying to deploy my repository to a private Docker registry on every new release and everything works except for the version tagging. No matter what I've tried ${{ github.event.release.tag_name }} is always '', which cancels the workflow since docker tags can't be empty.
on:
release:
types: [published]
jobs:
push_to_registry:
name: Push Docker image to Docker Registry
runs-on: ubuntu-latest
steps:
-
name: Check out the repo
uses: actions/checkout#v2
-
name: Set up QEMU
uses: docker/setup-qemu-action#v1
-
name: Setup Docker Buildx
uses: docker/setup-buildx-action#v1
-
name: Log in to Docker Registry
uses: docker/login-action#v1
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: check tag
if: ${{ github.event.release.tag_name }} == ''
run: |
echo Epic fail
exit 1
-
name: Build and Push to Docker Registry
id: docker_build
uses: docker/build-push-action#v2
with:
push: true
tags: ${{ secrets.DOCKER_REGISTRY }}/repos:latest, ${{ secrets.DOCKER_REGISTRY }}/$repos:${{ github.event.release.tag_name }}
-
name: Image digest
run: echo ${{ steps.docker_build.outputs.digest }}
The repository this is running on is private so that might have something to do with it but I haven't been able to figure out what.
Any suggestions?
I think the problem is that github.event.release.tag_name is only available if the tagging itself triggered the build (but I'm not sure)
I made it work using a separate action: https://github.com/dawidd6/action-get-tag
Here's my usage:
steps:
- uses: actions/checkout#v2
- name: Get git tag
id: tag
uses: dawidd6/action-get-tag#v1
- uses: docker/build-push-action#v2
with:
context: ./
file: ./Dockerfile
push: true
tags: locustio/locust:${{ steps.tag.outputs.tag }}