Skip to content

Dashboard

Cấu hình CI/CD với Github (phần 4): Các mẫu github actions

Created by Admin

Các bạn có thể sử dụng nhữn mẫu sau để tạo nhanh một flow CI/CD, chỉ cần copy về sửa lại một chút theo yêu cầu của mỗi dự án, thêm secret key là có thể cấu hình một flow để tự động buid, deploy một project lên production trong 2 nốt nhạc.

Một số mẫu cơ bản:

env:
  AZURE_WEBAPP_NAME: your-app-name 
  AZURE_WEBAPP_PACKAGE_PATH: '.' 
  NODE_VERSION: '10.x' 

jobs:
  build-and-deploy:
    name: Build and Deploy
    runs-on: ubuntu-latest
    environment: production
    steps:
    - uses: actions/[email protected]
    - name: Use Node.js ${{ env.NODE_VERSION }}
      uses: actions/[email protected]
      with:
        node-version: ${{ env.NODE_VERSION }}
    - name: npm install, build, and test
      run: |
        npm install
        npm run build --if-present
        npm run test --if-present
    - name: 'Deploy to Azure WebApp'
      uses: azure/[email protected]
      with:
        app-name: ${{ env.AZURE_WEBAPP_NAME }}
        publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
        package: ${{ env.AZURE_WEBAPP_PACKAGE_PATH }}
name: Deploy to Amazon ECS

jobs:
  deploy:
    name: Deploy
    runs-on: ubuntu-latest
    environment: production

    steps:
    - name: Checkout
      uses: actions/[email protected]

    - name: Configure AWS credentials
      uses: aws-actions/[email protected]
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: us-east-2

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/[email protected]

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: my-ecr-repo
        IMAGE_TAG: ${{ github.sha }}
      run: |
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
    - name: Fill in the new image ID in the Amazon ECS task definition
      id: task-def
      uses: aws-actions/[email protected]
      with:
        task-definition: task-definition.json
        container-name: sample-app
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/[email protected]
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: sample-app-service
        cluster: default
        wait-for-service-stability: true
jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [12.x]

    steps:
    - uses: actions/[email protected]
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/[email protected]
      with:
        node-version: ${{ matrix.node-version }}
    - name: npm install and build
      run: |
        npm install
        npm run build
    - name: GitHub Action for Heroku
      uses: actions/[email protected]
      env:
        CI: true
jobs:
  test:

    runs-on: ubuntu-latest

    steps:
      - uses: actions/[email protected]
      - name: Set up Ruby
        uses: ruby/[email protected]
        with:
          ruby-version: 2.6
      - name: Install dependencies
        run: bundle install
      - name: Run tests
        run: bundle exec rake
jobs:
  build:

    name: Build and test
    runs-on: ubuntu-latest

    steps:
    - uses: actions/[email protected]
    - name: Set up Elixir
      uses: erlef/[email protected]
      with:
        elixir-version: '1.10.3' # Define the elixir version [required]
        otp-version: '22.3' # Define the OTP version [required]
    - name: Restore dependencies cache
      uses: actions/[email protected]
      with:
        path: deps
        key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
        restore-keys: ${{ runner.os }}-mix-
    - name: Install dependencies
      run: mix deps.get
    - name: Run tests
      run: mix test
jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/[email protected]
    - name: Set up JDK 11
      uses: actions/[email protected]
      with:
        java-version: '11'
        distribution: 'adopt'
    - name: Build with Ant
      run: ant -noinput -buildfile build.xml
name: Node.js CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:
  build:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [10.x, 12.x, 14.x, 15.x]

    steps:
      - uses: actions/[email protected]
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/[email protected]
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci
      - run: npm run build --if-present
      - run: npm test
Source: https://viblo.asia/p/cau-hinh-cicd-voi-github-phan-4-cac-mau-github-actions-ORNZqdGnK0n