I have configured my github actions yml as shown below:
name: GZB Unit Tests
on:
push:
branches:
- main
pull_request:
branches: [main]
jobs:
frontend_unit_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 16.13.1
- name: Install Yarn
run: |
yarn
- name: Run Unit Tests in CI
run: |
yarn test:ci
- name: Build the Project
env:
NEXT_PUBLIC_FB_CLIENT_API_KEY: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_API_KEY }}
NEXT_PUBLIC_FB_CLIENT_AUTH_DOMAIN: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_AUTH_DOMAIN }}
NEXT_PUBLIC_FB_CLIENT_STORAGE_BUCKET: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_STORAGE_BUCKET }}
NEXT_PUBLIC_FB_CLIENT_MESSAGING_SENDER_ID: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_MESSAGING_SENDER_ID }}
NEXT_PUBLIC_FB_CLIENT_PROJECT_ID: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_PROJECT_ID }}
NEXT_PUBLIC_FB_CLIENT_APP_ID: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_APP_ID }}
NEXT_PUBLIC_FB_CLIENT_MEASUREMENT_ID: ${{ secrets.NEXT_PUBLIC_FB_CLIENT_MEASUREMENT_ID }}
FB_ADMIN_PRIVATE_KEY: ${{ secrets.FB_ADMIN_PRIVATE_KEY }}
FB_ADMIN_CLIENT_EMAIL: ${{ secrets.FB_ADMIN_CLIENT_EMAIL }}
FB_ADMIN_RTDB_URL: ${{ secrets.FB_ADMIN_RTDB_URL }}
NODE_ENV: ${{ secrets.NODE_ENV }}
run: |
yarn build
Now when I try to access it via: process.env.<VARIABLE_NAME> it says undefined. Can anyone help me understand what am I doing wrong? The build process is failing, should I configure it some other way? I have added all these secrets in the secrets tab of github settings.
NEXT uses its own web config to read files from .env.local. I was able to achieve my use case by creating a file .env.local and then passing in the secrets as shown below:
name: GZB Unit Tests
on:
push:
branches:
- main
pull_request:
branches: [main]
jobs:
frontend_unit_tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
with:
node-version: 16.13.1
- name: Install Yarn
run: |
yarn
- name: Run Unit Tests in CI
run: |
yarn test:ci
- name: Build the Project
run: |
touch .env.local
echo NEXT_PUBLIC_FB_CLIENT_API_KEY=${{ secrets.NEXT_PUBLIC_FB_CLIENT_API_KEY }} >> .env.local
echo NEXT_PUBLIC_FB_CLIENT_AUTH_DOMAIN=${{ secrets.NEXT_PUBLIC_FB_CLIENT_AUTH_DOMAIN }} >> .env.local
echo NEXT_PUBLIC_FB_CLIENT_STORAGE_BUCKET=${{ secrets.NEXT_PUBLIC_FB_CLIENT_STORAGE_BUCKET }} >> .env.local
echo NEXT_PUBLIC_FB_CLIENT_MESSAGING_SENDER_ID=${{ secrets.NEXT_PUBLIC_FB_CLIENT_MESSAGING_SENDER_ID }} >> .env.local
echo NEXT_PUBLIC_FB_CLIENT_PROJECT_ID=${{ secrets.NEXT_PUBLIC_FB_CLIENT_PROJECT_ID }} >> .env.local
echo NEXT_PUBLIC_FB_CLIENT_APP_ID=${{ secrets.NEXT_PUBLIC_FB_CLIENT_APP_ID }} >> .env.local
echo NEXT_PUBLIC_FB_CLIENT_MEASUREMENT_ID=${{ secrets.NEXT_PUBLIC_FB_CLIENT_MEASUREMENT_ID }} >> .env.local
echo FB_ADMIN_PRIVATE_KEY=${{ secrets.FB_ADMIN_PRIVATE_KEY }} >> .env.local
echo FB_ADMIN_CLIENT_EMAIL=${{ secrets.FB_ADMIN_CLIENT_EMAIL }} >> .env.local
echo FB_ADMIN_RTDB_URL=${{ secrets.FB_ADMIN_RTDB_URL }} >> .env.local
echo NODE_ENV=${{ secrets.NODE_ENV }} >> .env.local
yarn build
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 }}
I have a react app and I'm trying to set up ci/cd for this application.
I'm getting an error like this in GitHub actions and I get this error during "exporting to image"
name: "deploy-prod"
on:
push:
branches:
- "configure-ci/cd-pipeline-to-frontend-repo"
env:
REPOSITORY_NAME: ${{ github.event.repository.name }}
REACT_APP_BASENAME: "FooApp"
jobs:
build-backend:
runs-on: ubuntu-latest
steps:
- id: string
uses: ASzc/change-string-case-action#v1
with:
string: ${{ secrets.REGISTRY_HOST }}/${{ github.repository }}:build-${{ github.run_number }}
- name: Set up QEMU
uses: docker/setup-qemu-action#v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v2
- name: Login to Docker Hub
uses: docker/login-action#v2
with:
registry: ${{ secrets.REGISTRY_HOST }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- name: Build and push
uses: docker/build-push-action#v3
with:
push: true
tags: ${{ steps.string.outputs.lowercase }}
build-args: |
REACT_APP_BASENAME=${{ env.REACT_APP_BASENAME }}
NGINX_AUTH_USERNAME=${{ secrets.NGINX_AUTH_USERNAME }}
NGINX_AUTH_PASSWORD=${{ secrets.NGINX_AUTH_PASSWORD }}
deploy-backend:
runs-on: [ self-hosted ]
needs: [ build-backend ]
steps:
- id: string
uses: ASzc/change-string-case-action#v1
with:
string: ${{ secrets.REGISTRY_HOST }}/${{ github.repository }}:build-${{ github.run_number }}
- name: Login to Docker Hub
uses: docker/login-action#v2
with:
registry: ${{ secrets.REGISTRY_HOST }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}
- run: docker rm -f ${{ env.REPOSITORY_NAME }} 2> /dev/null
- run: >
docker run
--network=main
--detach
--restart unless-stopped
--name=${{ github.event.repository.name }}
${{ steps.string.outputs.lowercase }}
I have github-runner and docker-registry in my server. Docker registry is running on the server, I checked it
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)
We have set up Github actions for ci/cd pipelines, and it was working as expected a few days back. We use GitHub pr comments (/deploy staging) to deploy the code to the AWS S3 bucket and everything was working as expected, But now when we comment on pr(pull request), deployment does not trigger instead it triggers at midnight after 10 hours.
Here are my files.
workflows/deploy.yml
name: 'Deploy'
on: ['deployment']
jobs:
deployment:
name: 'Deploy to ${{ github.event.deployment.environment }}'
runs-on: ubuntu-latest
steps:
- name: Deployment pending
uses: deliverybot/deployment-status#master
with:
state: pending
token: '${{ github.token }}'
- name: Copy Repo Files
uses: actions/checkout#v1
- name: Set Variables
id: variables
run: |
if [ "$ENVIRONMENT" == "production" ]; then
echo ::set-output name=DEPLOY_BUCKET::$PROD_BUCKET
export PUBLIC_URL=https://app.virtualcombine.com/
else
echo ::set-output name=DEPLOY_BUCKET::$STAGING_BUCKET
export PUBLIC_URL=https://staging-app.virtualcombine.com/
fi
export VERSION="$(node -pe "require('./package.json').version")"
export COMMIT="$(sed -e 's/^\(.\{9\}\).*/\1/' <<< $(git rev-parse --short HEAD))"
echo ::set-output name=SOURCE_PATH::$VERSION/$COMMIT/$ENVIRONMENT
env:
ENVIRONMENT: ${{ github.event.deployment.environment }}
PROD_BUCKET: ${{ secrets.PROD_S3_UI_BUCKET }}
STAGING_BUCKET: ${{ secrets.STAGING_S3_UI_BUCKET }}
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials#v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2
- name: 'Deploy to ${{ github.event.deployment.environment }}'
run: |
aws s3 sync s3://${SOURCE_BUCKET_NAME}/${SOURCE_BUCKET_PATH} s3://${DEST_BUCKET_NAME} --delete
env:
SOURCE_BUCKET_NAME: ${{ secrets.AWS_S3_RELEASE_BUCKET }}
SOURCE_BUCKET_PATH: ${{ steps.variables.outputs.SOURCE_PATH }}
DEST_BUCKET_NAME: ${{ steps.variables.outputs.DEPLOY_BUCKET }}
- name: 'Deployment success'
if: success()
uses: 'deliverybot/deployment-status#master'
with:
state: 'success'
token: '${{ github.token }}'
- name: 'Deployment failure'
if: failure()
uses: 'deliverybot/deployment-status#master'
with:
state: 'failure'
token: '${{ github.token }}'
Workflows/mail.yml
name: Virtual Combine App UI
on:
push:
branches:
- 'release/'
- 'hotfix/'
jobs:
lint_test:
name: Affected Lint & Test
runs-on: ubuntu-latest
steps:
- name: Copy Repo Files
uses: actions/checkout#v1
- name: Yarn Install
uses: bahmutov/npm-install#v1
with:
useLockFile: true
- name: Setup Env Files
run: |
touch src/config/env-urls.js
echo "$ENV_URL_FILE" > src/config/env-urls.js
env:
ENV_URL_FILE: ${{ secrets.ALPHA_URL_FILE }}
- name: Lint Affected
run: yarn lint
- name: Test Affected
run: yarn test
build_staging:
name: Build Staging
needs:
- lint_test
runs-on: ubuntu-latest
steps:
- name: Copy Repo Files
uses: actions/checkout#v1
- name: Yarn Install
uses: bahmutov/npm-install#v1
with:
useLockFile: true
- name: Setup Env Files
run: |
touch src/config/env-urls.js
echo "$ENV_URL_FILE" > src/config/env-urls.js
env:
ENV_URL_FILE: ${{ secrets.ALPHA_URL_FILE }}
- name: Set Version
id: version
run: |
if [ "$EVENT" == "push" ]; then
export COMMIT="$(sed -e 's/^\(.\{9\}\).*/\1/' <<< "$SHA")"
else
export COMMIT="$(sed -e 's/^\(.\{9\}\).*/\1/' <<< "$PR_SHA")"
fi
export VERSION="$(node -pe "require('./package.json').version")"
echo ::set-output name=COMMIT::$COMMIT
echo ::set-output name=VERSION::$VERSION
echo ::set-output name=DEST_DIR::$VERSION/$COMMIT/staging
env:
EVENT: ${{ github.event_name }}
SHA: ${{ github.sha }}
PR_SHA: ${{ github.event.pull_request.head.sha }}
- name: Build
run: yarn build
env:
DEPLOY_COMMIT: ${{ steps.version.outputs.COMMIT }}
- name: Create Version File
run: |
echo "$VERSION/$COMMIT" > dist/apps/web/version.txt
env:
VERSION: ${{ steps.version.outputs.VERSION }}
COMMIT: ${{ steps.version.outputs.COMMIT }}
- name: Sync to S3
uses: jakejarvis/s3-sync-action#master
with:
args: --quiet --delete
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_RELEASE_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-west-2'
SOURCE_DIR: 'dist/apps/web'
DEST_DIR: ${{ steps.version.outputs.DEST_DIR }}
build_production:
name: Build Production
needs:
- lint_test
runs-on: ubuntu-latest
steps:
- name: Copy Repo Files
uses: actions/checkout#v1
- name: Yarn Install
uses: bahmutov/npm-install#v1
with:
useLockFile: true
- name: Setup Env Files
run: |
touch src/config/env-urls.js
echo "$ENV_URL_FILE" > src/config/env-urls.js
env:
ENV_URL_FILE: ${{ secrets.PROD_URL_FILE }}
- name: Set Version
id: version
run: |
if [ "$EVENT" == "push" ]; then
export COMMIT="$(sed -e 's/^\(.\{9\}\).*/\1/' <<< "$SHA")"
else
export COMMIT="$(sed -e 's/^\(.\{9\}\).*/\1/' <<< "$PR_SHA")"
fi
export VERSION="$(node -pe "require('./package.json').version")"
echo ::set-output name=VERSION::$VERSION
echo ::set-output name=COMMIT::$COMMIT
echo ::set-output name=DEST_DIR::$VERSION/$COMMIT/production
env:
EVENT: ${{ github.event_name }}
SHA: ${{ github.sha }}
PR_SHA: ${{ github.event.pull_request.head.sha }}
- name: Build
run: yarn build
env:
DEPLOY_COMMIT: ${{ steps.version.outputs.COMMIT }}
- name: Create Version File
run: |
echo "$VERSION/$COMMIT" > dist/apps/web/version.txt
env:
VERSION: ${{ steps.version.outputs.VERSION }}
COMMIT: ${{ steps.version.outputs.COMMIT }}
- name: Sync to S3
uses: jakejarvis/s3-sync-action#master
with:
args: --quiet --delete
env:
AWS_S3_BUCKET: ${{ secrets.AWS_S3_RELEASE_BUCKET }}
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: 'us-west-2'
SOURCE_DIR: 'dist/apps/web'
DEST_DIR: ${{ steps.version.outputs.DEST_DIR }}
deploy.yml
staging:
environment: staging
production_environment: true
production:
environment: production
production_environment: true
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') }}