Skip to content

Instantly share code, notes, and snippets.

@siyoungoh
Last active April 9, 2024 14:17
Show Gist options
  • Save siyoungoh/aeb2979f187ab9afe2a07679205ae51a to your computer and use it in GitHub Desktop.
Save siyoungoh/aeb2979f187ab9afe2a07679205ae51a to your computer and use it in GitHub Desktop.
Github Action - Docker CI/CD sample
name: Build and Deploy
on:
push:
branches:
- main
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build Docker image
run: docker build -t myapp:${{ github.sha }} .
- name: Log in to Docker Hub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Push Docker image to Docker Hub
run: docker push myapp:${{ github.sha }}
deploy:
runs-on: ubuntu-latest
needs: build-and-push
steps:
- name: Deploy to Server
uses: appleboy/ssh-action@v0.1.2
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
password: ${{ secrets.SERVER_PASSWORD }}
script: |
docker pull myapp:${{ github.sha }}
docker stop myapp || true
docker run -d --rm --name myapp -p 80:80 myapp:${{ github.sha }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment