From dd3c79465dc4a88e35a78f49093a4836b9917f08 Mon Sep 17 00:00:00 2001 From: ClementMabileau Date: Wed, 25 Oct 2023 19:35:44 +0200 Subject: [PATCH] Add automatic deployment with GH Action (#2) * Refactor build workflow to use our own docker-compose rules * Upload website build as artifact * Add a deploy job to build.yml workflow, triggering it only on push/merge to main * Rename build.yml to build_and_deploy.yml * Trigger deployment only if website sources have been modified * Create and populate SSH Key and upload website build to remote server --- .github/workflows/build.yml | 24 --------- .github/workflows/build_and_deploy.yml | 73 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 24 deletions(-) delete mode 100644 .github/workflows/build.yml create mode 100644 .github/workflows/build_and_deploy.yml diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml deleted file mode 100644 index 0668e7c..0000000 --- a/.github/workflows/build.yml +++ /dev/null @@ -1,24 +0,0 @@ -name: Build HUGO website - -on: - # Runs on pull requests to check that the website is building without errors - pull_request: - - # Allows you to run this workflow manually from the Actions tab - workflow_dispatch: - -jobs: - # Build job - build: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - with: - submodules: recursive - - name: hugo - uses: klakegg/actions-hugo@1.0.0 - with: - source: ./src - target: ./build/blog - env: production diff --git a/.github/workflows/build_and_deploy.yml b/.github/workflows/build_and_deploy.yml new file mode 100644 index 0000000..997a2d0 --- /dev/null +++ b/.github/workflows/build_and_deploy.yml @@ -0,0 +1,73 @@ +name: Build and deploy HUGO website + +on: + # Runs on pull requests to check that the website is building without errors + pull_request: + + # Runs on push/merge to main to deploy the new website + # Only run if the push updates website sources + push: + paths: + - src/** + branches: + - main + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +jobs: + # Build job + build: + runs-on: ubuntu-latest + steps: + # Checkout repo AND ITS SUBMODULES + - name: 🛒 Checkout + uses: actions/checkout@v3 + with: + submodules: recursive + + # Build the static website with the provided docker-compose rules + - name: 🛠️ Build with HUGO + run: | + docker compose up builder + + # Upload build artifacts for deployment or download + - name: 🚀 Upload Artifacts + uses: actions/upload-artifact@v3 + with: + name: build + path: ./build/blog + + # Deployment job: heavily inspired from https://swharden.com/blog/2022-03-20-github-actions-hugo/ + # /!\ only triggers on push events + deploy: + needs: [build] + if: ${{ github.event_name == 'push' }} + runs-on: ubuntu-latest + steps: + - name: 🛠️ Setup build directory + run: | + mkdir -p build/blog + - name: 📥 Download build Artifacts + uses: actions/download-artifact@v3 + with: + name: build + path: build/blog + + # Create the SSH key file and fill the known_hosts to avoid a prompt from ssh (1st time connecting to remote host) + - name: 🔐 Create Key File + run: | + mkdir ~/.ssh + touch ~/.ssh/id_rsa + chmod 600 ~/.ssh/id_rsa + - name: 🔐 Load Host Keys + run: | + echo "${{ secrets.SSH_KNOWN_HOSTS }}" > ~/.ssh/known_hosts + - name: 🔑 Populate Key + run: | + echo "${{ secrets.PRIVATE_SSH_KEY }}" > ~/.ssh/id_rsa + + # Finally upload the build to the remote server location: the volume shared by the nginx container serving http requests + - name: 🚀 Upload + run: | + rsync --archive --stats --verbose --delete ./build/blog/* ${{ secrets.CI_USER_NAME }}@iscsc.fr:${{ secrets.STATIC_WEBSITE_PATH}}