使用自动化进行多个环境变量设置的 GitHub Actions

2024-03-05

我正在与 GitHub 合作在多个环境上部署基于容器的应用程序,我有两个环境:

  1. Dev

  2. Prod

我正在两个环境上构建应用程序,这是我的 yml 文件:

name: 'Manual - Build & Deploy - Enterprise'

on:
  push:
    branches-ignore:
      - '**'

  workflow_dispatch:
    inputs:
      git-ref:
        description: Git Ref (Optional)
        default: develop
        required: false

      account:
        description: slb-dev, slb-prod
        default: slb-dev
        required: true
        
      account_prod:
       description: slb-dev, slb-prod
       default: slb-prod
       required: true

      environment:
        description: development (main, int, qs), production (v1_demo, v1_rosecity, demo)
        default: main
        required: false

      microservice:
        description: chroma, liquid, tenant, dashboard, lims, lims-simulator, client, logging, metrc
        default: chroma
        required: false

      builddir:
        description: MicroChromatographyService/MicroChromatographyService, MicroLiquidHandlingService/MicroLiquidHandlingService, MicroTenantService/MicroTenantService, MicroDashboardService/MicroDashboardService, LIMSIntegrationService/LIMSIntegrationService, LIMSSimulatorService/LIMSSimulatorService, IntegrationHubClientService/IntegrationHubClientService, PerkinElmer.LoggingService/PerkinElmer.LoggingService, MetRCReportService/MetRCReportService
        default: MicroChromatographyService/MicroChromatographyService
        required: false

jobs:
  setup:
    name: Setup ENV Variables
    runs-on: ubuntu-latest
    environment:
     name: Production
     url: https://dev.test.com
    steps:

    - name: Set Vars
      id: setvars
      run: |
          echo "::set-output name=APP_NAME::${{ github.event.inputs.microservice }}"
          echo "::set-output name=AWS_REGION::us-east-1"
          echo "::set-output name=SHA8::${{ github.sha }} | cut -c1-8)"
          echo "::set-output name=BUILD_DIR::${{ github.event.inputs.builddir }}"
          echo "::set-output name=ECR_REPOSITORY::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
          echo "::set-output name=ECS_CLUSTER::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}"
          echo "::set-output name=ECS_SERVICE::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
          echo "::set-output name=ECS_TASK_DEFINITION::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
          echo "::set-output name=ECS_TASK_DEFINITION_FILE::task-definition-${{ github.event.inputs.microservice }}.json"
          echo "::set-output name=ECS_CONTAINER_NAME::${{ github.event.inputs.account }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"

    outputs:
      APP_NAME: ${{ steps.setvars.outputs.APP_NAME }}
      AWS_REGION: ${{ steps.setvars.outputs.AWS_REGION }}
      SHA8: ${{ steps.setvars.outputs.SHA8 }}
      BUILD_DIR: ${{ steps.setvars.outputs.BUILD_DIR }}
      ECR_REPOSITORY: ${{ steps.setvars.outputs.ECR_REPOSITORY }}
      ECS_CLUSTER: ${{ steps.setvars.outputs.ECS_CLUSTER }}
      ECS_SERVICE: ${{ steps.setvars.outputs.ECS_SERVICE }}
      ECS_TASK_DEFINITION: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION }}
      ECS_TASK_DEFINITION_FILE: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION_FILE }}
      ECS_CONTAINER_NAME: ${{ steps.setvars.outputs.ECS_CONTAINER_NAME }}
      
  setup2:
    name: Setup prod ENV Variables
    runs-on: ubuntu-latest
    environment:
      name: dev
      url: https://production.test.com
    steps:
      
    - name: Set Vars
      id: setprodvars
      run: |
          echo "::set-output name=APP_NAME::${{ github.event.inputs.microservice }}"
          echo "::set-output name=AWS_REGION::us-east-1"
          echo "::set-output name=SHA8::${{ github.sha }} | cut -c1-8)"
          echo "::set-output name=BUILD_DIR::${{ github.event.inputs.builddir }}"
          echo "::set-output name=ECR_REPOSITORY::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
          echo "::set-output name=ECS_CLUSTER::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}"
          echo "::set-output name=ECS_SERVICE::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
          echo "::set-output name=ECS_TASK_DEFINITION::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
          echo "::set-output name=ECS_TASK_DEFINITION_FILE::task-definition-${{ github.event.inputs.microservice }}.json"
          echo "::set-output name=ECS_CONTAINER_NAME::${{ github.event.inputs.account_prod }}-${{ github.event.inputs.environment }}-${{ github.event.inputs.microservice }}"
    outputs:
      APP_NAME: ${{ steps.setvars.outputs.APP_NAME }}
      AWS_REGION: ${{ steps.setvars.outputs.AWS_REGION }}
      SHA8: ${{ steps.setvars.outputs.SHA8 }}
      BUILD_DIR: ${{ steps.setvars.outputs.BUILD_DIR }}
      ECR_REPOSITORY: ${{ steps.setvars.outputs.ECR_REPOSITORY }}
      ECS_CLUSTER: ${{ steps.setvars.outputs.ECS_CLUSTER }}
      ECS_SERVICE: ${{ steps.setvars.outputs.ECS_SERVICE }}
      ECS_TASK_DEFINITION: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION }}
      ECS_TASK_DEFINITION_FILE: ${{ steps.setvars.outputs.ECS_TASK_DEFINITION_FILE }}
      ECS_CONTAINER_NAME: ${{ steps.setvars.outputs.ECS_CONTAINER_NAME }}


      
  DeployDev:
    name: Deploy to Dev 
    needs: setup
    runs-on: ubuntu-latest
    permissions:
     packages: write
     contents: write
     id-token: write
    environment: 
      name: Dev
      url: 'http://dev.myapp.com'
    steps:
    - name: Set Environments
      run: |
        if [[ "${{github.event.inputs.account}}" == "slb-dev" ]]; then
          echo "AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
          echo "AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
        fi

        if [[ "${{github.event.inputs.account}}" == "slb-prod" ]]; then
          echo "AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
          echo "AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
        fi

    - name: Clone Repository (Current branch)
      uses: actions/checkout@v2
      if: github.event.inputs.git-ref == ''

    - name: Clone Repository (Custom Ref)
      uses: actions/checkout@v2
      if: github.event.inputs.git-ref != ''
      with:
        ref: ${{ github.event.inputs.git-ref }}

    - name: Configure AWS credentials
      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: ${{ needs.setup.outputs.AWS_REGION }}

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: ${{ needs.setup.outputs.ECR_REPOSITORY }}
        IMAGE_TAG: ${{ github.sha }}
      run: |
        cd ${{ needs.setup.outputs.BUILD_DIR }}
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"

    - name: Download task definition
      run: |
        aws ecs describe-task-definition --task-definition ${{ needs.setup.outputs.ECS_TASK_DEFINITION }} --query taskDefinition > ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}

    - name: Fill in the new image ID in the Amazon ECS task definition
      id: task-def
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
        container-name: ${{ needs.setup.outputs.ECS_CONTAINER_NAME }}
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: ${{ needs.setup.outputs.ECS_SERVICE }}
        cluster: ${{ needs.setup.outputs.ECS_CLUSTER }}
        wait-for-service-stability: true


  DeployProd:
    name: Deploy to Production 
    needs: [setup2, DeployDev]
    runs-on: ubuntu-latest
    permissions:
     packages: write
     contents: write
     id-token: write
    environment: 
      name: Production
      url: 'http://dev.myapp.com'
    steps:
    - name: Set Environments
      run: |
        if [[ "${{github.event.inputs.account_prod}}" == "slb-dev" ]]; then
          echo "AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
          echo "AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
        fi
        if [[ "${{github.event.inputs.account_prod}}" == "slb-prod" ]]; then
          echo "AWS_ACCESS_KEY_ID=${{ env.AWS_ACCESS_KEY_ID }}" >> $GITHUB_ENV
          echo "AWS_SECRET_ACCESS_KEY=${{ env.AWS_SECRET_ACCESS_KEY }}" >> $GITHUB_ENV
        fi
    - name: Clone Repository (Current branch)
      uses: actions/checkout@v2
      if: github.event.inputs.git-ref == ''

    - name: Clone Repository (Custom Ref)
      uses: actions/checkout@v2
      if: github.event.inputs.git-ref != ''
      with:
        ref: ${{ github.event.inputs.git-ref }}

    - name: Configure AWS credentials
      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: ${{ needs.setup2.outputs.AWS_REGION }}

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: ${{ needs.setup.outputs.ECR_REPOSITORY }}
        IMAGE_TAG: ${{ github.sha }}
      run: |
        cd ${{ needs.setup.outputs.BUILD_DIR }}
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG -t $ECR_REGISTRY/$ECR_REPOSITORY:latest .
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:latest
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
    - name: Download task definition
      run: |
        aws ecs describe-task-definition --task-definition ${{ needs.setup.outputs.ECS_TASK_DEFINITION }} --query taskDefinition > ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
    - name: Fill in the new image ID in the Amazon ECS task definition
      id: task-def
      uses: aws-actions/amazon-ecs-render-task-definition@v1
      with:
        task-definition: ${{ needs.setup.outputs.ECS_TASK_DEFINITION_FILE }}
        container-name: ${{ needs.setup.outputs.ECS_CONTAINER_NAME }}
        image: ${{ steps.build-image.outputs.image }}

    - name: Deploy Amazon ECS task definition
      uses: aws-actions/amazon-ecs-deploy-task-definition@v1
      with:
        task-definition: ${{ steps.task-def.outputs.task-definition }}
        service: ${{ needs.setup.outputs.ECS_SERVICE }}
        cluster: ${{ needs.setup.outputs.ECS_CLUSTER }}
        wait-for-service-stability: true

正如您所看到的,我们只是复制环境变量,但我们想要的本质上是摆脱该步骤,并使用我们拥有的输入的单个步骤来完成它,然后事情应该自动从开发到生产,我认为可以做一些基于 if 条件的事情,但我只是不确定如何实现它,欢迎任何想法,谢谢。


None

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用自动化进行多个环境变量设置的 GitHub Actions 的相关文章

  • 自定义 github 页面的预览图像

    是否可以自定义在将链接发布到 github 页面时看到的预览图像 我觉得他们专门解决了 github 存储库的问题here https docs github com en github administering a repository
  • YAML 中的多行键

    是否可以有这样的多线路键 mykey gt key one keytwo val 其中 keyone 被视为一把钥匙 我想解析 yaml 来产生 mykey keyone keytwo val 您可以在 YAML 中拥有多行密钥 但不完全按
  • 将 sourceforge 票证迁移到 GitHub 问题 [重复]

    这个问题在这里已经有答案了 有谁知道有什么脚本 包可以自动将 sourceforge bug 功能请求跟踪器中的票证迁移到 GitHub 问题吗 可以将 sourceforge 票证导出为 XML 所以我想应该可以编写一个脚本来自动在 Gi
  • Capistrano 和 GitHub Private Repo – 权限被拒绝(公钥)

    我继承了一个托管在 Linode 上的 Rails 项目 之前的开发人员使用 BitBucket 存储库以及 Capistrano 进行部署 我已经在 GitHub 上设置了一个私人存储库 并且正在尝试让 Capistrano 配方发挥作用
  • Maven 找不到 .git (dotGitDirectory)

    我有一个与所问问题类似的问题here https stackoverflow com questions 31159484 mavengit commit id plugin git directory could not be found
  • 如何从 Visual Studio 2013 将现有解决方案添加到 GitHub

    我浏览了许多关于 VS 2013 中新的 Git 集成的网页 它们不涉及向 Github 添加现有解决方案 事实上 我找不到太多关于使用 GitHub 而不是 Visual Studio Online 的信息 谁能告诉我如何从现有解决方案开
  • git push origin master 失败

    我正在尝试将现有文件夹上传到 gitHub 按照说明 我在 Windows 中输入了以下内容 git remote add origin email protected cdn cgi l email protection myname m
  • 收到别人的pull-request,如何编辑然后直接在github上合并?

    作为参考 这里是一个命令行git解决方案如何处理审查拉取请求 修改代码和合并 https stackoverflow com questions 27355333 how to handle review a pull request mo
  • 添加 Git 远程后“致命:拒绝合并不相关的历史记录”

    我已将远程存储库添加到我正在使用的文件夹中 git remote add origin https github com
  • 图表贡献者为空

    我在 github 上有几个项目 但其中一些项目的贡献者图是空的 即使我的 gitconfig 设置了名称和电子邮件 https github com jlengrand batchWaterMarking graphs contribut
  • 自动更改 github 文件

    我制作了一个带有白名单的应用程序 withelist 位于 github 存储库上 只有一个文件 即 withelist 每次下载我的应用程序的用户想要被允许使用该应用程序时 都必须向我发送一个消息插入白名单 现在这个过程真的很慢 我想加快
  • 如何删除“致命:松散物体”?

    我的一个克隆存储库是从 git fsck 获取的 致命 松散对象 40bda4e3b79c3d7bf598df31d9e68470f97a3f79 存储在 git objects 40 bda4e3b79c3d7bf598df31d9e68
  • 使用 Github 注册表中的 Docker 镜像未经授权

    我在本地创建了 Docker 镜像 将其标记为 Github Docker 注册表 将其推送到 Github Docker 注册表 现在我想在 Github 操作中使用它来创建 Docker 镜像FROM字段但它总是失败unauthoriz
  • 在 github 上查找强制推送的提交者

    在我们的项目 托管在 GitHub 上 中 每隔一段时间就会有人意外强制推送 master 没有人知道是否这样做 我想找出是谁干的 以及背后有什么样的错误配置的工具或坏习惯 那么问题来了 如何识别进行强制推送的用户呢 当我拉动时 我看到这样
  • github 操作未收到机密

    我看过其他答案 但似乎都不起作用 我想我只是想用 Github Actions 做一些非常简单的事情 只需让 access key 可用于我的 github 操作 而不将其放入我的 github 存储库中 所以我认为我们可以创造action
  • Flutter 分析/构建在 GitHub 操作中失败

    当运行以下操作时 它会失败flutter analyze 如果我删除它 稍后会失败flutter build 这两个命令在本地都可以正常工作 我理解该消息 但无法理解包路径可能有什么问题 GitHub 操作错误 flutter analyz
  • 错误:GitHub 目前无法显示这么大的文件 - 但该文件只有 1.06 MB [已关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我通过两种不同的方式将 HTML 文件上传到 GitHub 存储库 拖放以及使用终端上的 Git 添加 提交 推送 在这两种情况下
  • 子 git 存储库作为主存储库的子集

    我正在寻找一种方法来设置 git 存储库 其中包括来自较大存储库的文件子集 并从该主存储库继承历史记录 我的主要动机是能够通过 GitHub 共享代码子集 我目前通过单个 git 存储库管理我的研究相关 主要是 Matlab 代码 代码本身
  • 由于合并而不允许 git revert 但未给出 -m 选项

    我正在尝试使用 revert 命令恢复到 git 中的某个 哈希 号 我正在使用以下命令 git revert c14609d74eec3ccebafc73fa875ec58445471765 但是 我得到以下返回 错误 提交 c14609
  • 我可以通过链接分享我的私人 GitHub 存储库吗?

    我在 GitHub 上的私人存储库中有一个 Java 应用程序 我想与没有帐户的人共享它 我在网站上没有找到任何与此相关的选项 有没有办法做到这一点 协作者只能是 GitHub 用户 无法在非 Github 用户之间共享私有存储库 您需要

随机推荐