Skip to content

Add an article for multiple deployment #40

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

Merged
merged 4 commits into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions blog/2019-05-28-first-blog-post.md

This file was deleted.

44 changes: 0 additions & 44 deletions blog/2019-05-29-long-blog-post.md

This file was deleted.

20 changes: 0 additions & 20 deletions blog/2021-08-01-mdx-blog-post.mdx

This file was deleted.

Binary file not shown.
25 changes: 0 additions & 25 deletions blog/2021-08-26-welcome/index.md

This file was deleted.

Binary file added blog/2022-03-10-deploy-to-multi-envs/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
133 changes: 133 additions & 0 deletions blog/2022-03-10-deploy-to-multi-envs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
slug: deploy-to-multi-envs
title: Deploy to multiple environments with GitHub Action
description: Gitploy provides another solution deploying to multiple environments.
tags: [GitHub Action, Multiple Environments]
image: ./demo.gif
keywords: [deploy, multiple environments, different environments, github action]
---

Having a well-tuned workflow keeps a team productive and helps them deliver reliable software. One recommended practice for any significant software development is using multiple environments. Using multiple environments ensures that software is rigorously tested before deployed and made available to users.

A general way for deploying to an environment is the branch strategy. Users create branches for each environment and deploy them by pushing a commit to the branch. Then, The deployment tool such as GitHub Action or Circle CI will deploy to the environment corresponding to the branch name.

The following is an example implemented by a GitHub Action. Firstly, it wants to trigger the workflow on push events for specific branches such as `dev` and `prod`. And in the step, through the `if` syntax, the workflow deploys it to a different environment for each branch.

<details>
<summary>Deploy By Branch Push</summary>

```yaml title=".github/workflows/deploy-by-branch.yaml"
on:
push:
branches:
- dev
- staging
- prod

jobs:
deploy:
runs-on: ubuntu-latest
steps:
-
name: Set env vars for dev
if: github.ref_name == 'dev'
run: |
echo "AZURE_FUNCTIONAPP_NAME=gitploy-dev" >> $GITHUB_ENV
echo "PUBLISH_PROFILE_VAR_NAME=AZURE_FUNCTIONAPP_PUBLISH_PROFILE_DEV" >> $GITHUB_ENV
-
name: Set env vars for prod
if: github.ref_name == 'prod'
run: |
echo "AZURE_FUNCTIONAPP_NAME=gitploy-prod" >> $GITHUB_ENV
echo "PUBLISH_PROFILE_VAR_NAME=AZURE_FUNCTIONAPP_PUBLISH_PROFILE_PROD" >> $GITHUB_ENV
-
uses: Azure/functions-action@v1
with:
app-name: ${{ env.AZURE_FUNCTIONAPP_NAME }}
publish-profile: ${{ secrets[env.PUBLISH_PROFILE_VAR_NAME] }}
respect-funcignore: true
```
</details>

**However, although the branch strategy is easy and fast, it is not flexible and scalable.** The user must create a new branch and modify the workflow if a new environment is added.

On the other hand, Gitploy provides another solution. **Gitploy dispatches an event that includes information necessary for deployment, such as the environment name, Function App name, and so on, through the [deployment API](https://docs.github.com/en/rest/reference/deployments#deployments) when a user triggers.** To activate Gitploy, we should place the `deploy.yml` file in the root of the git repository like below. Note that the `payload` field has the extra information for deployment, and it has different information for each environment, respectively. When a user triggers, the `payload` is passed to the deployment tooling.

<details>
<summary>deploy.yml</summary>

```yaml title="deploy.yml"
envs:
- name: dev
auto_merge: false
payload:
AZURE_FUNCTIONAPP_NAME: gitploy-dev
PUBLISH_PROFILE_VAR_NAME: AZURE_FUNCTIONAPP_PUBLISH_PROFILE_DEV
- name: prod
auto_merge: false
payload:
AZURE_FUNCTIONAPP_NAME: gitploy-prod
PUBLISH_PROFILE_VAR_NAME: AZURE_FUNCTIONAPP_PUBLISH_PROFILE_PROD
```

</details>

And in the workflow, the information for deployment is read through the `github.event.deployment.payload` context. The thing is that the workflow does not need `if` syntax for conditional steps anymore because an event has information.

<details>
<summary>Deploy By Gitploy</summary>

```yaml title=".github/workflows/deploy-by-gitploy.yaml"
on:
deployment

jobs:
deploy:
runs-on: ubuntu-latest
steps:
-
uses: chrnorm/deployment-status@releases/v1
with:
deployment_id: ${{ github.event.deployment.id }}
description: Start to deploy.
state: "in_progress"
token: "${{ github.token }}"

-
uses: Azure/functions-action@v1
with:
app-name: ${{ github.event.deployment.payload['AZURE_FUNCTIONAPP_NAME'] }}
publish-profile: ${{ secrets[github.event.deployment.payload['PUBLISH_PROFILE_VAR_NAME'] }}
respect-funcignore: true

-
if: success()
uses: chrnorm/deployment-status@releases/v1
with:
deployment_id: ${{ github.event.deployment.id }}
description: Success to deploy.
state: "success"
token: "${{ github.token }}"
-
if: failure()
uses: chrnorm/deployment-status@releases/v1
with:
deployment_id: ${{ github.event.deployment.id }}
description: Failed to deploy.
state: "failure"
token: "${{ github.token }}"
```

</details>

Finally, when you trigger on Gitploy, you can see that it successfully deploys to the environment. 🚀

![Demo](./demo.gif)

*Thanks for reading, and stay tuned for a more good read. If you have a question, please post questions or comments to our [community](https://github.com/gitploy-io/gitploy/discussions)*

**Reference**

* [Github Actions: deploy to multiple environments from single workflow](https://www.maxivanov.io/github-actions-deploy-to-multiple-environments-from-single-workflow/)
* Demo: [gitploy-io/multi-env-demo](https://github.com/gitploy-io/multi-env-demo)

10 changes: 5 additions & 5 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const config = {
position: 'left',
label: 'Docs',
},
// {to: '/blog', label: 'Blog', position: 'left'},
{to: '/blog', label: 'Blog', position: 'left'},
{
href: 'https://github.com/gitploy-io/gitploy',
position: 'right',
Expand Down Expand Up @@ -134,10 +134,10 @@ const config = {
{
title: 'More',
items: [
// {
// label: 'Blog',
// to: '/blog',
// },
{
label: 'Blog',
to: '/blog',
},
{
label: 'Cloud',
href: 'https://cloud.gitploy.io',
Expand Down