![[ci/cd] github를 이용한 ci/cd 구축 - 1](https://img1.daumcdn.net/thumb/R750x0/?scode=mtistory2&fname=https%3A%2F%2Fblog.kakaocdn.net%2Fdn%2FboewaV%2FbtsrqINy7Qx%2FtCmYQfmWmaXRCC0Nmfac6K%2Fimg.png)
1. github 접속
2. repo 메뉴 중 Actions
3. New workflow 생성
Docker image를 빌드 하기 위해 Docker image 선택
build code는 본인의 설정에 따라 맞추면 된다.
# https://docs.docker.com/build/ci/github-actions/#step-three-define-the-workflow-steps
# workflow 이름
name: Docker hub image build
# workflow에서 작업이 실행되는 조건 (push, pull, ...)
on:
push:
branches:
- 'main'
tags:
- 'v*'
#작업에 대한 정의
jobs:
build:
runs-on: ubuntu-latest
steps:
### repository check
-
name: Checkout
uses: actions/checkout@v3
### image tag number
-
name: Set env
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/v}" >> $GITHUB_ENV
### docker hub login
-
name: Login to DockerHub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
### setup docker buildx
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
### meta https://github.com/docker/metadata-action
-
name: Docker meta
id: docker_meta
#uses: crazy-max/ghaction-docker-meta@v1
uses: docker/metadata-action@v4
with:
images: junisduck/image_test
tags: |
${{ github.repository_owner }}:latest
${{ github.repository_owner }}:${{ env.RELEASE_VERSION }}
### build and push docker image
-
name: Build and push
uses: docker/build-push-action@v4
with:
context: .
files: |
./Dockerfile
${{ steps.meta.outputs.bake-file }}
platforms: linux/amd64
push: true
tags: ${{ steps.docker_meta.outputs.tags }}
labels: ${{ steps.docker_meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
변수를 활용하기 위해 github repo의 Settings -> Secrets and variables -> Actions을 찾아가 변수를 지정해주면 된다.
slack 알림을 추가할거면 코드 마지막에 아래 내용을 추가
### slack notification
-
name: Slack Notification Demo
uses: rtCamp/action-slack-notify@v2
with:
status: ${{ job.status }}
author_name: dev(?)
fields: repo,commit,message,author
mention: here
if_mention: failure,cancelled
env:
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK }}
if: always()
Docker Token 발급 방법
Docker Hub Container Image Library | App Containerization
Deliver your business through Docker Hub Package and publish apps and plugins as containers in Docker Hub for easy download and deployment by millions of Docker users worldwide.
hub.docker.com
로그인 -> Account Settings -> Security -> New Access Token 생성 후 나오는 토큰 값 저장 -> 위에 변수 값 지정
Slack API Token 발급 방법
Slack API: Applications | Slack
Your Apps Don't see an app you're looking for? Sign in to another workspace.
api.slack.com
로그인 -> Your apps -> Generate Token -> Workspace 선택 -> Grenerate
# curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/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
수정된 코드를 tag를 달아서 github에 push를 하게 되면 github와 docker hub에 tag 버전이 생성됨
git add .
git tag v1.0.22
git push origin v1.0.22
참고 사이트
https://docs.docker.com/build/ci/github-actions/
https://github.com/marketplace/actions/build-and-push-docker-images
'IT > cicd' 카테고리의 다른 글
argocd rbac (0) | 2024.02.05 |
---|
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!