I have a react app with 2 environemnts in netlify. Until now, I was managing the Continuous deployments using 2 branches but sometimes it gets very messy when doing hotfixes. I would like to set up a Continuous Deployment system where it will deploy on git tags on the same branch instead of having different branches. I think it's the best approach as the code will always be common if it's the same branch.
After some research, I have found that it could be done using github actions (we are already using this for git) and the netlify cli to build based on tags.
From the documentation it seems I should use:
on: push
name: Publish on Netlify
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Publish
uses: netlify/actions/build@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_BASE: site
NETLIFY_CMD: npm build
NETLIFY_DIR: site/_build
This comes from https://github.com/netlify/actions/tree/master/build
The code for working with the tags in github actions is:
on:
push:
tags:
- '*'
It was posted on this article: https://github.community/t5/GitHub-Actions/How-to-run-GitHub-Actions-Workflow-only-for-new-tags/td-p/29413#
I understand that it should be like this:
on:
push:
tags:
- '*'
name: Publish on Netlify
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@master
- name: Publish
uses: netlify/actions/build@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
NETLIFY_BASE: site
NETLIFY_CMD: npm build
NETLIFY_DIR: site/_build
But it's not working. Any ideas?
Hey as zzarbi has commented, it's not clear what troubles you are facing, so I can just give an example what works for me.
# Name of workflow
name: Production workflow
# When workflow is triggered
on:
push:
tags:
- "v*"
# Jobs to carry out
jobs:
deploy:
# Operating system to run job on
runs-on: ubuntu-latest
# Steps in job
steps:
# Get code from repo
- name: Checkout code
uses: actions/checkout@v1
# Install NodeJS
- name: Use Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: 12.x
# Run npm install and build on our code
- run: npm install
- run: npm run build --if-present
# Deploy to Netlify using our production secrets
- name: Deploy to netlify
uses: netlify/actions/cli@master
env:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.NETLIFY_SITE_ID }}
with:
args: deploy --dir=build --prod
secrets: '["NETLIFY_AUTH_TOKEN", "NETLIFY_SITE_ID"]'
NETLIFY_AUTH_TOKEN
NETLIFY_SITE_ID
(this can be changed for example to NETLIFY_SITE_ID_DEV
just make sure to change it in the production.yml as wellNote: You might need to disable auto deploy process such as on pull requests in netlify
With that setup, each new tag will run the GitHub action.
Tip: I use the standard-version package for release management (https://www.npmjs.com/package/standard-version)
Credits: I could gain all of the ideas from https://dev.to/curtiscodes/ci-cd-pipeline-with-netlify-and-github-actions-bcm
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With