How to use github action auto push docker image to docker hub
Docker Hub is a cloud-based registry service for Docker containers. It provides a platform for Docker users to share and distribute container images.
- 1、You should register a user on docker hub, and next to do.
- 2、create your docker hub repositories on docker hub
- 3、 add workflow file in your github project.
name: Build
on:
push:
branches: [ main ]
paths-ignore:
- .github/**
- docs/**
pull_request:
branches: [ main ]
env:
IMAGE_NAME: registry.ap-southeast-1.aliyuncs.com/kuops/easy-admin # docker image
TAG: ${{ secrets.IMAGE_NAME_TAG }}
IMAGE_NAME_TAG: registry.ap-southeast-1.aliyuncs.com/kuops/easy-admin:${{ secrets.IMAGE_NAME_TAG }}
jobs:
sync:
name: 'Submodules Sync'
runs-on: ubuntu-latest
steps:
- name: check submodules
uses: actions/checkout@v4
with:
submodules: true
# Update references
- name: Git Sumbodule Update
run: |
git submodule update --remote --recursive
- name: ls dir
run: ls -la
- name: cd ui
run: cd ./ui/ && ls -la
build:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: ['1.21']
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: actions/setup-node@v3
with:
node-version: 16
- name: Git Sumbodule Update
run: |
git submodule update --remote --recursive
- name: npm install
run: cd ./ui/ && npm install --legacy-peer-deps
- name : npm build
run: cd ./ui/ && npm run build:prod --legacy-peer-deps
- name: Set up Go ${{ matrix.go-version }}
uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go-version }}
# - name: go Test
# run: go test
- name: Tidy
run: go mod tidy
- name: Build
run: CGO_ENABLED=0 go build -ldflags="$(LDFLAGS)" -a -installsuffix "" -o easy-admin .
- name: Build the Docker image and push
run: |
docker login --username=${{ secrets.DOCKER_USERNAME }} registry.ap-southeast-1.aliyuncs.com --password=${{ secrets.DOCKER_PASSWORD }}
echo "************ docker login end"
docker build -t easy-admin:latest .
echo "************ docker build end"
docker tag easy-admin ${{ env.IMAGE_NAME_TAG }}
echo "************ docker tag end"
docker images
echo "************ docker images end"
docker push ${{ env.IMAGE_NAME_TAG }} # 推送
echo "************ docker push end"
- name: Log in to Docker Hub
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
username: ${{ secrets.HUB_DOCKER_USERNAME }}
password: ${{ secrets.HUB_DOCKER_PASSWORD }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: nicesteven/easy-admin
- name: Build and push Docker image
uses: docker/build-push-action@3b5e8027fcad23fda98b2e3ac259d8d67585f671
with:
context: .
file: ./Dockerfile
push: true
tags: nicesteven/easy-admin:latest
- 4、configure your hub account and docker hub token, you can click this link to create your docker hub https://hub.docker.com/settings/security
- 5、Add your docker hub account in your github project
- 6、make your Dockerfile for your project, when you push the docker image before you must have docker image content
FROM alpine
RUN apk update --no-cache
RUN apk add --update gcc g++ libc6-compat
RUN apk add --no-cache ca-certificates
RUN apk add --no-cache tzdata
RUN apk add --no-cache tini
ENV TZ Asia/Shanghai
COPY ./easy-admin /easy-admin
COPY ./config/settings.demo.yml /config/settings.yml
EXPOSE 8000
RUN chmod +x /easy-admin
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["/easy-admin","server","-c", "/config/settings.yml"]
- 7、and then when you push your code, the github action will auto make docker image and push to docker hub
This article all code form https://github.com/nicelizhi/easy-admin, you can git clone do it.
If this article is helpful to you, please follow us, thank you!