Введение:
1 2 3 4 |
https://www.youtube.com/watch?v=Yg5rpke79X4&list=PLg5SS_4L6LYstwxTEOU05E0URTHnbtA0l&index=14 - видосик GitHub Action - позволяет запускать Linux и Windows команды, а также автоматизировать Test, Build, Deploy нашего кода в Repasitory GitHub без использования серверов для этой автоматизации например Jenkins. |
0. Включение автоматизации GitHub Action
1 2 3 4 5 6 7 8 |
Включение автоматизации происходит добавлением файлов YAML в специальную директорию .github/workflows в вашем Repository .github/workflows/myfile_pipe_line.yml *.yml - имя файла YAML может быть любым Синтаксис очень похож на Ansible и Dockerfile |
1. default файл YAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# This is a basic workflow to help you get started with Actions name: CI # Controls when the action will run. Triggers the workflow on push or pull request # events but only for the master branch on: push: branches: [ master ] pull_request: branches: [ master ] # A workflow run is made up of one or more jobs that can run sequentially or in parallel jobs: # This workflow contains a single job called "build" build: # The type of runner that the job will run on runs-on: ubuntu-latest # Steps represent a sequence of tasks that will be executed as part of the job steps: # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it - uses: actions/checkout@v2 # Runs a single command using the runners shell - name: Run a one-line script run: echo Hello, world! # Runs a set of commands using the runners shell - name: Run a multi-line script run: | echo Add other actions to build, echo test, and deploy your project. |
2. Создание и редактирование Action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
!!! идем в репозитории -> Action -> Skip this and set up a workflow yourself !!! попадаем в редактор Action !!! Называем файл как хотим !!! клавиши ctrl + space - авто подстановка значений # Коментарии... name: My-Project1 # важное имя, без пробелов env: # глобальные переменные APPLICATION_NAME : "MyFlask" # глобальные переменные DEPLOY_PACKAGE_NAME : "flask-deploy-ver-${{ github.sha }}" # глобальные переменные on: # когда запустить push: # запустить при push на ветку master branches: - master jobs: my_testing: runs-on: ubuntu-latest # какие образы используются steps: - name: Print hello message # Название задачи run : echo "Hello world from testing" # Задача - name: Execute few commands run: | echo "Hello Message1" echo "Hello Message2" echo "Aplication name: ${{ env.APPLICATION_NAME }}" - name: Git clone myproject uses: actions/checkout@v1 # получаем наш репозиторий git для виртуальной машины my_deploy: runs-on: ubuntu-latest needs: [my_testing] # выполнять после выполнения my_testing steps: - name: Print hello message run : echo "Hello world from deploy" |
3. Пример файла YAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
name: My-Project1 env: APPLICATION_NAME : "MyFlask" DEPLOY_PACKAGE_NAME : "flask-deploy-ver-${{ github.sha }}" on: push: branches: - master jobs: my_testing: runs-on: ubuntu-latest steps: - name: Print hellow message in testing run : echo "Hello world from testing" - name: Execute few commands run: | echo "Hello Message1" echo "Hello Message2" echo "Aplication name: ${{ env.APPLICATION_NAME }}" - name: List current folder run : ls -la - name: Git clone myproject uses: actions/checkout@v1 - name: List current folder run : ls -la my_deploy: runs-on: ubuntu-latest needs: [my_testing] env: VAR1: "This is Job Level var1" VAR2: "This is Job Level var2" steps: - name: Print hello message deploy run : echo "Hello world from deploy" - name: Print vars run : | echo "Var1 = ${{ env.VAR1 }}" echo "Var2 = ${{ env.VAR2 }}" echo "Var3 = $LOCAL_VAE" env: LOCAL_VAE: "This is super local Enviromment variable" - name: Deploymend package run : echo "Deploy package name is ${{ env.DEPLOY_PACKAGE_NAME }}" - name: Lets test some packages iof the are here 1 run : aws --version - name: Lets test some packages iof the are here 2 run : zip --version |
4. Пример файла yml для AWS
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
#--------------------------------------------------------------------- # GitHub Action Workflow to Deploy Flask App to AWS ElasticBeanstalk # # Version Date Info # 1.0 2019 Initial Version # # Made by Denis Astahov ADV-IT Copyleft (c) 2019 #--------------------------------------------------------------------- name: CI-CD-Pipeline-to-AWS-ElasticBeastalk env: EB_PACKAGE_S3_BUCKET_NAME : "adv-it-flask-application-packages" EB_APPLICATION_NAME : "MyFlask" EB_ENVIRONMENT_NAME : "MyFlask-env" DEPLOY_PACKAGE_NAME : "flask_app_${{ github.sha }}.zip" AWS_REGION_NAME : "us-west-2" on: push: branches: - master jobs: my_ci_part: runs-on: ubuntu-latest steps: - name: Git clone our repo uses: actions/checkout@v1 - name: Create ZIP deployment package run : zip -r ${{ env.DEPLOY_PACKAGE_NAME }} ./ -x *.git* - name: Configure my AWS Credentials uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id : ${{ secrets.MY_AWS_ACCESS_KEY }} aws-secret-access-key: ${{ secrets.MY_AWS_SECRET_KEY }} aws-region : ${{ env.AWS_REGION_NAME }} - name: Copy Deployment package to S3 bucket run : aws s3 cp ${{ env.DEPLOY_PACKAGE_NAME }} s3://${{ env.EB_PACKAGE_S3_BUCKET_NAME }}/ - name: Print Happy Message for CI finish run : echo "CI Pipeline part Finished successfully!" my_cd_part: runs-on: ubuntu-latest needs: [my_ci_part] steps: - name: Configure my AWS Credentials uses: aws-actions/configure-aws-credentials@v1 with: aws-access-key-id : ${{ secrets.MY_AWS_ACCESS_KEY }} aws-secret-access-key: ${{ secrets.MY_AWS_SECRET_KEY }} aws-region : ${{ env.AWS_REGION_NAME }} - name: Create new ElasticBeanstalk Application Version run : | aws elasticbeanstalk create-application-version \ --application-name ${{ env.EB_APPLICATION_NAME }} \ --source-bundle S3Bucket="${{ env.EB_PACKAGE_S3_BUCKET_NAME }}",S3Key="${{ env.DEPLOY_PACKAGE_NAME }}" \ --version-label "Ver-${{ github.sha }}" \ --description "CoimmitSHA-${{ github.sha }}" - name: Deploy new ElasticBeanstalk Application Version run : aws elasticbeanstalk update-environment --environment-name ${{ env.EB_ENVIRONMENT_NAME }} --version-label "Ver-${{ github.sha }}" - name: Print Happy Message for CD finish run : echo "CD Pipeline part Finished successfully!" |