The back-end

After the back-end code is committed, an automatic build (using Gradle) is packaged and the new executable JAR package is deployed to the specified server

implementation

In the. Github /workflows directory in the root directory of the project, write the gradle_build.yaml file

name: Java CI with Gradle
Set the trigger condition
Triggered when a push or pull request operation is performed on the main branch
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
# CI/CD task flow
jobs:
  build:
    Specify build container environment
    runs-on: ubuntu-latest
    steps:
    # 1: Download the source code (pull code into the build container)
    - name: Checkout
      uses: actions/checkout@master
    # 2. Set up the Java environment
    - name: Set up JDK 11
      uses: actions/setup-java@v1
      with:
        java-version: 11
    # 3. Set gradlew execution permission
    - name: Grant execute permission for gradlew
      run: chmod +x gradlew
    # 4. Cache Build cache
    - name: Cache Gradle packages
      uses: actions/cache@v2
      with:
        path: | ~/.gradle/caches ~/.gradle/wrapper        key: The ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
        restore-keys: | ${{ runner.os }}-gradle-    # 5. Project construction
    - name: Build with Gradle
      run: ./gradlew build -x test
    # 6. Deploy to the server
    - name: Copy file via scp
      uses: appleboy/scp-action@master
      env:
        HOST: The ${{ secrets.HOST }}
        USERNAME: The ${{ secrets.USERNAME }}
        PORT: The ${{ secrets.PORT }}
        KEY: The ${{ secrets.SSHKEY }}
      with:
        source: "Build/libs/dockerhub - 1.0.1. Jar"
        target: "/ opt/dockerhub - / temp 1.0.1"
    # 7. Restart service
    - name: Deploy
      uses: appleboy/ssh-action@master Use SSH to connect to the server
      with:
        host: The ${{ secrets.HOST }}
        username: The ${{ secrets.USERNAME }}
        key: The ${{ secrets.SSHKEY }}
        port: The ${{ secrets.PORT }}
        script: | mv/opt/dockerhub - 1.0.1 / temp/build/libs/dockerhub - 1.0.1. Jar/opt/dockerhub - 1.0.1 / lib/rm - rf/opt/dockerhub - 1.0.1 / temp CD/opt/dockerhub 1.0.1 / JPS | grep DockerHubService | awk '{print $1}' | xargs kill 9 nohup bin/dockerhub > nohup. Out 2> nohup.err < /dev/null &Copy the code

Matters needing attention

  1. Use it in the project root directorygradlewSo you can try it out in your local environmentgradlewBuild the project to ensure that the configuration is error-free and that thegradlewExecutable scripts are added to git repositories
  2. usegradlewConfiguration projects are generated in the root directorygradle/wrapperDirectory, which hasgradle-wrapper.jarandgradle-wrapper.propertiesTwo files to add to your Git repository
  3. willgradle/wrapper/gradle-wrapper.propertiesCopy the configuration file to your project root directory and add it to your Git repository to make the Gradle configuration take effect
  4. In steps 6 and 7, you need to remotely connect to the specified deployment server on the GitHub project interfaceSettingsOption interfaceSecretClick in the interfaceNew repository secretAdd the configuration information. The name is the name of the configuration property used in the YAML file
  5. Note in step 6 that the directory structure of source is copied to the folder specified by target, so it is placed in a temporary folder
  6. In Step 7, move the destination JAR package to the destination directory. Delete the temporary folder, restart the service, note the nohup command, if only writingnohup bin/dockerhub &The GitHub Actions execution process is stuck here

The front end

After the front-end code (Vue project) is submitted, the new package is automatically packaged and deployed to the specified server

implementation

In the. Github /workflows directory of the project root directory, write the following file vue_build.yaml

name: VUE-CI
on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master
      - name: set up node env
        uses: actions/setup-node@master
      - name: Installing project dependencies
        run: yarn
      - name: Building the project
        run: npm run build
      Use SSH to connect to the server and empty the old deployment
      - name: Deploy
        uses: appleboy/ssh-action@master 
        with:
          host: The ${{ secrets.HOST }}
          username: The ${{ secrets.USERNAME }}
          key: The ${{ secrets.SSHKEY }}
          port: The ${{ secrets.PORT }}
          script: | rm -rf /opt/dvclab/ mkdir -p /opt/dvclab/      # Copy the packaged file
      - name: Copy file via scp
        uses: appleboy/scp-action@master
        env:
          HOST: The ${{ secrets.HOST }}
          USERNAME: The ${{ secrets.USERNAME }}
          PORT: The ${{ secrets.PORT }}
          KEY: The ${{ secrets.SSHKEY }}
        with:
          source: "./dist/"
          target: "/opt/dvclab/"
Copy the code