用Github Action 做了一个简单的CI/CD,这一次我上传cloudformation的 yaml文件,首先把他推送到我的一个S3 Bucket里面,然后利用这个yaml文件来生成对应的服务。

https://github.com/vetpartner/cf

workflow 文件如下所示,首先checkout,然后检测cloudformation的语法,然后通过secret的值进行aws的登录验证,然后拷贝文件,然后配置cloudformation。Github action上有其他人写好的各种action来进行各种高大上的操作,我这里是直接用aws cli进行的操作

# This is a basic workflow to help you get started with Actions

name: aws

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: 
      - master

jobs:
  deploy:
    name: Upload to Amazon
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v2

    - name: Check cloudformation yaml file
      uses: scottbrenner/cfn-lint-action@master
      with:
        args: "*.yaml"

    - name: Configure AWS credentials from Production account
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ap-southeast-2

    - name: Copy files to the production website with the AWS CLI
      run: |
        aws s3 sync . s3://yuanlitest
    - name: Deploy to AWS CloudFormation
      run:  aws cloudformation create-stack --stack-name myteststack2 --template-body file://vpc.yaml --parameter ParameterKey=S3BucketName,ParameterValue=githubtestcicds3bucket1 ParameterKey=UserName,ParameterValue=githubuser1 ParameterKey=PolicyName,ParameterValue=githubtests3bucket1 --capabilities CAPABILITY_NAMED_IAM  

可以看见我的cf成功执行了

Github Action- CI/CD 推送 Cloudformation 到 AWS

Github Action- CI/CD 推送 Cloudformation 到 AWS