So I have the following workflow and its working perfectly. I now want to enhance it and when I am doing a PR to master, I want to set NETLIFY_DEPLOY_TO_PROD: false instead of it being true? Do I have to duplicate this all in a new workflow, or could do some inline if check of github.event_name === push ? true : false
name: 'Netlify Deploy'
on:
push:
branches: [ master ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "${{ github.event.head_commit.message }}"
NETLIFY_DEPLOY_TO_PROD: true
You could set an environment variable to indicate if deploy to prod should happen, and change it depending on the event name:
name: Netlify Deploy
on:
push:
branches:
- master
pull_request:
branches:
- master
env:
DEPLOY: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Deploy on pushes
if: github.event_name == 'push'
run: echo 'DEPLOY=true' >> "$GITHUB_ENV"
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: ${{ github.event.head_commit.message }}
NETLIFY_DEPLOY_TO_PROD: ${{ env.DEPLOY }}
You want to use github action expressions for this as it's quicker and you don't need any other unnecessary steps. I would only use steps to run scripts when they are more complex in nature
Reference: https://docs.github.com/en/actions/reference/context-and-expression-syntax-for-github-actions
Example 1: Trigger on push
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "${{ github.event.head_commit.message }}"
NETLIFY_DEPLOY_TO_PROD: ${{ github.event_name == 'push' }}
Example 2 & Solution: Trigger on push and branch is master
NOTE: You only need to check for branch master if you are planning to let this workflow run on other branches. Otherwise you can just use example 1 above that sets variable to true if event name is push only.
- uses: jsmrcaga/action-netlify-deploy#master
with:
NETLIFY_AUTH_TOKEN: ${{ secrets.NETLIFY_AUTH_TOKEN }}
NETLIFY_SITE_ID: ${{ secrets.SITE_ID }}
NETLIFY_DEPLOY_MESSAGE: "${{ github.event.head_commit.message }}"
NETLIFY_DEPLOY_TO_PROD: ${{ github.event_name == 'push' && contains(github.ref, 'master') }}
Related
I'm currently trying to allow tags only on the main branch. But I can't seem to get it to work. This is what I currently have but it also triggers the production build if a tag is being put on a develop/release/hotfix branch.. Is there a way to check if the tag has been created on the main branch?
Current YAML:
name: Release to production
on:
push:
branches:
- 'hotfix/**'
tags:
- 'v*'
jobs:
get-version-data:
runs-on: ubuntu-latest
steps:
- name: Get production version from tag
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
id: get_version
run: echo "VERSION=$(echo ${{ github.ref }} | cut -d '/' -f 3 | cut -c2-)" >> $GITHUB_ENV
- name: Get hotfix version from tag
if: ${{ contains(github.ref, 'hotfix') }}
id: split
uses: jungwinter/split#v2
with:
separator: '/'
msg: ${{ github.ref }}
- name: Write hotfix version to env
if: ${{ contains(github.ref, 'hotfix') }}
run: echo "VERSION=${{ steps.split.outputs._3 }}" >> $GITHUB_ENV
- name: Exit if version could not be determined
if: ${{ env.VERSION == '' }}
run: exit -1
- name: Build Number
run: echo "BUILD_NUMBER=$(($GITHUB_RUN_NUMBER + 125))" >> $GITHUB_ENV
outputs:
version: ${{ env.VERSION }}
build-number: ${{ env.BUILD_NUMBER }}
So this is a weird one... I am trying to implement a CODEFREEZE option in release pipelines so I can implement a global freeze to any release with an organization secret:
name: test code freeze
on:
push:
jobs:
test:
runs-on: ubuntu-latest
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
steps:
- name: test
if: ${{ env.CODEFREEZE }} == "true"
run: echo "code is frozen"
- name: test unfreeze
if: ${{ env.CODEFREEZE }} == "false"
run: echo "code is NOT frozen"
For some reason, both of these run. I've tried setting the secret to a number of different values. I've tried using quotes and not using quotes, but nothing I do seems to have an effect. Am I missing something extremely obvious?
This does appear to work but I don't understand why:
name: test code freeze
on:
push:
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: test
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
if: ${{ env.CODEFREEZE == 'true' }}
run: echo "code is frozen"
- name: test unfreeze
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
if: ${{ env.CODEFREEZE == 'false' }}
run: echo "code is NOT frozen"
This also appears to work:
name: test code freeze
on:
push:
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: test
if: ${{ env.CODEFREEZE == 'true' }}
run: echo "code is frozen"
- name: test unfreeze
if: ${{ env.CODEFREEZE == 'false' }}
run: echo "code is NOT frozen"
so the problem only exists when you set the env on the job level
Nevermind... I clearly have not had enough coffee... I found the syntax error:
name: test code freeze
on:
push:
jobs:
test:
runs-on: ubuntu-latest
env:
CODEFREEZE: ${{ secrets.CODEFREEZE }}
steps:
- name: test
if: ${{ env.CODEFREEZE == 'true' }}
run: echo "code is frozen"
- name: test unfreeze
if: ${{ env.CODEFREEZE == 'false' }}
run: echo "code is NOT frozen"
It requires single quotes in the evaluation and the eval has to be inside the curly braces...
I have a github action yml file that i'm having issues in 2 areas first is the newTag is no longer working when I broke everything out into separate job names.
These lines no longer work. I get release-v instead of the actual bumped version number.
tag_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
release_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
Second Issue: The if statement for docker build
if: github.ref == 'ref/head/release' || contains(github.ref, '/tags/release')
The above if does not work. If I add a branch called release/test or using a tag name release it does not run
name: Publish
on:
push:
branches:
- main
- release/*
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-image
TAG_PREXIX: release-v
jobs:
Publish:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout#v3
- name: "Bump package version"
id: bumpVersion
uses: "phips28/gh-action-bump-version#master"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PACKAGEJSON_DIR: "./client"
tag-prefix: ${{env.TAG_PREXIX}}
major-wording: "MAJOR,BREAKING CHANGE:"
minor-wording: "feat"
patch-wording: "patch,fix,bugfix,chore"
Build-Docker-Image:
runs-on: ubuntu-latest
needs: Publish
if: github.ref == 'ref/head/release' || contains(github.ref, '/tags/release')
steps:
- name: Log into Container registry ${{ env.REGISTRY }}
uses: docker/login-action#v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push Docker image
uses: docker/build-push-action#v3
with:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{env.TAG_PREXIX}}${{steps.bumpVersion.outputs.newTag}}
Release:
runs-on: ubuntu-latest
needs: Publish
steps:
- name: Create Release
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
release_name: ${{env.TAG_PREXIX}}${{ steps.bumpVersion.outputs.newTag }}
I would like to setup a workflow in github yml such that I have some default values for variables and also would like to be able to manually provide the values to these variables when running the workflow manually.
I understood that we can use workflow_dispatch to set some input variables when running manually. However, when the workflow is executed as part of a code push, these variables (runTests and uploadArtifacts) are coming as null.
name: Example
on:
workflow_dispatch:
inputs:
runTests:
description: run tests
required: true
default: true
type: Boolean
uploadArtifacts:
description: upload artifacts
required: true
default: false
type: Boolean
push:
branches:
- master
- main
- release/*
jobs:
Build_Job:
runs-on: [self-hosted, raya]
steps:
- name: Publish drop artifact
if: ${{ inputs.uploadArtifacts }}
uses: actions/upload-artifact#v2
with:
name: Installer
path: "${{ runner.temp }}/AppxPackages/"
It's the expected behavior, as the inputs will be set only if the workflow_dispacth event is used to trigger the workflow.
If you want the workflow to perform a default operation when the code is pushed, you would need to implement the if condition differently.
Example:
on:
push:
workflow_dispatch:
inputs:
test1:
description: test1
required: false
default: false
type: boolean
test2:
description: test2
required: false
default: true
type: boolean
jobs:
job1: # will always run
runs-on: ubuntu-latest
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job2: # will only run on a workflow_dispatch event, if test1 input is true
runs-on: ubuntu-latest
if: ${{ inputs.test1 }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job3: # will only run on a workflow_dispatch event, if test2 input is true
runs-on: ubuntu-latest
if: ${{ inputs.test2 }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job4: # will only run on a push event
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
job5: # will only run on a push event OR if inputs.test2 is true on a workflow_dispatch event
runs-on: ubuntu-latest
if: ${{ github.event_name == 'push' || inputs.test2 }}
steps:
- run: |
echo ${{ inputs.test1 }}
echo ${{ inputs.test2 }}
echo ${{ github.event_name }}
I understand that what you want to achieve is something similar to the job5 example above (you could even add a github.ref context variable to the expression if you only want a job to be executed if the branch name is something specific).
I made some tests if you want to have a look:
workflow file
workflow run (push event)
workflow run (workflow_dispatch event with default value)
I am preety sure its not possible but still wanna try my luck. I have deploy-dev step and smoke-test-dev. So if smoke test for dev fails then I want to fail the last step which is deploy-dev.
deploy-dev:
if: ${{ github.event.workflow_run.conclusion == 'skipped' && github.actor == 'dependabot[bot]' }}
runs-on: ubuntu-latest
needs: [lint, test]
steps:
- uses: actions/checkout#v2
- name: Start deployment
uses: bobheadxi/deployments#v0.4.3
id: deployment
with:
step: start
token: ${{ secrets.GITHUB_TOKEN }}
env: dev
- name: Update deployment status
uses: bobheadxi/deployments#v0.4.3
if: always()
with:
step: finish
token: ${{ secrets.GITHUB_TOKEN }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
smoke-tests-dev:
if: ${{ github.event.workflow_run.conclusion == 'skipped' && github.actor == 'dependabot[bot]' }}
runs-on: ubuntu-latest
needs: deploy-dev
steps:
- uses: actions/checkout#v2
- name: Checkout GitHub Action Repo
uses: actions/checkout#v2
with:
repository: my/github-actions
ref: ${{ env.COMMON_ACTIONS_VERSION }}
token: ${{ secrets.REPO_READ_TOKEN }}
path: ${{ env.COMMON_ACTIONS_PATH }}
- name: install and smoke test
uses: ./.github/common/actions/yarn
with:
npm_token: ${{ env.NPM_TOKEN }}
env: dev
action: test:smoke