This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

One, foreword

In the last section, we used Spring Boot to build a simple Web application. In this section, we upload the code to Github and use Github Actions to quickly complete the automatic packaging and uploading image function.

Second, the productionDockerfileFile Building image

Dockerfile is a text file used to build the image

We create a Dockerfile file under the root of the project in the previous section

Dockerfile defines the version of the base image, how to run the Jar package, which port to open, etc. Here is just a simple example, there are many configuration items that can be automatically used

# Docker image for springboot file run
# VERSION 0.0.1
# Author: sdp
The base image uses Java
FROM java:8
# the author
MAINTAINER sdp <[email protected]>
# VOLUME specifies the temporary file directory as/TMP.
The effect is to create a temporary file in the /var/lib/docker directory of the host and link to/TMP of the container
VOLUME /tmp
Add the jar package to the container and rename it common-mail-service.jar
ADD The target/common E-mail - service - 0.0.1 - the SNAPSHOT. The jar /common-mail-service.jar
Run the jar package
RUN bash -c 'touch /common-mail-service.jar'
ENV TZ 'Asia/Shanghai'
EXPOSE 8089
ENTRYPOINT ["java"."-jar"."/common-mail-service.jar"]
Copy the code

Three, upload the code

If you are familiar with Git, you can skip this step

1. Create a code repository and obtain the code repository address

Special note: If the three items in the following figure already exist, do not check them when creating the Repo to avoid project file conflicts

2. Upload the code

Complete step FYI:  echo "# mail-service" >> README.md git init git add README.md git commit -m "first commit" git branch -M master git remote add origin https://github.com/xxx/mail-service.git git push -u origin masterCopy the code

Four, obtainDockerHubMirror Warehouse Configuration

Docker Hub official website address

Because we use Github Actions to automatically mirror the project code and upload it to the mirror repository. Therefore, we need to prepare a mirror warehouse. In this paper, we registered an account and opened an open DockerHub mirror warehouse to store the image. In this article, the public DockerHub image warehouse as an example, such as the existing private library or Ali Cloud and other cloud platform image warehouse, can also be directly used.

1. Create a mirror vault

Create a custom mirror warehouse

2. Set the access_token

Automatic push mirroring requires account and Access_token information. You can create an Access_token in Accout Settings->Security as a credential.

Note: After the access_token is created, a copy button will be displayed in the pop-up window. Please be sure to copy and save the Access_token. After this interface is closed, you cannot see the Access_token

Five, configuration,Github Actions

Next, start Github Actions to write custom workflow profiles

1. OpenGithub AcitonsFunctions, as shown below:

2. To createGithub AcitonsRequired key parameters

To push an image to the DockerHub repository, you need to use the DockerHub account and access_token credentials created above. Under Settings->Secrets New Repository secret create the desired key value pair.

  • DOCKER_HUB_USER indicates the DockerHub account

  • DOCKER_HUB_TOKEN value The access_token credential created under the DockerHub in the step above

After completing the above steps, next we start defining the CI file

3. Create adocker_push.ymlCustom CI processes

name: Build and Publish Docker Image

# refer to https://github.com/actions/starter-workflows/blob/main/ci/docker-push.yml 

on:
  When # push is triggered, the branch is master
  push:
    branches:
      - master

env:
  # Use docker.io for Docker Hub if empty
  REGISTRY: docker.io
  # <repo>
  IMAGE_NAME: candy-common-mail-service


jobs:
  build:

    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
    
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
        
      - name: Set up JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8
      - name: Cache Maven packages
        uses: The actions/[email protected]
        with:
          path: ~/.m2
          key: The ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: The ${{ runner.os }}-m2
    
      # compile package
      - name: Build with Maven
        run: mvn package -Dmaven.test.skip=true
      
      # Login against a Docker registry except on PR
      # https://github.com/docker/login-action
      - name: Log into registry The ${{ env.REGISTRY }}
        if: github.event_name ! = 'pull_request'
        uses: docker/login-action@v1
        with:
          registry: The ${{ env.REGISTRY }}
          username: The ${{ secrets.DOCKER_HUB_USER }}
          password: The ${{ secrets.DOCKER_HUB_TOKEN }}

      # Extract metadata (tags, labels) for Docker
      # https://github.com/docker/metadata-action
      - name: Extract Docker metadata
        id: meta
        uses: docker/metadata-action@v3
        with:
          images: The ${{ env.REGISTRY / ${{}} secrets.DOCKER_HUB_USER / ${{}} env.IMAGE_NAME }}

      - name: Get Time
        id: time
        uses: Nanzm/[email protected]
        with:
          timeZone: 8
          format: 'YYYY-MM-DD-HH-mm-ss'

      # Build and push Docker image with Buildx (don't push on PR)
      # https://github.com/docker/build-push-action
      - name: Build and push Docker image
        uses: docker/build-push-action@v2
        with:
          context: .
          push: The ${{ github.event_name ! = 'pull_request' }}
          tags: The ${{ steps.meta.outputs.tags ${{}} - steps.time.outputs.time }}
          labels: The ${{ steps.meta.outputs.labels }}
Copy the code

4. Click to complete the preceding stepsActionsYou can see the workflow that has been triggered

5. DockerHubCheck the mirror on the mirror warehouse

✿ angry (°▽°) Blue ✿

summary

In this section, we created the image file Dockerfile and configured the Github Actions function on Github to automatically package the image and upload it to the DockerHub image repository while pushing the code.

Next day forecast

In this section, we have successfully packaged the image. Next, we will configure a LOCAL K8S test environment, deploy the created image and provide services externally.