DEV Community

muckitymuck
muckitymuck

Posted on

Github Actions: Build+Test in GitubVM, Deploy on Cloud

An interesting new way to Build and Test in the Github environment before deploying is possible using Artifacts.

It involves breaking up 2 jobs in your workflow yml.

You will use the

actions/upload-artifact@v2
actions/download-artifact@v2
Enter fullscreen mode Exit fullscreen mode

as a means to pass the artifact between the jobs.
Here, I am using NodeJS as the build, so I all I need to move is the Dist folder without Markdown.

- name: Archive production artifacts
        uses: actions/upload-artifact@v2
        with:
          name: dist-without-markdown
          path: |
            dist
            !dist/**/*.md
Enter fullscreen mode Exit fullscreen mode

You also need to remember to name the artifact in case there are multiple artifacts produced in the workflow.

Finished workflow is below.

name: CI

# Controls when the workflow will run
on: 
  push:
    branches: [prod]

jobs:
  build:
    runs-on: ubuntu-20.04      
    steps:
      ### new workflow artifact
      - name: Make envfile
        uses: SpicyPizza/create-envfile@v1
        with:
          envkey_DEBUG: false
          envkey_SECRET_KEY: ${{ secrets.ENV_FILE }}
          file_name: .env
      - name: Checkout respository
        uses: actions/checkout@v2
      - name: npm install, build and test
        run: |
          npm install
          npm run build
          npm test 

      - name: Archive production artifacts
        uses: actions/upload-artifact@v2
        with:
          name: dist-without-markdown
          path: |
            dist
            !dist/**/*.md
  deploy:
    #needs will wait for Build to finish
    needs: build
    runs-on: self-hosted 
    steps:
      - name: Runs code deploy
        uses: actions/checkout@v2
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          password: ${{ secrets.PASSWORD }}
          port: ${{ secrets.PORT }}
      - run: cd /path/to/deploy/
      - name: Make envfile
        uses: SpicyPizza/create-envfile@v1
        with:
          envkey_DEBUG: false
          envkey_SECRET_KEY: ${{ secrets.ENV_FILE }}
          file_name: .env

      - name: Archive production artifacts
        uses: actions/download-artifact@v2
        with:
          name: dist-without-markdown
          path: |
            /path/to/deploy/dist
      - run: npm ci
      - run: pm2 restart PROCESSNAME

Enter fullscreen mode Exit fullscreen mode

Top comments (0)