Github Actions run terraform init in multiple directories - github-actions

I have 3 Terraform directories:
test_1
test_2
test_3
I would like to run terraform init in each of these directories. Is there a way to do this with a loop instead of specifying it 3 individual times?
I have the below code that runs it once inside only test_1 directory.
defaults:
run:
working-directory: test_1
jobs:
terraform-plan:
name: "Terraform"
runs-on: ubuntu-latest
steps:
- name: "Run - Terraform Init"
run: terraform init

Here's an idea.
defaults:
run:
working-directory: root_dir
jobs:
terraform-plan:
name: Terraform
runs-on: ubuntu-latest
steps:
- name: Run - Terraform Init
run: |
find -mindepth 1 -maxdepth 1 -type d -exec terraform init \;
Keep in mind this needs to be done in parent dir of those test dirs.

Related

GitHub Action Vitest CI pipeline

I would like to create a CI pipeline with GitHub Action but I have problems. The front end of the project is developed in Vue 3 and uses vitest to run the tests. It's in a folder called "WEB" in the root directory. So I would like to run my tests at each commit in the master branch but it returns this error:
Error: .github#L1
each step must define a `uses` or `run` key
I used the preexisting "Node.js" template and just added the "working-directory" property to specify the "WEB" folder.
Here is my ".yml" file
name: Runs frontend tests
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x]
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
working-directory: ./WEB
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- name: Execute Unit tests
- run: npm ci
- run: npm run test
When you add a - it add an element at the steps array:
You should do it like this:
- name: Execute Unit tests
run: |
npm ci
npm run test

actions/upload-pages-artifact fails at actions/upload-artifact with "No files were found with the provided path"

I would like to create a GitHub Workflow that builds a C++ application using emscripten and cmake, and deploys it to GitHub Pages. My Workflow job looks like this.
environment:
name: github-pages
url: ${{steps.deployment.outputs.page_url}}
runs-on: ubuntu-latest
container:
image: emscripten/emsdk
steps:
- uses: actions/checkout#v3
- run: cmake -B $GITHUB_WORKSPACE/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DEMSCRIPTEN=ON
- run: cmake --build $GITHUB_WORKSPACE/build --config ${{env.BUILD_TYPE}}
# actions/upload-pages-artifact uses this directory, but it doesn't exist in the image
- run: mkdir -p ${{runner.temp}}
- uses: actions/configure-pages#v1
- uses: actions/upload-pages-artifact#v1
with:
path: $GITHUB_WORKSPACE/build
- id: deployment
uses: actions/deploy-pages#v1
upload-pages-artifact runs tar and lists all the files to be deployed in the log. When running upload-artifact the log reads Warning: No files were found with the provided path: /__w/_temp/artifact.tar. No artifacts will be uploaded..
Note that the path in the warning is different from the one provided as a parameter to upload-artifact (path: /home/runner/work/_temp/artifact.tar).
upload-pages-artifact works as expected when running without the emscripten container.
I would have to either get upload-pages-artifact working inside the container, or somehow share the build with a second job running outside the container.
Split up the job into two jobs, one for building and one for deploying. Use actions/upload-artifact and actions/download-artifact to pass the build from one job to the next. Don't use $GITHUB_WORKSPACE, as it might not point to the right directory in your image.
jobs:
build:
runs-on: ubuntu-latest
container:
image: emscripten/emsdk
steps:
- uses: actions/checkout#v3
- run: cmake -B build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DEMSCRIPTEN=ON
- run: cmake --build build --config ${{env.BUILD_TYPE}}
- uses: actions/upload-artifact#master
with:
name: page
path: build
if-no-files-found: error
deploy:
runs-on: ubuntu-latest
needs: build
environment:
name: github-pages
url: ${{steps.deployment.outputs.page_url}}
steps:
- uses: actions/download-artifact#master
with:
name: page
path: .
- uses: actions/configure-pages#v1
- uses: actions/upload-pages-artifact#v1
with:
path: .
- id: deployment
uses: actions/deploy-pages#main

Running Github actions in another directory

I would like to execute a github action in a sub-directory of my project because my Github repo contains 2 applications (front/back) and I need to test the front. (see below)
my-project
├─ api/
│ └─ ...
└─ front-app/
└─ (node-js application I want to run the tests for)
I want to execute only the tests for the front-app running on svelte (node-js)
The tests fails because they run in the project directory instead of the front-app/ one.
However I already implemented :
# Just after `on: push / pull_request`
defaults:
run:
working-directory: ./front-app/
# And before `jobs`
And I still get the error :
Run actions/setup-node#v3
Found in cache # /opt/hostedtoolcache/node/16.16.0/x64
/opt/hostedtoolcache/node/16.16.0/x64/bin/npm config get cache
/home/runner/.npm
Error: Dependencies lock file is not found in /home/runner/work/my-project/my-project. Supported file patterns: package-lock.json,npm-shrinkwrap.json,yarn.lock
The file is define as :
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: Tests for front-app
on:
push:
branches: [ "main" ]
paths: ["front-app/**"]
pull_request:
branches: [ "main" ]
paths: ["front-app/**"]
defaults:
run:
working-directory: ./front-app/
jobs:
test-front-app:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [16.x, 18.x]
steps:
- uses: actions/checkout#v3
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3 # <-- I think the error occurs here
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci # <-- This is not executed - The error occurs before
- run: npm run build --if-present
- run: npm test
So, I found the solution in the documentation here by adding cache-dependency-path: with the path of the package-lock.json (see below)
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node#v3
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
cache-dependency-path: 'front-app/package-lock.json'

Github Composite action cannot find file

I have created an action that runs the following command
- name: Create Release Notes File
env:
COMMITTEXT: "${{ github.event.head_commit.message }}"
run: |
php ./create-release-notes-file.php
The file is in the same folder as the create-release.yml which is .github/workflows
when the action runs it fails with the following error:
Run php ./create-release-notes-file.php
Could not open input file: ./create-release-notes-file.php
Error: Process completed with exit code 1.
I can't find any documentation that tells me to put the files anywhere else. Any suggestions welcomed
Be sure to have cloned the repo before the above step (like with the actions/checkout#v3), as example:
jobs:
create-release-note:
name: Create Release note
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v3
with:
fetch-depth: 0
persist-credentials: false
token: ${{ secrets.GH_API_TOKEN }}
- name: Create Release Notes File
env:
COMMITTEXT: "${{ github.event.head_commit.message }}"
run: |
php ./create-release-notes-file.php

How to build, run and call docker container in Github Action

I need to build docker image form the source code of the current repository, run a container, then execute some API calls. How to do that with the github action?
name: Docs Generator
on:
pull_request:
types: [opened]
jobs:
pr-labeler:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
with:
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Get the version
id: vars
run: echo ::set-output name=tag::$(echo ${GITHUB_REF:10})
- name: Build the tagged Docker image
run: docker build . --file Dockerfile --tag service:${{steps.vars.outputs.tag}}
- name: Run docker image
docker run -v ${{ inputs.path }}:/src service:${{steps.vars.outputs.tag}}
- name: Call API
run: |
curl +x http://localhost:8080/test
.....
For this purpose, you could use a combination of https://github.com/marketplace/actions/build-and-push-docker-images and https://github.com/addnab/docker-run-action
The first would build and publish a container, and the second would take this container and run your commands there.
The example is below. I don't use this setup myself but I have tested it. Replace username/container with your username and container.
name: Docker Image CI
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
compile:
name: Build and run the container
runs-on: ubuntu-latest
steps:
- name: Set up QEMU
uses: docker/setup-qemu-action#v1
- name: Set up Docker Buildx
uses: docker/setup-buildx-action#v1
- name: Login to DockerHub
uses: docker/login-action#v1
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build and push
uses: docker/build-push-action#v2
with:
push: true
tags: username/container
- name: Check out the repo
uses: actions/checkout#v2
- name: Run the build process with Docker
uses: addnab/docker-run-action#v3
with:
image: username/container:latest
options: -v ${{ github.workspace }}:/data
run: |
echo "Hello World"
Note that building a container is quite a long task and might deplete your Github Action limits quickly. You might consider building/publishing a container separately, or add better caching here (i.e. to rebuild it only on Dockerfile change)
Note that you need to set up DOCKERHUB_USERNAME and DOCKERHUB_TOKEN secrets.
Instead of echo "Hello World", use the commands you want to run. The repo data will be in the /data directory, for this setup.