|
1 | | -# Github Actions |
| 1 | +# GitHub Actions |
2 | 2 |
|
3 | | -TODO |
| 3 | +## Configuring deployment environments |
| 4 | + |
| 5 | +To start using GitHub Actions, we need to prepare the environments we want to deploy to. |
| 6 | + |
| 7 | +For example, these environments can be: |
| 8 | + |
| 9 | +- production |
| 10 | +- acceptance (or staging) |
| 11 | +- test |
| 12 | + |
| 13 | +### Configuring the production environment |
| 14 | + |
| 15 | +Hypernode Deploy will need a pair of SSH keys for authentication to the server. |
| 16 | + |
| 17 | +First, we generate an SSH keypair on the production server, copy the public key to the `~/.ssh/authorized_keys` file |
| 18 | +and encode the private key with base64. We'll use this base64-encoded private key later on. |
| 19 | + |
| 20 | +```console |
| 21 | +app@abc-example-magweb-cmbl:~$ ssh-keygen -t ed25519 -C gh-actions-deploy -f gh-actions-deploy -q -P "" |
| 22 | +app@abc-example-magweb-cmbl:~$ cat gh-actions-deploy.pub >> ~/.ssh/authorized_keys |
| 23 | +app@abc-example-magweb-cmbl:~$ cat gh-actions-deploy | base64 -w0 # encode the private key with base64 |
| 24 | +LS0tLS1CRUdJTiBPUEVOU1NIIFBSSVZBVEUgS0VZLS0tLS0KYjNCbGJuTnphQzFyWlhrdGRqRUFBQUFBQkc1dmJtV... |
| 25 | +``` |
| 26 | + |
| 27 | +Now go to your GitHub project and go to **Settings -> Environments**. |
| 28 | + |
| 29 | +1. Create a new environment called `production`, if it doesn't exist yet. |
| 30 | +1. Click the `production` environment and click `Add secret`. |
| 31 | +1. Set the **Name** to `SSH_PRIVATE_KEY`. |
| 32 | +1. Set the **Value** to the base64-encoded private key we generated earlier. |
| 33 | +1. Click **Add secret**. |
| 34 | + |
| 35 | +### Configuring the acceptance environment |
| 36 | + |
| 37 | +If you have an acceptance (or staging) environment, repeat the same steps for the production environment, but now for |
| 38 | +your acceptance environment, with the GitHub environment name `acceptance`. |
| 39 | + |
| 40 | +## Build |
| 41 | + |
| 42 | +To get started, we need to make sure the `.github/workflows` directory structure exists. |
| 43 | + |
| 44 | +```bash |
| 45 | +mkdir -p .github/workflows |
| 46 | +``` |
| 47 | + |
| 48 | +Then create the file `.github/workflows/build.yml` with the contents below. |
| 49 | +This workflow will be used in other workflows. |
| 50 | + |
| 51 | +```yaml |
| 52 | +name: Build application |
| 53 | + |
| 54 | +on: |
| 55 | + workflow_call: |
| 56 | + |
| 57 | +jobs: |
| 58 | + build: |
| 59 | + runs-on: ubuntu-latest |
| 60 | + container: quay.io/hypernode/deploy:3-php8.1-node18 |
| 61 | + steps: |
| 62 | + - uses: actions/checkout@v3 |
| 63 | + - uses: actions/cache@v3 |
| 64 | + with: |
| 65 | + path: /tmp/composer-cache |
| 66 | + key: ${{ runner.os }}-composer |
| 67 | + - uses: webfactory/ssh-agent@v0.7.0 |
| 68 | + with: |
| 69 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 70 | + - run: hypernode-deploy build -vvv |
| 71 | + - name: archive production artifacts |
| 72 | + uses: actions/upload-artifact@v3 |
| 73 | + with: |
| 74 | + name: deployment-build |
| 75 | + path: build/build.tgz |
| 76 | + retention-days: 7 |
| 77 | +``` |
| 78 | +
|
| 79 | +````{note} |
| 80 | +Don't forget to set the specifications of the image to what your project needs. The same goes for the deploy steps. |
| 81 | +For example, if your project needs PHP 7.4 and Node.js 16, set the image to: |
| 82 | +```yaml |
| 83 | +jobs: |
| 84 | + build: |
| 85 | + container: quay.io/hypernode/deploy:3-php7.4-node16 |
| 86 | + ... |
| 87 | +``` |
| 88 | +```` |
| 89 | +
|
| 90 | +## Deploy to production |
| 91 | +
|
| 92 | +Create a file called `.github/workflows/deploy_production.yml` with the following contents. |
| 93 | +
|
| 94 | +```yaml |
| 95 | +name: Deploy application to production |
| 96 | +
|
| 97 | +on: |
| 98 | + push: |
| 99 | + branches: |
| 100 | + - 'master' # production branch |
| 101 | +
|
| 102 | +jobs: |
| 103 | + build: |
| 104 | + uses: ./.github/workflows/build.yml |
| 105 | +
|
| 106 | + deploy: |
| 107 | + needs: build |
| 108 | + runs-on: ubuntu-latest |
| 109 | + # Concurrency takes any arbitrary value, but this prevents multiple deployments happening at the same time. |
| 110 | + # We set the concurrency to production for this deployment workflow. |
| 111 | + concurrency: production |
| 112 | + environment: |
| 113 | + name: production |
| 114 | + url: https://www.example.com |
| 115 | + container: quay.io/hypernode/deploy:3-php8.1-node18 |
| 116 | + steps: |
| 117 | + - uses: actions/checkout@v3 |
| 118 | + - name: download build artifact |
| 119 | + uses: actions/download-artifact@v3 |
| 120 | + with: |
| 121 | + name: deployment-build |
| 122 | + path: build/ |
| 123 | + - uses: actions/cache@v3 |
| 124 | + with: |
| 125 | + path: /tmp/composer-cache |
| 126 | + key: ${{ runner.os }}-composer |
| 127 | + - uses: webfactory/ssh-agent@v0.7.0 |
| 128 | + with: |
| 129 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 130 | + - run: mkdir -p $HOME/.ssh |
| 131 | + - run: hypernode-deploy deploy production -vvv # Deploy production stage defined in deploy.php |
| 132 | +``` |
| 133 | +
|
| 134 | +## Deploy to acceptance |
| 135 | +
|
| 136 | +Create a file called `.github/workflows/deploy_acceptance.yml` with the following contents. |
| 137 | +
|
| 138 | +```yaml |
| 139 | +name: Deploy application to acceptance |
| 140 | +
|
| 141 | +on: |
| 142 | + push: |
| 143 | + branches: |
| 144 | + - 'acceptance' # acceptance/staging branch |
| 145 | +
|
| 146 | +jobs: |
| 147 | + build: |
| 148 | + uses: ./.github/workflows/build.yml |
| 149 | +
|
| 150 | + deploy: |
| 151 | + needs: build |
| 152 | + runs-on: ubuntu-latest |
| 153 | + # Concurrency takes any arbitrary value, but this prevents multiple deployments happening at the same time. |
| 154 | + # We set the concurrency to acceptance for this deployment workflow. |
| 155 | + concurrency: acceptance |
| 156 | + environment: |
| 157 | + name: acceptance |
| 158 | + url: https://acceptance.example.com |
| 159 | + container: quay.io/hypernode/deploy:3-php8.1-node18 |
| 160 | + steps: |
| 161 | + - uses: actions/checkout@v3 |
| 162 | + - name: download build artifact |
| 163 | + uses: actions/download-artifact@v3 |
| 164 | + with: |
| 165 | + name: deployment-build |
| 166 | + path: build/ |
| 167 | + - uses: actions/cache@v3 |
| 168 | + with: |
| 169 | + path: /tmp/composer-cache |
| 170 | + key: ${{ runner.os }}-composer |
| 171 | + - uses: webfactory/ssh-agent@v0.7.0 |
| 172 | + with: |
| 173 | + ssh-private-key: ${{ secrets.SSH_PRIVATE_KEY }} |
| 174 | + - run: mkdir -p $HOME/.ssh |
| 175 | + - run: hypernode-deploy deploy acceptance -vvv # Deploy acceptance/staging stage defined in deploy.php |
| 176 | +``` |
0 commit comments