You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

2 Días
0 Hrs
3 Min
51 Seg
Curso de GitHub Actions

Curso de GitHub Actions

Juan José Torres

Juan José Torres

Uso de variables para almacenar información para reutilizar en un workflow

9/18
Resources

We start our exploration in the GitHub Marketplace, where we discover different Actions. For our example, we select 'Deploy Docker to AWS'. This Action takes a Docker application and deploys it to an S2 instance of AWS.

What does this AWS Action require to work?

The Action requires us to configure several variables for it. Through Secrets, we can configure an access key ID, a secret access key, a default region and an .env. This is what the Action needs to upload a Docker application to an AWS S2 instance.

The values of these variables are obtained from our AWS account credentials. They are stored in the GitHub repository as secrets, protecting their security.

From there, we can copy the Workflow that uses these secrets in case of need if we want to upload a Docker application to AWS.

How can we verify and debug a Workflow?

Once the Workflow is deployed, it is important to check the logs to verify that it is working correctly. In the actions tab, you can see the logs of each Workflow execution.

For example, when exploring a failed Workflow, we can see the error 'process completed with exit code 1', indicating that the process was not completed correctly. By detailing each of the jobs, we can identify the step where the failure occurred.

Although GitHub indicates whether a job has been successful or not, it is always important to check the logs, as there may be errors not detected by GitHub.

How to troubleshoot Workflows?

When reviewing the logs, we noticed that the Workflow greeting 'greetings and secrets' was not being displayed correctly. The analysis of the logs revealed that the variable 'greeting' was only configured for the job 'greeting variables' and not for the job 'greetings and secrets'.

To fix this problem, it is necessary to add the variable 'greeting' to the job 'greetings and secrets'. With this fix, the greeting is displayed correctly in the logs, indicating that the Workflow is working as expected.

How to avoid errors when creating new Workflows?

When creating a new Workflow, it is essential to test it several times and carefully review all logs. In this way, we can be sure that the Workflow is working as expected.

Contributions 7

Questions 1

Sort by:

Want to see more contributions, questions and answers from the community?

Sería bueno que el curso lo enfocaran a un nivel más realista o como realmente se pueda implementar, por ejemplo en una empresa o situaciones que se presenten el día a día, ya que en el curso tiene un enfoque muy teórico y la poca práctica que tiene no se centran en casos que se puedan implementar en un futuro.

name: Saludo usando variables
on:
  push
env:
  DIA_DE_SEMANA: Lunes
jobs:
  mostrar-variables:
    env:
      VARS: ${{ toJSON(vars) }}
    runs-on: ubuntu-latest
    steps:
      - name: imprimir variables
        run: echo $VARS
  saludo-variables:
    runs-on: ubuntu-latest
    env:
      SALUDO: Hola
    steps:
      - name: saludar
        run : echo $SALUDO, $NOMBRE y $NOMBRE2. Hoy es $DIA_DE_SEMANA
        env:
          NOMBRE2: Villanos
          NOMBRE: ${{ vars.NOMBRE }}

Recordatorio: para acceder a una variable dentro de una expresión, se debe hacer por medio del contexto env.

if: ${{ env.USERNAME == 'diego' }}
run: echo $USERNAME
Siento la información desordenada, creo que lo que el profesor menciono sobre contextos aquí, debió haberlo mostrado antes del reto.
# Uso de variables de entorno GitHub Actions permite manejar información sensible y valores reutilizables mediante **secretos** y **variables de entorno**. Ambos se usan para mantener el flujo de trabajo seguro y organizado. 1. Secretos (`secrets`)Los **secretos** son valores sensibles, como contraseñas, tokens de acceso o claves API, que se almacenan de forma segura en GitHub y no se muestran en los registros de ejecución.**¿Cómo definir un secreto?Uso de secretos en un workflow:**name: Uso de Secretos on: \[push] jobs: usar-secreto: runs-on: ubuntu-latest steps: \- name: Mostrar secreto de ejemplo run: echo "El token es: ${{ secrets.MI\_TOKEN }}" \<aside> ‼️**Nota:** Los secretos nunca se muestran en los registros por razones de seguridad. Si intentas imprimirlos directamente, se ocultarán automáticamente.\</aside> * Ve a tu repositorio en GitHub. * Haz clic en **Settings → Secrets and variables → Actions → New repository secret**. * Asigna un nombre (en mayúsculas, por convención) y su valor. 2. Variables de entorno (`env`)Las **variables de entorno** son valores reutilizables que no son necesariamente sensibles. Sirven para almacenar configuraciones que se repiten a lo largo del workflow.name: Uso de Variables de Entorno on: \[push] jobs: variables-ejemplo: runs-on: ubuntu-latest env: ENTORNO: produccion VERSIÓN\_APP: 1.0.0 steps: \- name: Mostrar variables run: | echo "Entorno actual: ${{ env.ENTORNO }}" echo "Versión de la app: ${{ env.VERSIÓN\_APP }}" 3. **Secretos y variables a nivel de organización**Si trabajas en múltiples repositorios, puedes definir secretos y variables a nivel de organización: 1. Ve a **Settings → Secrets and variables → Actions → New organization secret**. 2. Define el alcance (todos los repositorios o solo algunos).
```js name: Using variables on: push env: environment_var: development token: ${{ secrets.TOKEN_TEST }} jobs: deploy_to_environment: runs-on: ubuntu-latest steps: - name: Deploy to environment run: echo "Deploying to ${{ env.environment_var }} environment with token ${{ env.token }}" ```

Reto 5

name: Reto 5 - Aprende a usar las Variables de Entorno
on: [push, pull_request]
env:
  fruta1: Manzana
  fruta2: Pera
  fruta3: Frutilla
jobs:
  Variables:
    runs-on: ubuntu-latest
    steps:
      - name: echo frutas
        run: echo "La $fruta1 es roja, la $fruta2 es verde, la $fruta3 es roja y el ${{ vars.FRUTA4 }} es amarillo"