본문 바로가기

리눅스

[Linux] How to build and push Docker images with GitHub Actions

728x90

How to build and push Docker images with GitHub Actions

Create a new repository for GitHub

new repository > Actions > set up a workflow yourself

Editing

main.yml editing

name: CI

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Check Out Repo
        uses: actions/checkout@v2

      - name: Login to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKER_HUB_USERNAME }}
          password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

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

      - name: Build and push
        id: docker_build
        uses: docker/build-push-action@v2
        with:
          context: ./
          file: ./Dockerfile
          push: true
          tags: ${{ secrets.DOCKER_HUB_USERNAME }}/actions-test:latest

      - name: Image digest
        run: echo ${{ steps.docker_build.outputs.digest }}

Dockerfile editing

FROM centos:7

LABEL website="sangchul.kr"

ENV PS1A="\[\e[33m\]\u\[\e[m\]\[\e[37m\]@\[\e[m\]\[\e[34m\]\h\[\e[m\]:\[\033[01;31m\]\W\[\e[m\]$ "

RUN echo 'PS1=$PS1A' >> ~/.bashrc

Repository(actions-test) source

Create an access token in the dockerhub

Account Settings > Security > New Access Token

Create a new repository in the docker hub

Repositories > Create

Register dockerhub access token as an environmental variable

repository > Settings > Secrets > New repository secret

- DOCKER_HUB_USERNAME
- DOCKER_HUB_ACCESS_TOKEN

When the source is committed, it automatically builds and pushes.

[Github]

[Dockerhub]

728x90