I'm trying to print content of the current working directory $PWD or $GITHUB_WORKSPACE with my debug job. My expectation is to see the directory's content. Unfortunately, it returns no content.
Results
Run ls -la $PWD
total 8
drwxr-xr-x 2 runner docker 4096 Apr 19 19:42 .
drwxr-xr-x 3 runner docker 4096 Apr 19 19:42 ..
total 8
drwxr-xr-x 2 runner docker 4096 Apr 19 19:42 .
drwxr-xr-x 3 runner docker 4096 Apr 19 19:42 ..
.github/workflows/debug.yaml
name: Debug
on: [push, workflow_dispatch]
jobs:
debug:
runs-on: ubuntu-latest
steps:
- name: Print current working dir
run: |
ls -la $PWD
ls -la $GITHUB_WORKSPACE
I figured it out. Please add uses: actions/checkout#v2 as the first step in your workflow.
name: Debug
on: [push, workflow_dispatch]
jobs:
debug:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- name: Print current working dir
run: |
ls -la $PWD
ls -la $GITHUB_WORKSPACE
Related
I'm having issues using mysql as the database for my tests that are running in a Github action. I'm using this as a guide.
I'm getting the following error:
SQLSTATE[HY000] [1045] Access denied for user 'root'#'172.18.0.2' (using password: NO) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
here is my yaml file:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-latest
container:
image: kirschbaumdevelopment/laravel-test-runner:8.1
services:
testdb:
image: mysql:5.7
env:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test
MYSQL_ALLOW_EMPTY_PASSWORD: 1
ports:
- 33306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
steps:
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-latest
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET
UPDATE
I removed this line from my phpunit.xml file:
<env name="DB_HOST" value="testdb"/>
and now I'm getting a different error:
SQLSTATE[HY000] [2002] Connection refused (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
After doing some more googling, I found this post which made me go back and pull out the docker container and try to do it using the mysql service that's already available on ubuntu.
Once I was able to successfully start the service and create a database, I then realized that the steps in my action were copying the .env file, and if it wasn't there, it was copying the .env.example file, which I overlooked, and was getting odd results. Once I realized that it was looking in that file for database connection values, I was able to override them with the env option in the yaml file. So, I finally got it working, and this is my working yaml file for anyone that may run into this at some point:
name: LaravelTest
on:
push:
branches: [ test ]
jobs:
laravel_tests:
runs-on: ubuntu-20.04
env:
DB_CONNECTION: mysql
DB_HOST: localhost
DB_PORT: 3306
DB_DATABASE: testdb
DB_USERNAME: root
DB_PASSWORD: root
steps:
- name: Set up MySQL
run: |
sudo systemctl start mysql
mysql -e 'CREATE DATABASE testdb;' -uroot -proot
mysql -e 'SHOW DATABASES;' -uroot -proot
- uses: actions/checkout#main
- name: Copy .env
run: php -r "file_exists('.env') || copy('.env.example', '.env');"
- name: Install Dependencies
run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress
- name: Generate key
run: php artisan key:generate
- name: Directory Permissions
run: chmod -R 777 storage bootstrap/cache
- name: Install dependencies
run: npm install
- name: Compile assets
run: npm run dev
- name: Execute tests (Unit and Feature tests) via PHPUnit
run: vendor/bin/phpunit
forge_deploy:
runs-on: ubuntu-20.04
needs: laravel_tests
steps:
- name: Make Get Request
uses: satak/webrequest-action#master
with:
url: ${{ secrets.MOMENTUM_TEST_DEPLOY_URL }}
method: GET
I'm trying to pull a git submodule during my 'push' github action. The submodule is a private repository.
I've created a PAT with read-only access to my repositories. And I've added the contents of that PAT as a secret in the git repo (not the submodule).
This is my github action file:
name: Java CI
on: [ push ]
env:
SUBMODULE_ACCESS: ${{ secrets.SUBMODULE_ACCESS }}
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Update submodules
run: |
git config --global url."https://${SUBMODULE_ACCESS}:x-oauth-basic#github.com/".insteadOf "https://github.com/"
git submodule update --init --recursive
- name: Set up JDK 19
uses: actions/setup-java#v2
with:
java-version: '19'
distribution: 'adopt'
- name: Cache local .m2
uses: actions/cache#v2
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**.pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- name: Run lints and test with Maven
run: mvn clean install -Dall
When I run this, I see the following error:
remote: Support for password authentication was removed on August 13, 2021.
remote: Please see https://docs.github.com/en/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended modes of authentication.
But that link just mentions PATs, which I think I've set up?
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.
I'm trying to clone a repository from GitHub to a remote server.
My solution using appleboy/ssh-action GitHub action was working but I was told the same can be achieved using actions/checkout#v2 GitHub action.
I tried to just change - uses: value to actions/checkout#V2` but then the code doesn't work.
I can't find any templates on how to do it using actions/checkout#v2. Any advice would be much appreciated.
name: deploy to a server on push
on:
push:
branches: [ master ]
jobs:
deploy-to-server:
runs-on: ubuntu-latest
steps:
- uses: appleboy/ssh-action#master
with:
host: 123.132.123.132
username: tomas
key: ${{ secrets.PRIVATE_KEY }}
port: 59666
script:
git clone https://github.com/Tomas-R/website.git
As the documentation of actions/checkout#v2 says
This action checks-out your repository under $GITHUB_WORKSPACE, so your workflow can access it.
steps:
- name: Checkout the repo
uses: actions/checkout#v2
with:
# This will create a directory named `my-repo` and copy the repo contents to it
# so that you can easily upload it to your remote server
path: my-repo
To copy this checked-out repo to a remote server, you may use scp command as follows.
# Runs a set of commands using the runners shell
- name: Upload repo to remote server
env:
SSH_AUTH_SOCK: /tmp/ssh_agent.sock
run: |
ssh-agent -a $SSH_AUTH_SOCK > /dev/null
ssh-add - <<< "${{ secrets.PRIVATE_KEY }}"
scp -o StrictHostKeyChecking=no -r -P 59666 my-repo tomas#123.132.123.132:/target/directory
By using the above commands we,
Start ssh-agent and bind it to a known location.
Import the private key from the secret to the ssh-agent.
Copy contents from my-repo to the target directory on your remote server.
This way, the private key is never written to the disk / being exposed.
There is yet another easier way to run scp using the Copy via ssh GitHub action.
- name: Copy folder content recursively to remote
uses: garygrossgarten/github-action-scp#release
with:
local: my-repo
remote: ~/target/directory
host: 123.132.123.132
port: 59666
username: tomas
privateKey: ${{secrets.PRIVATE_KEY}}
I have encountered similar problem.
In my case, problem is this (appleboy/ssh-action#master) action file.
Just replace this action file with other action files from Github Marketplace
I have used LuisEnMarroquin/setup-ssh-action#v2.0.0 action file.
My Workflow File:
name: SSH to Ubuntu EC2
on:
push:
branches:
- main
jobs:
ssh-to-ec2:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Set up SSH key
uses: LuisEnMarroquin/setup-ssh-action#v2.0.0
with:
ORIGIN: ${{ secrets.HOST }}
SSHKEY: ${{ secrets.TEST }}
NAME: production
PORT: 22
USER: ubuntu
- run: ssh production "ls -la;id; echo hehe > h.txt "
I have GitHub repo and I want to use GithubActions to automatically execute unit Tests with every pull request.
I already set up a workflow file:
name: CI
on:
pull_request:
branches: [ master ]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
- name: Install
run: npm ci
- name: Linter
run: npm run lint
- name: Build
run: npm run build
- name: Docker
run: docker-compose up -d
- name: Wait / Sleep
uses: jakejarvis/wait-action#v0.1.0
with:
time: '10s'
- run: |
docker ps
cat ./dumps/backup.sql | docker exec -i mysql-development_1 /usr/bin/mysql -u root --password=password
- name: Test
run: npm test
As I need to insert the tables first, I want to insert a dump which does work on my machine with the exact same command used here.
However, the Action fails with this error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
read unix #->/var/run/docker.sock: read: connection reset by peer
cat: write error: Broken pipe
##[error]Process completed with exit code 1.
How can I access the database within GithubActions?
docker-compose.yml:
version: '3'
services:
mysql-development:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: password
MYSQL_DATABASE: test_db
ports:
- "3308:3306"
To anyone who might run into the same problem:
https://github.blog/changelog/2020-02-21-github-actions-breaking-change-ubuntu-virtual-environments-will-no-longer-start-the-mysql-service-automatically/
You just need to start the mysql-service manually and perhaps wait for a couple of seconds