Skip to content

Deploy main

Deploy main #2

Workflow file for this run

name: Deploy main
on:
push:
branches:
- main
workflow_dispatch:
jobs:
prepare:
# Build the matrix of Dockerfiles, looking for all Dockerfiles in deploy/remote
runs-on: ubuntu-22.04
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
path: source
- name: Find Dockerfiles
id: set-matrix
run: |
echo "matrix=$(find ./source/deploy/remote -name 'Dockerfile' -o -name '*.Dockerfile' | jq -R -s -c 'split("\n")[:-1] | map({dockerfile: .})')" >> "$GITHUB_OUTPUT"
build:
# Build and push every Dockerfile found in the previous step
needs: prepare
environment:
name: main
runs-on: ubuntu-22.04
strategy:
matrix:
include: ${{fromJson(needs.prepare.outputs.matrix)}}
steps:
- name: Create image name
# Format the Dockerfile name to use it as the image name
# Example: ./source/remote/main/php.Dockerfile -> php
id: dockerfile-data
run: |
echo "image-name=$(basename ${{ matrix.dockerfile }} | sed 's/\.[^.]*$//')" >> "$GITHUB_OUTPUT"
- name: Checkout repo
uses: actions/checkout@v2
with:
path: source
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: ./source/
file: ${{ matrix.dockerfile }}
push: true
no-cache: true
tags: ${{ secrets.DOCKER_REGISTRY }}/${{ github.repository }}-${{ steps.dockerfile-data.outputs.image-name }}:${{ github.ref_name }}-latest
deploy:
# Deploy the Docker image to the remote server
needs: build
runs-on: ubuntu-22.04
environment:
name: main
steps:
- name: Checkout repo
uses: actions/checkout@v2
with:
path: source
- name: Copy remote/compose.yaml to remote server
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_KEY }}
port: ${{ secrets.SERVER_PORT }}
strip_components: 3
source: "source/deploy/remote/compose.yaml"
target: "${{ secrets.SERVER_PATH }}/${{ github.ref_name }}/${{ github.event.repository.name }}"
- name: Pull and start the Docker container
uses: appleboy/[email protected]
with:
host: ${{ secrets.SERVER_IP }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_KEY }}
port: ${{ secrets.SERVER_PORT }}
script: |
cd ${{ secrets.SERVER_PATH }}/${{ github.ref_name }}/${{ github.event.repository.name }}
docker compose pull
docker compose up -d