DEV Community

Reene
Reene

Posted on

[CI/CD][Github actions] common template

Intro

  • This GitHub Actions template is designed to automate the CI/CD pipeline for a backend project. It has two main stages: Build and Deploy.

Hint:

  • Replacement instructions:Add the Secrets information provided above (such as DOCKER_USERNAME, NGROK_HOST, etc.) to GitHub's Secrets configuration one by one.
  • When using, there is no need to modify ${{ secrets.XXXX }} in the YAML file, GitHub will automatically replace them with the corresponding Secrets values.

Applicable scenarios:

  • Applicable to Docker image building and pushing.
  • Supports deploying projects to specified servers and executing Kubernetes configuration through Ngrok.
name: CI/CD for Backend on: push: branches: - main jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v3 - name: Set up JDK 17 uses: actions/setup-java@v3 with: distribution: 'temurin' java-version: '17' - name: Build with Maven run: mvn clean package -DskipTests - name: Build Docker image run: docker build -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:latest . - name: Push Docker image to DockerHub uses: docker/login-action@v2 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - run: docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }}:latest deploy: runs-on: ubuntu-latest needs: build steps: - name: Checkout code uses: actions/checkout@v3 # Load NGROK_PORT and NGROK_HOST from the config file - name: Load NGROK configuration run: | NGROK_PORT=$(yq '.NGROK_PORT' ./.github/workflows/config.yml) NGROK_HOST=$(yq '.NGROK_HOST' ./.github/workflows/config.yml) echo "NGROK_PORT=$NGROK_PORT" >> $GITHUB_ENV echo "NGROK_HOST=$NGROK_HOST" >> $GITHUB_ENV # Ensure the target directory exists - name: Prepare target directory run: | sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no -p ${{ env.NGROK_PORT }} ${{ secrets.SSH_USER }}@${{ env.NGROK_HOST }} << 'EOF' mkdir -p /home/linlin1/io-multiple-backend/ EOF # Transfer code to the target server - name: Transfer code to server run: | sshpass -p "${{ secrets.SSH_PASSWORD }}" scp -o StrictHostKeyChecking=no -P ${{ env.NGROK_PORT }} -r * ${{ secrets.SSH_USER }}@${{ env.NGROK_HOST }}:/home/linlin1/io-multiple-backend/ # Deploy Kubernetes configurations - name: Deploy on server run: | sshpass -p "${{ secrets.SSH_PASSWORD }}" ssh -o StrictHostKeyChecking=no -p ${{ env.NGROK_PORT }} ${{ secrets.SSH_USER }}@${{ env.NGROK_HOST }} << 'EOF' cd /home/linlin1/io-multiple-backend/k8s kubectl apply -f . EOF 
Enter fullscreen mode Exit fullscreen mode
Placeholder Description Example Value Secret Key in GitHub
${{ secrets.DOCKER_USERNAME }} DockerHub username your-docker-username DOCKER_USERNAME
${{ secrets.DOCKER_PASSWORD }} DockerHub password your-docker-password DOCKER_PASSWORD
${{ secrets.DOCKER_REPO }} DockerHub repository name dockerRepo DOCKER_REPO
${{ secrets.SSH_USER }} SSH username for the target server ubuntu SSH_USER
${{ secrets.SSH_PASSWORD }} SSH password for the target server 12345678 SSH_PASSWORD

configure config.yml for dynamic ip of ngork

if you choose dynamic ip configuration in ngork, then it is better to configure ngork host and port in config.yml file instead of github secrets.

  • create config.yml under the same directory:
NGROK_PORT: 12345 NGROK_HOST: 0.tcp.eu.ngrok.io 
Enter fullscreen mode Exit fullscreen mode

Image description

Top comments (0)