Skip to content

github action + docker image #24

@san3Xian

Description

@san3Xian

huawei docker image registry + github repository's docker image + github action's artifact

workflow file (maven file location should be changed, container name and port should be changed)

name: build docker image and deploy

on:
  release:
    types: [created]

jobs:
  build_image:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set up JDK 1.8
      uses: actions/setup-java@v1
      with:
        java-version: 1.8
        server-id: github # Value of the distributionManagement/repository/id field of the pom.xml
        settings-path: ${{ github.workspace }} # location for the settings.xml file
    
    - name: Prepare environment variable information
      run: |
        echo "::set-env name=HUAWEI_IMAGE_SITE::***.myhuaweicloud.com"
        echo "::set-env name=HUAWEI_IMAGE_URL::***.myhuaweicloud.com/{{{organization name}}}"
        
        VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),\1,')
        echo "::set-env name=IMAGE_VERSION::$VERSION"
        echo "::set-env name=IMAGE_NAME::{{{docker image name,use in docker save file name and github's repository docker image name}}}"
        
    - name: Log into Huawei registry
      run: echo -n "${{ secrets.huawei_registry_password }}" | docker login --username=${{ secrets.huawei_registry_username }} ${HUAWEI_IMAGE_SITE} --password-stdin
    
    - name: Log into Github registry
      run: echo -n "${{ secrets.githubtoken }}" | docker login --username=${{ github.repository_owner }} docker.pkg.github.com --password-stdin
    
    - name: Build jar file with Maven
      run: mvn -B package --file travel/pom.xml

    - name: Build docker image
      run: docker build ./travel/target --file Dockerfile --tag image
        
    - name: Push docker image to Huawei's docker image registry
      run: | 
          docker tag image ${HUAWEI_IMAGE_URL}/${IMAGE_NAME}:${IMAGE_VERSION}
          docker push ${HUAWEI_IMAGE_URL}/${IMAGE_NAME}:${IMAGE_VERSION}
          echo ${HUAWEI_IMAGE_URL}/${IMAGE_NAME}:${IMAGE_VERSION} > image_metadata
          
    - name: Push docker image to Github's repository image registry
      run: |
        docker tag image docker.pkg.github.com/${{ github.repository }}/${IMAGE_NAME}:${IMAGE_VERSION}
        docker images
        docker push docker.pkg.github.com/${{ github.repository }}/${IMAGE_NAME}:${IMAGE_VERSION}
    
    - name: Save docker image to a archive file
      run: |
        docker save docker.pkg.github.com/${{ github.repository }}/${IMAGE_NAME}:${IMAGE_VERSION} | gzip > ${IMAGE_NAME}_${IMAGE_VERSION}.tar.gz
    
    - name: Upload image archive file to action artifact
      uses: actions/upload-artifact@v1
      with:
       name: image_save
       path: ${{ env.IMAGE_NAME }}_${{ env.IMAGE_VERSION }}.tar.gz
        
    - name: Upload image metadata to artifact for next job
      uses: actions/upload-artifact@v1
      with:
       name: temp
       path: image_metadata
  
  run_image:
    runs-on: ubuntu-latest
    needs: build_image
    steps:
      - name: Get image metadata from artifact
        uses: actions/download-artifact@v1
        with:
          name: temp
          path: temp

      - name: Server docker container republish
        run: |
          cat <<EOF > temp_server_key
          ${{ secrets.server_key }}
          EOF
          chmod 0600 temp_server_key
          image=`cat temp/image_metadata`
          ssh -p22 -o StrictHostKeyChecking=no -i ./temp_server_key root@*** bash /root/sanXian/docker_publish.sh $image {{{container name}}} {{{container port}}}
          sleep 60
          ssh -p22 -o StrictHostKeyChecking=no -i ./temp_server_key root@*** docker logs {{{container name}}}

server deploy file

root@ubuntu18:~/sanXian# cat docker_publish.sh 
#!/bin/bash
########################################
# Author: SanXian
# $1 image repostory and version
# $2 container name
# $3 container port(same with host port)
#
#########################################

set -eu

docker_image=$1
pull_result=`docker pull $docker_image`
if [[ $pull_result =~ "Image is up to date" ]]
then
    echo "[Debug] Image is up to date"
else
    container_name=$2
    container_port=$3
    echo "[Debug] Container $2 port $3 going to rebuild or build"
    docker stop $2 || true
    docker rm $2 || true
    docker run -it -d --name $2 -p $3:$3 $1
fi

docker file

FROM openjdk:8u212-jdk-alpine

WORKDIR /app

COPY travel-0.0.1-SNAPSHOT.jar travel-0.0.1-SNAPSHOT.jar

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone

CMD java $JAVA_OPTS -XX:+UnlockExperimentalVMOptions -XX:+UseCGroupMemoryLimitForHeap \
         -Djava.security.egd=file:/dev/./urandom -jar travel-0.0.1-SNAPSHOT.jar

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions