Update main.yml #5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy with Docker | |
# 내가 만드는 이름이다 뭐든 상관없다 | |
on: | |
push: | |
branches: [ main ] | |
# 무슨 브랜치가 업데이트 될 때 Actions 를 작동시킬지 적는다. | |
# develop 브랜치로 개발하며 완료되면 main 에 머지해 Actions 를 작동시키면 조금 더 안전하겠지. | |
# 여러개 적어도 된다. | |
env: | |
DOCKER_IMAGE: ghcr.io/YoujeongChoi/myproject | |
VERSION: latest | |
NAME: newproject | |
# Docker image 를 ghcr.io 에 올릴 때 우리의github이름/이미지이름 으로 저장한다. 이미지이름을 정해주면 된다. | |
# Docker image 의 이름을 newproject 이라고 해놓은 것. 이름 뭐할지 정하면 된다. | |
jobs: | |
build: | |
name: Build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v2 | |
- name: Setup docker buildx | |
id: buildx | |
uses: docker/setup-buildx-action@v1 | |
- name: Cache docker layers | |
uses: actions/cache@v2 | |
with: | |
path: /tmp/.buildx-cache | |
key: ubuntu-buildx-latest | |
restore-keys: | | |
ubuntu-buildx- | |
- name: Login to ghcr | |
uses: docker/login-action@v1 | |
with: | |
registry: ghcr.io | |
username: YoujeongChoi | |
password: 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 | |
# 우리가 방금 복사해서 setting secrets 에 붙여줬던 token 이다. 이름을 기억해 넣어주자. | |
# 우리의 ghcr.io 에 접근하기 위함이다. | |
- name: Build and push | |
id: docker_build | |
uses: docker/build-push-action@v2 | |
with: | |
builder: server-docker.buildx.outputs | |
push: true | |
tags: server-docker-image:latest | |
deploy: | |
needs: build | |
name: Deploy | |
runs-on: [ self-hosted, label-newproject ] | |
# label-newproject 라는 이름으로 AWS EC2 가 Runner 를 작동시킬 때 사용했던 그 label | |
steps: | |
- name: Login to ghcr | |
uses: docker/login-action@v1 | |
with: | |
registry: ghcr.io | |
username: YoujeongChoi | |
password: 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 | |
- name: Docker run | |
run: | | |
docker stop server-docker && docker rm server-docker && docker rmi server-docker-image:latest | |
docker run -d -p 8000:8000 --name newproject --restart always server-docker-image:latest | |
# 첫 줄 | |
# 먼저 진행되고 있던 docker 를 stop(멈추고), rm 로 docker 컨테이너를 지우고, rmi 로 docker 이미지를 지운다. | |
# 새롭게 build 된 docker 를 사용하기 위함이다. | |
# 두번째 줄 | |
# -d : 백그라운드로 | |
# -p : 포트 번호 3000번에서 3000번으로 접근 |