fix: build script to deploy #12
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: Build, Push and Deploy | |
# Controls when the action will run. | |
on: | |
# Triggers the workflow on push events but only for the master branch | |
push: | |
branches: [main] | |
# Allows you to run this workflow manually from the Actions tab | |
workflow_dispatch: | |
env: | |
REGISTRY: ghcr.io | |
IMAGE_NAME: ${{ github.repository }} | |
CONTAINER: brainto-service | |
jobs: | |
push_to_registry: | |
name: Push Docker image to GitHub Packages | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
attestations: write | |
id-token: write | |
steps: | |
- name: Check out the repo | |
uses: actions/checkout@v3 | |
- name: Login to Github Docker Registry | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
registry: ${{ env.REGISTRY }} | |
- name: Docker meta | |
id: meta | |
uses: docker/metadata-action@v5 | |
with: | |
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | |
- name: Set .env file ✨ | |
run: | | |
touch .env | |
echo "${{secrets.ENV_FILE}}" >> .env | |
- run: ls -la | |
- name: Push to GitHub Packages | |
uses: docker/build-push-action@v6 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
deploy: | |
needs: [push_to_registry] | |
name: Deploy to DigitalOcean | |
runs-on: ubuntu-latest | |
steps: | |
- name: Setup SSH connection | |
uses: webfactory/[email protected] | |
with: | |
ssh-private-key: ${{ secrets.SSH_KEY }} | |
- name: Adding Known Hosts | |
run: ssh-keyscan -H ${{ secrets.SSH_HOST }} >> ~/.ssh/known_hosts | |
- name: Login to the GitHub Packages Docker Registry | |
run: ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker login ${{env.REGISTRY}} -u ${{github.actor}} -p ${{secrets.GITHUB_TOKEN}}" | |
- name: Pull latest container | |
run: | | |
ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker pull ${{env.REGISTRY}}/${{env.IMAGE_NAME}}:main" | |
- name: Stop deployed container | |
continue-on-error: true | |
run: | | |
ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker stop ${{env.CONTAINER}}" | |
- name: Remove deployed container | |
continue-on-error: true | |
run: | | |
ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker rm ${{env.CONTAINER}}" | |
- name: Start docker container | |
run: | | |
ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker run -p 80:3000 -d --name=${{env.CONTAINER}} ${{env.REGISTRY}}/${{env.IMAGE_NAME}}:main" | |
- name: Start docker container | |
run: | | |
ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker ps" | |
- name: Logout from the GitHub Packages Docker Registry | |
run: ssh ${{secrets.SSH_USER}}@${{secrets.SSH_HOST}} "docker logout ${{env.REGISTRY}}" |