How to access GitHub variables from a custom action's dockerfile? - github

I have this workflow yaml file:
name: PHP code review
on: push
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: PHPUnit
uses: ./.github/actions/phpunit
This is the .github/actions/phpunit/action.yml file:
name: PHPUnit in Docker
description: Run PHPUnit in a custom configured Docker container
runs:
using: 'docker'
image: phpunit.dockerfile
And this is the Dockerfile:
FROM ubuntu
# Update aptitude with new repo
RUN apt-get update
# Install software
RUN apt-get install -y git
# Clone the conf files into the docker container
RUN git clone https://${{ secrets.PHPUNIT_ACCESS_TOKEN }}#github.com/${{ GITHUB_REPOSITORY }}
But in the Dockerfile it looks like a string, and not a variable.
How can I access to GitHub variables in my Dockerfile?

Related

Github Actions - Invalid workflow file

I am trying to build CI/CD pipelines using GitHub Actions but unfortunately, I am stuck with an error with the yaml file.
Here is my Yaml file is:
---
name: Build and push python code to gcp with github actions
on:
push:
branches:
- main
jobs:
build_push_grc:
name: Build and push to gcr
runs_on: unbuntu-latest
env:
IMAGE_NAME: learning_cicd
PROJECT_ID: personal-370316
steps:
- name: Checkoutstep
uses: actions/checkout#v2
- uses: google-github-actions/setup-gcloud#master
with:
service_account_key: ${{ secrets.SERVICE_ACCOUNT_KEY}}
project_id: ${{ env.PROJECT_ID }}
export_default_credentials: true
- name: Build Docker Image
run: docker build -t $IMAGE_NAME:latest .
- name: Configure Docker Client
run: |-
gcloud auth configure-docker --quiet
- name: Push Docker Image to Container Registry (GCR)
env:
GIT_TAG: v0.1.0
run: |-
docker tag $IMAGE_NAME:latest gcr.io/$PROJECT_ID/$IMAGE_NAME:latest
docker tag $IMAGE_NAME:latest gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG
docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:latest
docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG
Here is an error where I am stuck with:
GitHub Actions
/ .github/workflows/gcp.yaml
Invalid workflow file
You have an error in your yaml syntax on line 15
I tried all possible indentations available on the internet but had no luck. I tried Yamllinter but still could not find where the error comes from. Please point me to where I am going wrong.
Thanks.
The runs-on (not runs_on) should have two spaces indentation relative to the job identifier. Also, the OS should be ubuntu-latest.
Then, env should have the same indentation as runs-on or name, the same as steps.
Here is the correct WF:
---
name: Build and push python code to gcp with github actions
on:
push:
branches:
- main
jobs:
build_push_grc:
name: Build and push to gcr
runs-on: ubuntu-latest
env:
IMAGE_NAME: learning_cicd
PROJECT_ID: personal-370316
steps:
- name: Checkoutstep
uses: actions/checkout#v2
- uses: google-github-actions/setup-gcloud#master
with:
service_account_key: ${{ secrets.SERVICE_ACCOUNT_KEY}}
project_id: ${{ env.PROJECT_ID }}
export_default_credentials: true
- name: Build Docker Image
run: docker build -t $IMAGE_NAME:latest .
- name: Configure Docker Client
run: |-
gcloud auth configure-docker --quiet
- name: Push Docker Image to Container Registry (GCR)
env:
GIT_TAG: v0.1.0
run: |-
docker tag $IMAGE_NAME:latest gcr.io/$PROJECT_ID/$IMAGE_NAME:latest
docker tag $IMAGE_NAME:latest gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG
docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:latest
docker push gcr.io/$PROJECT_ID/$IMAGE_NAME:$GIT_TAG
I would recommend debugging such issues in the GitHub file edit form (editing the yml file in the .github/workflows directory). It will highlight all the issues regarding the workflow syntax. Demo.

why I can run docker-compose in Github action without install it

I make a copy/paste from the link here. I am confused about why I can run docker-compose without installing it. When I test locally using "act", it will give me an error about docker-compose command not found.
name: CI-dev-pipeline
on:
push:
branches: [ develop ]
pull_request:
branches: [ develop ]
jobs:
build:
runs-on: ubuntu-latest
container:
image: lucasalt/act_base:latest
steps:
- uses: actions/checkout#v2
with:
path: "<INSERT_NAME_OF_REPOSITORY>"
- name: Build docker
run: |
docker-compose -f ./<REPO_NAME>/docker-compose.test.yml -p testing up -d
You are using a docker-image from "lucasalt/act_base".
If you look at the description of the images it states:
act_base is a custom runner for the act project. This image contain Node, npm, yarn, docker and docker-compose.
Meaning that it already downloads docker-compose etc. for you.

Automatically setting the release tag on a GitHub workflow

I am trying to build an action that is triggered on creating a new release on GitHub which works fine, but I would like to reference the tag in my action:
name: Build production container
on:
release:
types:
- created
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Build the Docker image
run: |
echo "${{ SECRET }}" | docker login -u ME --password-stdin docker.pkg.github.com
docker build app/ -t docker.pkg.github.com/REPO_PATH/image:$VERSION
docker push docker.pkg.github.com/REPO_PATH/image:$VERSION
shell: bash
env:
VERSION: 0.0.1
This is my working action, but I would like to automatically pull the tag into the VERSION environment variable. I read the documentation, especially here where the GitHub context is referenced, but I can't seem to find anything about it.
It took me a while to figure out that the action has a different context for each method documented here. So the parameter I was looking for is the and I've set my action up after this example:
name: Build production container
on:
release:
types:
- created
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Get Tag Name
id: tag_name
run: |
echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}
- name: Build the Docker image
run: |
echo "${{ SECRET }}" | docker login -u ME --password-stdin docker.pkg.github.com
docker build app/ -t docker.pkg.github.com/REPO_PATH/image:$VERSION
docker push docker.pkg.github.com/REPO_PATH/image:$VERSION
shell: bash
env:
VERSION: ${{ steps.tag_name.outputs.SOURCE_TAG }}
This basically adds getting the source parameter as an extra step, this way I can use it in the environment variables of the next step.

Github CI/CD pipeline crashes with webpack

I'm trying to learn how CI/CD pipelines work.
I decided to use it with my portfolio page which should re-run itself on every push.
Here is my yaml config:
name: Build Bundle for Github Pages
on:
push:
branches:
- source
env:
NODE_ENV: production
PUBLIC_URL: http://crrmacarse.github.io/
GA_TRACKING_CODE: ${{ secrets.GA_TRACKING_CODE }}
FIREBASE_TOKEN: ${{ secrets.FIREBASE_TOKEN }}
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v1
with:
persist-credentials: false
- name: Build
run: |
npm install
npm run prod:pipeline
npm run sitemap
cp dist/index.html dist/404.html
cp google21029c74dc702d92.html dist/
cp robots.txt dist/
- name: Deploy
uses: JamesIves/github-pages-deploy-action#releases/v3
with:
ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }}
BRANCH: source
FOLDER: dist
Here is the error:
Source code for the webpack config:
https://github.com/crrmacarse/crrmacarse.github.io/blob/source/compiler/production.pipeline.js
If the error message is cannot find module 'html-webpack-plugin', you could try, for testing, to install it.
See survivejs/webpack-book issue 100 as an example:
The solution was to run npm i html-webpack-plugin --save-dev before building with webpack
The OP has fixed the GitHub Action workflow with crrmacarse/crrmacarse.github.io commit 8a4397b
jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2.0.0
- name: Use node 12
uses: actions/setup-node#v1
with:
node-version: 12
registry-url: https://registry.npmjs.org
- name: install
run: npm install
- name: lint
run: npm run sitemap
- name: build
run: npm run prod
- name: copy
run: npm run copy
Then the module html-webpack-plugin is properly installed and available, as seen in this Actions CI run.
Use npm install before build your project which installs your npm library on docker where your project will be built.
Note: Don't forget to define html-webpack-plugin on packge.json file

Running actions in another directory

I've just started exploring Github actions however I've found myself placing a command in multiple places.
I have a PHP project where the composer.json is not in the root, my structure looks like:
my-project:
readme.md
app:
composer.json
Obviously there is more to it and there is a reason why, but my composer.json sits in a subdirectory called 'app'. As a result in my workflow, I have to cd into that folder every time to run a command:
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup Symfony
run: |
cd app
cp .env.dev .env
- name: Install Composer Dependencies
run: |
cd app
composer install --prefer-dist
- name: Run Tests
run: |
cd app
php bin/phpunit
How can I remove the cd app in every stage?
Update: It's now possible to set a working-directory default for a job. See this answer.
There is an option to set a working-directory on a step, but not for multiple steps or a whole job. I'm fairly sure this option only works for script steps, not action steps with uses.
https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstepsrun
Using working-directory, your workflow would look like this. It's still quite verbose but maybe a bit cleaner.
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup Symfony
working-directory: ./app
run: cp .env.dev .env
- name: Install Composer Dependencies
working-directory: ./app
run: composer install --prefer-dist
- name: Run Tests
working-directory: ./app
run: php bin/phpunit
Alternatively, you can run it all in one step so that you only need to specify working-directory once.
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v1
- name: Setup and run tests
working-directory: ./app
run: |
cp .env.dev .env
composer install --prefer-dist
php bin/phpunit
You can now add a default working directory for all steps in a job: docs
For the example here, this would be:
name: CI
on: [push]
jobs:
phpunit:
runs-on: ubuntu-latest
defaults:
run:
working-directory: ./app
steps:
- uses: actions/checkout#v1
- name: Setup Symfony
run: .env.dev .env
- name: Install Composer Dependencies
run: composer install --prefer-dist
- name: Run Tests
run: php bin/phpunit
Caveat: this only applies to run steps; eg you'll still need to add the subdirectory to with parameters of uses steps, if required.
Hope this will help somebody
name: CI
on:
...
defaults:
run:
working-directory: ./app
jobs:
...