Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deploy to different ELB environments based on code pushed to branch #5

Closed
yashwp opened this issue Jan 6, 2020 · 2 comments
Closed

Comments

@yashwp
Copy link

yashwp commented Jan 6, 2020

@einaregilsson My concern is how can I deploy my application to different environments on the AWS Beanstalk based on which branch the code is being pushed.

For example - I have two environment prod and staging having respective branches master and staging. I want to trigger the CI/CD procedure whenever the code is pushed to these branches.

The only thing that needs to be taken care of is that when code pushed to staging branch the application should be deployed to staging environment and if pushed to master branch the build for prod environment should be triggered.

Travis CI has the same thing like below:

deploy:
  - provider: elasticbeanstalk
    access_key_id: $AWS_ACCESS_KEY
    secret_access_key: $AWS_SECRET_KEY
    region: "ap-south-1"
    app: "my-app"
    env: "my-app-staging"
    bucket_name: "<s3_bucket_name>"
    on:
      branch: staging

  - provider: elasticbeanstalk
    access_key_id: $AWS_ACCESS_KEY
    secret_access_key: $AWS_SECRET_KEY
    region: "ap-south-1"
    app: "my-app"
    env: "my-app-production"
    bucket_name: "<s3_bucket_name>"
    on:
      branch: master

So, how can I implement the same thing in github action using your action?

@einaregilsson
Copy link
Collaborator

einaregilsson commented Jan 6, 2020

I think the best way to do that would be to use if conditions on the steps. NOTE: I have not tried this myself, but it should be possible with something like:

- name: Deploy to EB
  if: github.ref == 'master'
  uses: einaregilsson/beanstalk-deploy@v4
  with:
    aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    application_name: MyApplicationName
    environment_name: prod
    version_label: 12345
    region: us-west-2
    deployment_package: deploy.zip

and then have another block for staging. Or, you could use an expression for the environment name, something like:

- name: Deploy to EB
  uses: einaregilsson/beanstalk-deploy@v4
  with:
    aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    application_name: MyApplicationName
    environment_name: {{ github.ref }} 
    version_label: 12345
    region: us-west-2
    deployment_package: deploy.zip

Of course, this would only work if your branches were named the same thing as your environments, but you could also just set an environment variable before based on the branches, something like:

- run: echo "::set-env name=ENVIRONMENT_NAME::prod"
  if: github.ref == 'master'
- run: echo "::set-env name=ENVIRONMENT_NAME::staging"
  if: github.ref == 'staging'

# and then

- name: Deploy to EB
  uses: einaregilsson/beanstalk-deploy@v4
  with:
    aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }}
    aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
    application_name: MyApplicationName
    environment_name: {{ env.ENVIRONMENT_NAME }} 
    version_label: 12345
    region: us-west-2
    deployment_package: deploy.zip

So that's something to try out. Look at https://help.github.com/en/actions/automating-your-workflow-with-github-actions/contexts-and-expression-syntax-for-github-actions#about-context-and-expressions for more info about expression syntax in GitHub Actions

@einaregilsson
Copy link
Collaborator

Closing this, since it seems to have a reasonable workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant