If the React project is used, this configuration is not required. You can use the SCP Action to deploy it on the server.

Front knowledge

Before reading this article, you should have a brief understanding of the following concepts.

  • Docker -Docker tutorial
  • SSH Remote SSH tutorial
  • Github Actions – Github Actions tutorial
  • Next. Js official documentation

The target schedule

  • In making the ActionsbuildDocker image and push to ari ACR.
  • Use SSH to link ali ECS in Github Actions,pullandrunThe mirror.
  • An expired image is automatically deleted from the ACR.
  • An image of an expired version is automatically deleted from the server.
  • Build the cache.
  • Configure docker’s network to interact with other containers.

The main process

  1. The local usegit push origin mainCommand.
  2. Github ActionsAccording to the root directory.github/workflowsUnder theyamlFile configuration is carried outjobsOperation.

Dockerfile

# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why  libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /information-manage-com
COPY package.json yarn.lock ./
RUN yarn config set registry https://registry.npm.taobao.org/
RUN yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /information-manage-com
COPY.
COPY --from=deps /information-manage-com/node_modules ./node_modules
RUN yarn build

# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /information-manage-com

ENV NODE_ENV production

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /information-manage-com/next.config.js ./
COPY --from=builder /information-manage-com/public ./public
COPY --from=builder /information-manage-com/.next ./.next
COPY --from=builder /information-manage-com/node_modules ./node_modules

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /information-manage-com/.next
USER nextjs

EXPOSE 3000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# RUN npx next telemetry disable

CMD ["node_modules/.bin/next"."start"]d
Copy the code

Next. Js docker official configuration, which I added NPM domestic image.

build-docker

The build-Docker function builds a Docker and pushes it to a Docker Hub or Ali ACR.

build-docker:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout
      uses: actions/checkout@v2
      with:
        ref: main
    - name: Login to ACR
      uses: aliyun/acr-login@v1
      with:
        login-server: <ACR Public Network Path >
        username: "${{ secrets.ACR_USERNAME }}"
        password: "${{ secrets.ACR_PASSWORD }}"
    - name: Build and push image
      env:
        IMAGE_TAG: The ${{ github.sha }}
      run: | docker build - t < ACR public path > / < ACR namespace > / < ACR iamges name > : $IMAGE_TAG. Docker push < ACR public path > / < ACR namespace > / < ACR iamges name>:$IMAGE_TAGCopy the code

Among them, secret information can be set secrets in github project setting. ${{secrets.< keys you set >}} ${{github. Sha}} represents the unique ID that can be used as a Docker tag. Second, two actions are used. For details, refer to the documentation.

  • Switch branches Actions /checkout@v2
  • Login ACR aliyun/ACR – login @ v1

ecs-pull

  ecs-pull:
    needs: build-docker
    runs-on: ubuntu-latest
    steps:
    - name: Execute SSH commmands on remote server
      uses: fifsky/ssh-action@master
      with:
        user: root
        host: "${{ secrets.ECS_HOST }}"
        key: The ${{ secrets.ECS_PRIVATE_KEY }}
        command: | CD/docker login - username = ${{secrets. ACR_USERNAME}} - password = ${{secrets. ACR_PASSWORD}} < ACR public path > docker pull 
      
       /
       
        /
        
         :${{github. Sha}} docker run -p 80:3000 -d 
         
          /
          
           /
           
            :${{ github.sha }}
           
          
         
        
       
      Copy the code

Note: the key is the private key generated by the server (full copy, not only the middle part of the copy), and the public key generated with the private key in the authorization file (Ali cloud is authorized_keys). “Needs” means that the job needs to wait for the completion of build-Docker before execution. Otherwise, jobs are processed in parallel. The official Action documentation is as follows:

  • Fifsky/SSH – action @ master SSH link

Horizontal contrast

Superficial comparison: Jenkins using his own server can also achieve automatic deployment by linkage with Web hooks of other websites, but Jenkins needs to consume his own service resources. GitHub Actions is a free resource. Moreover, if you do not use Docker, you need to install and configure a lot of services on your own server, which is very troublesome and error-prone.