DEV Community

DevOpsCloudCloud
DevOpsCloudCloud

Posted on

Create Pipeline build and push docker to Docker Registry with Github actions

Hi! everyone, Now I learning github actions and I would like to know how use docker for build source code by github action.

and I very love about feature actions. someone can create actions and deploy to action marketplace and then anyone can download and use them pipelines that is make productivity when you have create pipeline tasks

I write file workflow yml

name: Build and Push Docker to Dockerhub

on:
    # Allows you to run this workflow manually from Actions tab
    workflow_dispatch:
    # workflow_run:
    #   workflows: ["Build Cache and Push Docker to Dockerhub"]
    #   types:
    #     - completed
    push:
      branches:
        - main

permissions:
  contents: 'read'
  packages: 'write'

jobs:
  build:
    runs-on: ubuntu-latest
    environment:
            name: main
    steps:
        - uses: actions/checkout@v4

        - name: Set up Docker Buildx
          uses: docker/setup-buildx-action@v3

        - name: Log in to GitHub container registry
          uses: docker/login-action@v3
          with:
            # registry: ghcr.io
            username: ${{ secrets.DOCKER_USERNAME }}
            password: ${{ secrets.DOCKER_PAT }}

        - name: Set SHA SHORT
          id: set-sha-short
          run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT

        - name: Build and push container image to registry
          uses: docker/build-push-action@v6
          with:
            push: true
            cache-from: type=registry,ref=${{ secrets.DOCKER_REPO_NAME }}:cache
            cache-to: type=registry,ref=${{ secrets.DOCKER_REPO_NAME }}:cache, mode=max
            tags: ${{ secrets.DOCKER_REPO_NAME }}:${{ steps.set-sha-short.outputs.sha_short }}
            file: ./Dockerfile
Enter fullscreen mode Exit fullscreen mode

and Thank you about feature buildx of docker. They help me about cache you don't need to create Cache Dockerfile for create cache. They have feature cache-to, it's help create image cache after pipeline docker builded

Top comments (0)